mirror of
https://gitlab.com/news-flash/article_scraper.git
synced 2025-07-08 08:30:00 +02:00
require client for parsing
This commit is contained in:
parent
a7c247549a
commit
d2960d8539
2 changed files with 31 additions and 48 deletions
|
@ -15,29 +15,18 @@ use url;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
pub struct ImageDownloader {
|
pub struct ImageDownloader {
|
||||||
client: Client,
|
|
||||||
max_size: (u32, u32),
|
max_size: (u32, u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImageDownloader {
|
impl ImageDownloader {
|
||||||
pub fn new(max_size: (u32, u32)) -> Self {
|
pub fn new(max_size: (u32, u32)) -> Self {
|
||||||
Self::new_with_client(max_size, Client::new())
|
ImageDownloader { max_size }
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_client(max_size: (u32, u32), client: Client) -> Self {
|
|
||||||
ImageDownloader {
|
|
||||||
client,
|
|
||||||
max_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_client(&mut self, client: Client) {
|
|
||||||
self.client = client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn download_images_from_string(
|
pub async fn download_images_from_string(
|
||||||
&self,
|
&self,
|
||||||
html: &str,
|
html: &str,
|
||||||
|
client: &Client,
|
||||||
) -> Result<String, ImageDownloadError> {
|
) -> Result<String, ImageDownloadError> {
|
||||||
let parser = Parser::default_html();
|
let parser = Parser::default_html();
|
||||||
let doc = parser.parse_string(html).map_err(|_| {
|
let doc = parser.parse_string(html).map_err(|_| {
|
||||||
|
@ -50,7 +39,8 @@ impl ImageDownloader {
|
||||||
ImageDownloadErrorKind::HtmlParse
|
ImageDownloadErrorKind::HtmlParse
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.download_images_from_context(&xpath_ctx).await?;
|
self.download_images_from_context(&xpath_ctx, client)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let options = SaveOptions {
|
let options = SaveOptions {
|
||||||
format: false,
|
format: false,
|
||||||
|
@ -68,6 +58,7 @@ impl ImageDownloader {
|
||||||
pub async fn download_images_from_context(
|
pub async fn download_images_from_context(
|
||||||
&self,
|
&self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
|
client: &Client,
|
||||||
) -> Result<(), ImageDownloadError> {
|
) -> Result<(), ImageDownloadError> {
|
||||||
let xpath = "//img";
|
let xpath = "//img";
|
||||||
let node_vec = ArticleScraper::evaluate_xpath(context, xpath, false)
|
let node_vec = ArticleScraper::evaluate_xpath(context, xpath, false)
|
||||||
|
@ -76,13 +67,13 @@ impl ImageDownloader {
|
||||||
if let Some(url) = node.get_property("src") {
|
if let Some(url) = node.get_property("src") {
|
||||||
if !url.starts_with("data:") {
|
if !url.starts_with("data:") {
|
||||||
if let Ok(url) = url::Url::parse(&url) {
|
if let Ok(url) = url::Url::parse(&url) {
|
||||||
let parent_url = match self.check_image_parent(&node, &url).await {
|
let parent_url = match self.check_image_parent(&node, &url, client).await {
|
||||||
Ok(url) => Some(url),
|
Ok(url) => Some(url),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok((small_image, big_image)) =
|
if let Ok((small_image, big_image)) =
|
||||||
self.save_image(&url, &parent_url).await
|
self.save_image(&url, &parent_url, client).await
|
||||||
{
|
{
|
||||||
if let Err(_) = node.set_property("src", &small_image) {
|
if let Err(_) = node.set_property("src", &small_image) {
|
||||||
return Err(ImageDownloadErrorKind::HtmlParse)?;
|
return Err(ImageDownloadErrorKind::HtmlParse)?;
|
||||||
|
@ -105,9 +96,9 @@ impl ImageDownloader {
|
||||||
&self,
|
&self,
|
||||||
image_url: &url::Url,
|
image_url: &url::Url,
|
||||||
parent_url: &Option<url::Url>,
|
parent_url: &Option<url::Url>,
|
||||||
|
client: &Client,
|
||||||
) -> Result<(String, Option<String>), ImageDownloadError> {
|
) -> Result<(String, Option<String>), ImageDownloadError> {
|
||||||
let response = self
|
let response = client
|
||||||
.client
|
|
||||||
.get(image_url.clone())
|
.get(image_url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -133,8 +124,7 @@ impl ImageDownloader {
|
||||||
let mut big_image: Option<Vec<u8>> = None;
|
let mut big_image: Option<Vec<u8>> = None;
|
||||||
|
|
||||||
if let Some(parent_url) = parent_url {
|
if let Some(parent_url) = parent_url {
|
||||||
let response_big = self
|
let response_big = client
|
||||||
.client
|
|
||||||
.get(parent_url.clone())
|
.get(parent_url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -271,22 +261,21 @@ impl ImageDownloader {
|
||||||
&self,
|
&self,
|
||||||
node: &Node,
|
node: &Node,
|
||||||
child_url: &url::Url,
|
child_url: &url::Url,
|
||||||
|
client: &Client,
|
||||||
) -> Result<url::Url, ImageDownloadError> {
|
) -> Result<url::Url, ImageDownloadError> {
|
||||||
if let Some(parent) = node.get_parent() {
|
if let Some(parent) = node.get_parent() {
|
||||||
if parent.get_name() == "a" {
|
if parent.get_name() == "a" {
|
||||||
if let Some(url) = parent.get_property("href") {
|
if let Some(url) = parent.get_property("href") {
|
||||||
let parent_url =
|
let parent_url =
|
||||||
url::Url::parse(&url).context(ImageDownloadErrorKind::ParentDownload)?;
|
url::Url::parse(&url).context(ImageDownloadErrorKind::ParentDownload)?;
|
||||||
let parent_response = self
|
let parent_response = client
|
||||||
.client
|
|
||||||
.head(parent_url.clone())
|
.head(parent_url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context(ImageDownloadErrorKind::ParentDownload)?;
|
.context(ImageDownloadErrorKind::ParentDownload)?;
|
||||||
let _ = ImageDownloader::check_image_content_type(&parent_response)
|
let _ = ImageDownloader::check_image_content_type(&parent_response)
|
||||||
.context(ImageDownloadErrorKind::ParentDownload)?;
|
.context(ImageDownloadErrorKind::ParentDownload)?;
|
||||||
let child_response = self
|
let child_response = client
|
||||||
.client
|
|
||||||
.get(child_url.clone())
|
.get(child_url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -326,6 +315,7 @@ impl ImageDownloader {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use reqwest::Client;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
@ -335,7 +325,7 @@ mod tests {
|
||||||
let hdyleaflet = fs::read_to_string(r"./resources/tests/planetGnome/fedora31.html")
|
let hdyleaflet = fs::read_to_string(r"./resources/tests/planetGnome/fedora31.html")
|
||||||
.expect("Failed to read HTML");
|
.expect("Failed to read HTML");
|
||||||
let result = image_dowloader
|
let result = image_dowloader
|
||||||
.download_images_from_string(&hdyleaflet)
|
.download_images_from_string(&hdyleaflet, &Client::new())
|
||||||
.await
|
.await
|
||||||
.expect("Failed to downalod images");
|
.expect("Failed to downalod images");
|
||||||
let mut file =
|
let mut file =
|
||||||
|
|
39
src/lib.rs
39
src/lib.rs
|
@ -27,15 +27,10 @@ use url;
|
||||||
pub struct ArticleScraper {
|
pub struct ArticleScraper {
|
||||||
pub image_downloader: ImageDownloader,
|
pub image_downloader: ImageDownloader,
|
||||||
config_files: Arc<RwLock<Option<ConfigCollection>>>,
|
config_files: Arc<RwLock<Option<ConfigCollection>>>,
|
||||||
client: Client,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ArticleScraper {
|
impl ArticleScraper {
|
||||||
pub fn new(config_path: PathBuf) -> Self {
|
pub fn new(config_path: PathBuf) -> Self {
|
||||||
Self::new_with_client(config_path, Client::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_client(config_path: PathBuf, client: Client) -> Self {
|
|
||||||
let config_files = Arc::new(RwLock::new(None));
|
let config_files = Arc::new(RwLock::new(None));
|
||||||
|
|
||||||
let locked_config_files = config_files.clone();
|
let locked_config_files = config_files.clone();
|
||||||
|
@ -54,25 +49,19 @@ impl ArticleScraper {
|
||||||
});
|
});
|
||||||
|
|
||||||
ArticleScraper {
|
ArticleScraper {
|
||||||
image_downloader: ImageDownloader::new_with_client((2048, 2048), client.clone()),
|
image_downloader: ImageDownloader::new((2048, 2048)),
|
||||||
config_files,
|
config_files,
|
||||||
client,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_client(&mut self, client: Client) {
|
|
||||||
self.client = client.clone();
|
|
||||||
self.image_downloader.set_client(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn parse(
|
pub async fn parse(
|
||||||
&self,
|
&self,
|
||||||
url: url::Url,
|
url: url::Url,
|
||||||
download_images: bool,
|
download_images: bool,
|
||||||
|
client: &Client,
|
||||||
) -> Result<Article, ScraperError> {
|
) -> Result<Article, ScraperError> {
|
||||||
info!("Scraping article: '{}'", url.as_str());
|
info!("Scraping article: '{}'", url.as_str());
|
||||||
let response = self
|
let response = client
|
||||||
.client
|
|
||||||
.head(url.clone())
|
.head(url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -117,7 +106,7 @@ impl ArticleScraper {
|
||||||
|
|
||||||
ArticleScraper::generate_head(&mut root, &document)?;
|
ArticleScraper::generate_head(&mut root, &document)?;
|
||||||
|
|
||||||
self.parse_pages(&mut article, &url, &mut root, &config)
|
self.parse_pages(&mut article, &url, &mut root, &config, client)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let context = Context::new(&document).map_err(|()| {
|
let context = Context::new(&document).map_err(|()| {
|
||||||
|
@ -138,7 +127,7 @@ impl ArticleScraper {
|
||||||
if download_images {
|
if download_images {
|
||||||
if let Err(error) = self
|
if let Err(error) = self
|
||||||
.image_downloader
|
.image_downloader
|
||||||
.download_images_from_context(&context)
|
.download_images_from_context(&context, client)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
error!("Downloading images failed: '{}'", error);
|
error!("Downloading images failed: '{}'", error);
|
||||||
|
@ -168,8 +157,9 @@ impl ArticleScraper {
|
||||||
url: &url::Url,
|
url: &url::Url,
|
||||||
root: &mut Node,
|
root: &mut Node,
|
||||||
config: &GrabberConfig,
|
config: &GrabberConfig,
|
||||||
|
client: &Client,
|
||||||
) -> Result<(), ScraperError> {
|
) -> Result<(), ScraperError> {
|
||||||
let html = ArticleScraper::download(&url, &self.client).await?;
|
let html = ArticleScraper::download(&url, client).await?;
|
||||||
let mut document = Self::parse_html(html, config)?;
|
let mut document = Self::parse_html(html, config)?;
|
||||||
let mut xpath_ctx = Self::get_xpath_ctx(&document)?;
|
let mut xpath_ctx = Self::get_xpath_ctx(&document)?;
|
||||||
|
|
||||||
|
@ -183,9 +173,10 @@ impl ArticleScraper {
|
||||||
if !result.trim().is_empty() {
|
if !result.trim().is_empty() {
|
||||||
// parse again with single page url
|
// parse again with single page url
|
||||||
debug!("Single page link found '{}'", result);
|
debug!("Single page link found '{}'", result);
|
||||||
let single_page_url = url::Url::parse(&result).context(ScraperErrorKind::Url)?;
|
let single_page_url =
|
||||||
|
url::Url::parse(&result).context(ScraperErrorKind::Url)?;
|
||||||
return self
|
return self
|
||||||
.parse_single_page(article, &single_page_url, root, config)
|
.parse_single_page(article, &single_page_url, root, config, client)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +188,7 @@ impl ArticleScraper {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(url) = self.check_for_next_page(&xpath_ctx, config) {
|
if let Some(url) = self.check_for_next_page(&xpath_ctx, config) {
|
||||||
let html = ArticleScraper::download(&url, &self.client).await?;
|
let html = ArticleScraper::download(&url, client).await?;
|
||||||
document = Self::parse_html(html, config)?;
|
document = Self::parse_html(html, config)?;
|
||||||
xpath_ctx = Self::get_xpath_ctx(&document)?;
|
xpath_ctx = Self::get_xpath_ctx(&document)?;
|
||||||
ArticleScraper::strip_junk(&xpath_ctx, config, &url);
|
ArticleScraper::strip_junk(&xpath_ctx, config, &url);
|
||||||
|
@ -261,8 +252,9 @@ impl ArticleScraper {
|
||||||
url: &url::Url,
|
url: &url::Url,
|
||||||
root: &mut Node,
|
root: &mut Node,
|
||||||
config: &GrabberConfig,
|
config: &GrabberConfig,
|
||||||
|
client: &Client,
|
||||||
) -> Result<(), ScraperError> {
|
) -> Result<(), ScraperError> {
|
||||||
let html = ArticleScraper::download(&url, &self.client).await?;
|
let html = ArticleScraper::download(&url, client).await?;
|
||||||
let document = Self::parse_html(html, config)?;
|
let document = Self::parse_html(html, config)?;
|
||||||
let xpath_ctx = Self::get_xpath_ctx(&document)?;
|
let xpath_ctx = Self::get_xpath_ctx(&document)?;
|
||||||
ArticleScraper::extract_metadata(&xpath_ctx, config, article);
|
ArticleScraper::extract_metadata(&xpath_ctx, config, article);
|
||||||
|
@ -811,6 +803,7 @@ impl ArticleScraper {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
use reqwest::Client;
|
||||||
|
|
||||||
#[tokio::test(basic_scheduler)]
|
#[tokio::test(basic_scheduler)]
|
||||||
async fn golem() {
|
async fn golem() {
|
||||||
|
@ -819,7 +812,7 @@ mod tests {
|
||||||
let url = url::Url::parse("https://www.golem.de/news/http-error-418-fehlercode-ich-bin-eine-teekanne-darf-bleiben-1708-129460.html").unwrap();
|
let url = url::Url::parse("https://www.golem.de/news/http-error-418-fehlercode-ich-bin-eine-teekanne-darf-bleiben-1708-129460.html").unwrap();
|
||||||
|
|
||||||
let grabber = ArticleScraper::new(config_path);
|
let grabber = ArticleScraper::new(config_path);
|
||||||
let article = grabber.parse(url, true).await.unwrap();
|
let article = grabber.parse(url, true, &Client::new()).await.unwrap();
|
||||||
article.save_html(&out_path).unwrap();
|
article.save_html(&out_path).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -841,7 +834,7 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let grabber = ArticleScraper::new(config_path);
|
let grabber = ArticleScraper::new(config_path);
|
||||||
let article = grabber.parse(url, true).await.unwrap();
|
let article = grabber.parse(url, true, &Client::new()).await.unwrap();
|
||||||
article.save_html(&out_path).unwrap();
|
article.save_html(&out_path).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue