diff options
| author | Mariot Tsitoara <[email protected]> | 2021-01-02 17:16:15 +0100 |
|---|---|---|
| committer | Mariot Tsitoara <[email protected]> | 2021-01-02 17:16:15 +0100 |
| commit | 1b74d00ce9a143ed68bc723128fc21f572a4889f (patch) | |
| tree | f8f6c0090c93005e7cc5685fef2185ded47191f4 | |
| parent | Fix error result in let call (diff) | |
| download | chan-downloader-1b74d00ce9a143ed68bc723128fc21f572a4889f.tar.xz chan-downloader-1b74d00ce9a143ed68bc723128fc21f572a4889f.zip | |
Add log actions to lin and bin
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/bin.rs | 35 | ||||
| -rw-r--r-- | src/lib.rs | 9 |
3 files changed, 40 insertions, 6 deletions
@@ -21,7 +21,9 @@ path = "src/bin.rs" [dependencies] clap = {version = "2.33.3", features = ["yaml"]} +env_logger = "0.8.2" indicatif = "0.15.0" lazy_static = "1.4.0" +log = "0.4.11" regex = "1.4.2" reqwest = { version = "0.10", features = ["blocking"] } @@ -1,5 +1,7 @@ #[macro_use] extern crate clap; +#[macro_use] +extern crate log; use std::env; use std::fs::create_dir_all; @@ -12,31 +14,43 @@ use reqwest::blocking::Client; use chan_downloader::{get_image_links, get_page_content, get_thread_infos, save_image}; fn main() { + env_logger::init(); let yaml = load_yaml!("cli.yml"); let matches = App::from_yaml(yaml).get_matches(); let thread = matches.value_of("thread").unwrap(); let output = matches.value_of("output").unwrap_or("downloads"); + info!(target: "downloader_events", "Downloading images from {} to {}", thread, output); download_thread(thread, &output).unwrap(); } fn download_thread(thread_link: &str, output: &str) -> Result<String, Error> { let client = Client::builder().user_agent("reqwest").build()?; let workpath = env::current_dir().unwrap(); + info!("Working from {}", workpath.display()); let (board_name, thread_id) = get_thread_infos(thread_link); let directory = workpath.join(output).join(board_name).join(thread_id); if !directory.exists() { match create_dir_all(&directory) { - Ok(_) => {} - Err(err) => eprintln!("Failed to create new directory: {}", err), + Ok(_) => { + info!("Created directory {}", directory.display()); + } + Err(err) => { + error!("Failed to create new directory: {}", err); + eprintln!("Failed to create new directory: {}", err); + }, } } let page_string = match get_page_content(thread_link, &client) { - Ok(page_string) => page_string, + Ok(page_string) => { + info!("Loaded content from {}", thread_link); + page_string + }, Err(err) => { + error!("Failed to get content from {}", thread_link); eprintln!("Error: {}", err); String::from("") }, @@ -52,19 +66,28 @@ fn download_thread(thread_link: &str, output: &str) -> Result<String, Error> { for cap in links_iter.step_by(2) { let img_path = directory.join(&cap[2]); if !img_path.exists() { + let image_path = img_path.to_str().unwrap(); match save_image( format!("https:{}", &cap[1]).as_str(), - img_path.to_str().unwrap(), + image_path, &client, ) { - Ok(_) => {} - Err(err) => eprintln!("Error: {}", err), + Ok(path) => { + info!("Saved image to {}", path); + } + Err(err) => { + error!("Couldn't save image {}", image_path); + eprintln!("Error: {}", err); + }, } + } else { + info!("Image {} already exists. Skipped", img_path.display()); } pb.set_message(&cap[2].to_string()); pb.inc(1); } pb.finish_with_message("Done"); + info!("Download finished"); Ok(format!("Downloaded: {} in {}", thread_link, output)) } @@ -11,6 +11,7 @@ extern crate reqwest; use std::fs::File; use std::io::copy; +use log::info; use regex::{CaptureMatches, Regex}; use reqwest::Error; use reqwest::blocking::{Client}; @@ -33,6 +34,7 @@ use reqwest::blocking::{Client}; /// remove_file(answer).unwrap(); /// ``` pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Error> { + info!(target: "image_events", "Saving image to: {}", path); let response = client.get(url).send()?; if response.status().is_success() { @@ -40,6 +42,7 @@ pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Erro let content = response.text()?; copy(&mut content.as_bytes(), &mut dest).unwrap(); } + info!("Saved image to: {}", path); Ok(String::from(path)) } @@ -57,8 +60,10 @@ pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Erro /// } /// ``` pub fn get_page_content(url: &str, client: &Client) -> Result<String, Error> { + info!(target: "page_events", "Loading page: {}", url); let response = client.get(url).send()?; let content = response.text()?; + info!("Loaded page: {}", url); Ok(content) } @@ -74,10 +79,12 @@ pub fn get_page_content(url: &str, client: &Client) -> Result<String, Error> { /// assert_eq!(thread_id, "6872254"); /// ``` pub fn get_thread_infos(url: &str) -> (&str, &str) { + info!(target: "thread_events", "Getting thread infos from: {}", url); let url_vec: Vec<&str> = url.split('/').collect(); let board_name = url_vec[3]; let thread_vec: Vec<&str> = url_vec[5].split('#').collect(); let thread_id = thread_vec[0]; + info!("Got thread infos from: {}", url); (board_name, thread_id) } @@ -104,6 +111,7 @@ pub fn get_thread_infos(url: &str) -> (&str, &str) { /// } /// ``` pub fn get_image_links(page_content: &str) -> (CaptureMatches, usize) { + info!(target: "link_events", "Getting image links"); lazy_static! { static ref RE: Regex = Regex::new(r"(//i(?:s)?\d*\.(?:4cdn|4chan)\.org/\w+/(\d+\.(?:jpg|png|gif|webm)))") @@ -112,6 +120,7 @@ pub fn get_image_links(page_content: &str) -> (CaptureMatches, usize) { let links_iter = RE.captures_iter(page_content); let number_of_links = RE.captures_iter(page_content).count() / 2; + info!("Got {} image links from page", number_of_links); (links_iter, number_of_links) } |