add image downloading (nightmare nightmare nightmare i hate reqwest aaaaaaaaaaaaAA)

This commit is contained in:
SadlyNotSappho 2023-08-29 11:59:02 -07:00
parent f2d40849db
commit 1367f2d684
3 changed files with 35 additions and 2 deletions

View File

@ -9,7 +9,7 @@ edition = "2021"
clap = { version = "4.3.3", features = ["derive"] } clap = { version = "4.3.3", features = ["derive"] }
iced = "0.9.0" iced = "0.9.0"
regex = "1.8.4" regex = "1.8.4"
reqwest = "0.11.18" reqwest = {version="0.11.18",features=["blocking"]}
scraper = "0.16.0" scraper = "0.16.0"
serde = { version = "1.0.164", features = ["derive"] } serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.97" serde_json = "1.0.97"

View File

@ -0,0 +1,31 @@
use std::{fs, process};
pub async fn download_image(cache_path: &String, image_url: String, file_name: String) {
let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME
crate::ensure_exists(&replaced);
let image = match reqwest::get(image_url).await {
Ok(image) => match image.bytes().await {
Ok(bytes) => bytes,
Err(why) => {
eprintln!("cache::download_image: Couldn't get the bytes of the current page's image: {why:?}");
process::exit(1);
}
},
Err(why) => {
eprintln!("cache::download_image: Couldn't get the current image: {why:?}");
process::exit(1);
}
};
// println!("{image:?}");
// println!("{}", yansi::Color::Magenta.paint(filename.clone()));
// println!("{}", yansi::Color::Magenta.paint(filepath.clone()));
println!("{replaced}/{file_name}");
let wrote = fs::write(format!("{replaced}/{file_name}"), image);
if let Err(why) = wrote {
eprintln!("cache::download_image: Couldn't save the image to `{replaced}/{file_name}`: {why:?}");
process::exit(1);
}
}

View File

@ -1,4 +1,5 @@
use clap::Parser; use clap::Parser;
use ggg::cache;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -20,5 +21,6 @@ async fn main() {
let current_page = ggg::get_page(&config.latest_date).await; let current_page = ggg::get_page(&config.latest_date).await;
println!("{:?}", &config); println!("image url: {}\ncache folder: {}", &current_page.image, &config.cache_folder);
cache::download_image(&config.cache_folder, current_page.image, format!("{}.jpg", current_page.date)).await;
} }