caching, i think (i genuinely dont remember what half of this does but i know it works)

This commit is contained in:
SadlyNotSappho 2023-09-05 11:50:14 -07:00
parent 5b69c68f24
commit 7a6134b075
3 changed files with 77 additions and 19 deletions

View File

@ -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:?}");
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)
}
};
}

View File

@ -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<String>,
pub prev_page: Option<String>,
pub image: String,
pub cached: bool
pub cached: bool,
}
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);
}
}
}
pub async fn get_page(date: &str) -> Page {
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(),

View File

@ -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: {}", &current_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()[..]), &current_page).await;
}