From 7a6134b0752b477d3ee6583dd3b19b3bd4af5318 Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 5 Sep 2023 11:50:14 -0700 Subject: [PATCH] caching, i think (i genuinely dont remember what half of this does but i know it works) --- src/cache.rs | 51 ++++++++++++++++++++++++++++++++++++++------------- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++---- src/main.rs | 5 +++-- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 7683c62..0789d77 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,32 +1,57 @@ -use std::{fs, process, path::Path}; +use std::{fs, path::Path, process}; -pub async fn download_image(cache_path: &str, image_url: String, file_name: String) { +use crate::Page; + +pub async fn download_page_image(cache_path: &str, page: &Page) { let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME crate::ensure_exists(&replaced); - if Path::new(&format!("{cache_path}/{file_name}.json")).exists() { - return + if Path::new(&format!("{cache_path}/{}.json", page.date)).exists() { + return; } - let image = match reqwest::get(image_url).await { + let image = match reqwest::get(&page.image).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:?}"); + eprintln!("cache::download_page_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:?}"); + eprintln!("cache::download_page_image: Couldn't get the current image: {why:?}"); process::exit(1); } }; // println!("{replaced}/{file_name}"); - - let wrote = fs::write(format!("{replaced}/images/{file_name}.jpg"), image); - if let Err(why) = wrote { - eprintln!("cache::download_image: Couldn't save the image to `{replaced}/{file_name}`: {why:?}"); - process::exit(1); - } + + match fs::write(format!("{replaced}/images/{}.jpg", page.date), image) { + Ok(var) => var, + Err(why) => { + eprintln!( + "cache::download_page_image: Couldn't save the image to `{replaced}/images/{}`: {why:?}", + page.date + ); + process::exit(1); + } + }; + + match fs::write( + format!("{replaced}/{}.json", page.date), + match serde_json::to_string(page) { + Ok(var) => var, + Err(why) => { + eprintln!("cache::download_page_image: Couldn't serialize Page struct: {why:?}"); + process::exit(1) + } + } + .replace("\"cached\":false", "\"cached\":true"), // war crimes + ) { + Ok(var) => var, + Err(why) => { + eprintln!("cache::download_page_image: Couldn't save the Page struct to {replaced}/{}.json: {why:?}", page.date); + process::exit(1) + } + }; } diff --git a/src/lib.rs b/src/lib.rs index ef88d0a..6729922 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,7 @@ pub fn create_config(file: &str) -> bool { if file_path.exists() && !Path::new(&replaced).exists() { let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}"; - + match fs::write(replaced, file_contents) { Ok(var) => var, Err(why) => { @@ -138,16 +138,44 @@ pub struct Config { pub cache_folder: String, } -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct Page { pub date: String, pub next_page: Option, pub prev_page: Option, pub image: String, - pub cached: bool + pub cached: bool, } -pub async fn get_page(date: &str) -> Page { +pub async fn get_page(date: &str, config: &Config) -> Page { + if Path::new(&format!( + "{}/{date}.json", + config.cache_folder.replace('~', &get_home()[..]) + )) + .exists() + { + match serde_json::from_str( + &match fs::read_to_string( + &format!( + "{}/{date}.json", + config.cache_folder.replace('~', &get_home()[..]) + )[..], + ) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::get_page: Couldn't read cache data: {why:?}"); + process::exit(1) + } + }[..], + ) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::get_page: Couldn't serialize read cache data: {why:?}"); + process::exit(1); + } + } + } + let page_html = &match match reqwest::get(format!( "https://girlgeniusonline.com/comic.php?date={date}" )) @@ -194,6 +222,10 @@ pub async fn get_page(date: &str) -> Page { // check if cache/date.json exists // if it does, cached = true // if it doesn't, cached = false + let mut cached = false; + if Path::new(&format!("{}/{date}.json", config.cache_folder)).exists() { + cached = true; + } Page { date: date.to_string(), diff --git a/src/main.rs b/src/main.rs index a2f394f..0b9966d 100755 --- a/src/main.rs +++ b/src/main.rs @@ -18,9 +18,10 @@ async fn main() { let config = ggg::read_config(&config_file); ggg::ensure_exists(&config.cache_folder); + ggg::ensure_exists(&format!("{}/images", &config.cache_folder)[..]); - let current_page = ggg::get_page(&config.latest_date).await; + let current_page = ggg::get_page(&config.latest_date, &config).await; println!("image url: {}\ncache folder: {}", ¤t_page.image, &config.cache_folder); - cache::download_image(&config.cache_folder.replace('~', &ggg::get_home()[..]), current_page.image, current_page.date).await; + cache::download_page_image(&config.cache_folder.replace('~', &ggg::get_home()[..]), ¤t_page).await; }