Compare commits

..

No commits in common. "5b69c68f24f9f1468292bd30ca625507dc8a26e1" and "1912b7bcb8edcd7703b2d92de3bd210e61796a7c" have entirely different histories.

3 changed files with 4 additions and 14 deletions

View File

@ -1,13 +1,9 @@
use std::{fs, process, path::Path}; use std::{fs, process};
pub async fn download_image(cache_path: &str, image_url: String, file_name: String) { pub async fn download_image(cache_path: &str, image_url: String, file_name: String) {
let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME
crate::ensure_exists(&replaced); crate::ensure_exists(&replaced);
if Path::new(&format!("{cache_path}/{file_name}.json")).exists() {
return
}
let image = match reqwest::get(image_url).await { let image = match reqwest::get(image_url).await {
Ok(image) => match image.bytes().await { Ok(image) => match image.bytes().await {
Ok(bytes) => bytes, Ok(bytes) => bytes,
@ -24,7 +20,7 @@ pub async fn download_image(cache_path: &str, image_url: String, file_name: Stri
// println!("{replaced}/{file_name}"); // println!("{replaced}/{file_name}");
let wrote = fs::write(format!("{replaced}/images/{file_name}.jpg"), image); let wrote = fs::write(format!("{replaced}/{file_name}"), image);
if let Err(why) = wrote { if let Err(why) = wrote {
eprintln!("cache::download_image: Couldn't save the image to `{replaced}/{file_name}`: {why:?}"); eprintln!("cache::download_image: Couldn't save the image to `{replaced}/{file_name}`: {why:?}");
process::exit(1); process::exit(1);

View File

@ -39,7 +39,7 @@ pub fn create_config(file: &str) -> bool {
let file_path = std::path::Path::new(&path); // Path without the filename let file_path = std::path::Path::new(&path); // Path without the filename
if file_path.parent().is_some() && !file_path.exists() { if file_path.parent().is_some() {
// if the file_path has a parent folder // if the file_path has a parent folder
println!("lib::create_config: creating folder {path}"); println!("lib::create_config: creating folder {path}");
match fs::create_dir_all(&path) { match fs::create_dir_all(&path) {
@ -144,7 +144,6 @@ pub struct Page {
pub next_page: Option<String>, pub next_page: Option<String>,
pub prev_page: Option<String>, pub prev_page: Option<String>,
pub image: String, pub image: String,
pub cached: bool
} }
pub async fn get_page(date: &str) -> Page { pub async fn get_page(date: &str) -> Page {
@ -191,15 +190,10 @@ pub async fn get_page(date: &str) -> Page {
prev_page = Some(element.value().attr("href").unwrap().to_string()); prev_page = Some(element.value().attr("href").unwrap().to_string());
} }
// check if cache/date.json exists
// if it does, cached = true
// if it doesn't, cached = false
Page { Page {
date: date.to_string(), date: date.to_string(),
next_page, next_page,
prev_page, prev_page,
image, image,
cached,
} }
} }

View File

@ -22,5 +22,5 @@ async fn main() {
let current_page = ggg::get_page(&config.latest_date).await; let current_page = ggg::get_page(&config.latest_date).await;
println!("image url: {}\ncache folder: {}", &current_page.image, &config.cache_folder); 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_image(&config.cache_folder.replace('~', &ggg::get_home()[..]), current_page.image, format!("{}.jpg", current_page.date)).await;
} }