caching, i think (i genuinely dont remember what half of this does but i know it works)
This commit is contained in:
parent
5b69c68f24
commit
7a6134b075
51
src/cache.rs
51
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)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
40
src/lib.rs
40
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<String>,
|
||||
pub prev_page: Option<String>,
|
||||
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(),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue