Refactor fetch_url

This adds:
- More validation of responses to ensure the HTML response is valid.
- Better handling of redirecting URLs which allows for fetching of
  links proxied to Medium.
This commit is contained in:
Kenneth Gitere 2021-01-24 17:49:42 +03:00
parent 1dc7b3432b
commit 21c3ffd922

View file

@ -23,70 +23,106 @@ fn main() {
type HTMLResource = (String, String); type HTMLResource = (String, String);
async fn fetch_url(url: &str) -> Result<HTMLResource, Box<dyn std::error::Error>> { async fn fetch_url(url: &str) -> Result<HTMLResource, Box<dyn std::error::Error + Send + Sync>> {
let client = surf::Client::new(); let client = surf::Client::new();
println!("Fetching..."); println!("Fetching...");
let mut res = client
.with(surf::middleware::Redirect::default()) let mut redirect_count: u8 = 0;
.get(url) let base_url = Url::parse(&url)?;
.send() let mut url = base_url.clone();
.await while redirect_count < 5 {
.expect(&format!("Unable to fetch {}", url)); redirect_count += 1;
if res.status() == 200 { let req = surf::get(&url);
Ok((url.to_string(), res.body_string().await?)) let mut res = client.send(req).await?;
} else { if res.status().is_redirection() {
Err("Request failed to return HTTP 200".into()) if let Some(location) = res.header(surf::http::headers::LOCATION) {
match Url::parse(location.last().as_str()) {
Ok(valid_url) => url = valid_url,
Err(e) => match e {
url::ParseError::RelativeUrlWithoutBase => {
url = base_url.join(location.last().as_str())?
}
e => return Err(e.into()),
},
};
}
} else if res.status().is_success() {
if let Some(mime) = res.content_type() {
if mime.essence() == "text/html" {
return Ok((url.to_string(), res.body_string().await?));
} else {
return Err(format!(
"Invalid HTTP response. Received {} instead of text/html",
mime.essence()
)
.into());
}
} else {
return Err("Unknown HTTP response".into());
}
} else {
return Err(format!("Request failed: HTTP {}", res.status()).into());
}
} }
Err("Unable to fetch HTML".into())
} }
fn download(urls: Vec<String>) { fn download(urls: Vec<String>) {
let mut async_url_tasks = Vec::with_capacity(urls.len()); let mut async_url_tasks = Vec::with_capacity(urls.len());
for url in urls { for url in urls {
async_url_tasks.push(task::spawn(async move { fetch_url(&url).await.unwrap() })); async_url_tasks.push(task::spawn(async move { fetch_url(&url).await }));
} }
task::block_on(async { task::block_on(async {
for url_task in async_url_tasks { for url_task in async_url_tasks {
let (url, html) = url_task.await; match url_task.await {
println!("Extracting"); Ok((url, html)) => {
let mut extractor = Extractor::from_html(&html); println!("Extracting");
extractor.extract_content(&url); let mut extractor = Extractor::from_html(&html);
if extractor.article().is_some() { extractor.extract_content(&url);
extractor if extractor.article().is_some() {
.download_images(&Url::parse(&url).unwrap()) extractor
.await .download_images(&Url::parse(&url).unwrap())
.expect("Unable to download images"); .await
let file_name = format!( .expect("Unable to download images");
"{}.epub", let file_name = format!(
extractor "{}.epub",
.metadata() extractor
.title() .metadata()
.replace("/", " ") .title()
.replace("\\", " ") .replace("/", " ")
); .replace("\\", " ")
let mut out_file = File::create(&file_name).unwrap(); );
let mut html_buf = Vec::new(); let mut out_file = File::create(&file_name).unwrap();
extractor::serialize_to_xhtml(extractor.article().unwrap(), &mut html_buf) let mut html_buf = Vec::new();
.expect("Unable to serialize to xhtml"); extractor::serialize_to_xhtml(extractor.article().unwrap(), &mut html_buf)
let html_buf = std::str::from_utf8(&html_buf).unwrap(); .expect("Unable to serialize to xhtml");
let mut epub = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap(); let html_buf = std::str::from_utf8(&html_buf).unwrap();
if let Some(author) = extractor.metadata().byline() { let mut epub = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap();
epub.metadata("author", author.replace("&", "&amp;")) if let Some(author) = extractor.metadata().byline() {
.unwrap(); epub.metadata("author", author.replace("&", "&amp;"))
} .unwrap();
epub.metadata("title", extractor.metadata().title().replace("&", "&amp;")) }
.unwrap(); epub.metadata("title", extractor.metadata().title().replace("&", "&amp;"))
epub.add_content(EpubContent::new("index.xhtml", html_buf.as_bytes())) .unwrap();
.unwrap(); epub.add_content(EpubContent::new("index.xhtml", html_buf.as_bytes()))
for img in extractor.img_urls { .unwrap();
let mut file_path = std::env::temp_dir(); for img in extractor.img_urls {
file_path.push(&img.0); let mut file_path = std::env::temp_dir();
file_path.push(&img.0);
let img_buf = File::open(&file_path).expect("Can't read file"); let img_buf = File::open(&file_path).expect("Can't read file");
epub.add_resource(file_path.file_name().unwrap(), img_buf, img.1.unwrap()) epub.add_resource(
.unwrap(); file_path.file_name().unwrap(),
img_buf,
img.1.unwrap(),
)
.unwrap();
}
epub.generate(&mut out_file).unwrap();
println!("Created {:?}", file_name);
}
} }
epub.generate(&mut out_file).unwrap(); Err(e) => println!("{}", e),
println!("Created {:?}", file_name);
} }
} }
}) })