Change using Result
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use reqwest::{Client, ClientBuilder, Url};
|
||||
use robotstxt::{DefaultMatcher, RobotsParseHandler};
|
||||
use crate::constants::{APP_CACHE_PATH, APP_DATA_DIR};
|
||||
use reqwest::{Client, Url};
|
||||
use robotstxt::{DefaultMatcher};
|
||||
use color_eyre::Result;
|
||||
use crate::constants::{APP_CACHE_PATH};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Crawler {
|
||||
id: String,
|
||||
base_url: Url,
|
||||
@@ -14,7 +16,7 @@ impl Crawler {
|
||||
let crawler = Self {
|
||||
id: id.to_string(),
|
||||
client: Client::new(),
|
||||
robots_txt: Self::get_robots_txt(id, &base_url).await,
|
||||
robots_txt: Self::get_robots_txt(id, &base_url).await.unwrap(),
|
||||
base_url,
|
||||
};
|
||||
let mut matcher = DefaultMatcher::default();
|
||||
@@ -23,7 +25,7 @@ impl Crawler {
|
||||
crawler
|
||||
}
|
||||
|
||||
async fn get_robots_txt(id: &str, base_url: &Url) -> String {
|
||||
async fn get_robots_txt(id: &str, base_url: &Url) -> Result<String> {
|
||||
let local_robots_path = APP_CACHE_PATH.clone()
|
||||
.join(id).join("robots.txt");
|
||||
if !local_robots_path.exists() {
|
||||
@@ -31,13 +33,14 @@ impl Crawler {
|
||||
robots_url.set_path("/robots.txt");
|
||||
let response = reqwest::get(robots_url).await
|
||||
.expect(format!("Failed to get robots.txt in `{}/robots.txt`", base_url.as_str()).as_str());
|
||||
let content = response.text().await.unwrap();
|
||||
tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await.unwrap();
|
||||
tokio::fs::write(&local_robots_path, &content).await.unwrap();
|
||||
content
|
||||
let content = response.text().await?;
|
||||
tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await?;
|
||||
tokio::fs::write(&local_robots_path, &content).await?;
|
||||
Ok(content)
|
||||
|
||||
}
|
||||
else {
|
||||
tokio::fs::read_to_string(&local_robots_path).await.unwrap()
|
||||
Ok(tokio::fs::read_to_string(&local_robots_path).await?)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user