Compare commits
3 Commits
1912b7bcb8
...
5b69c68f24
Author | SHA1 | Date |
---|---|---|
SadlyNotSappho | 5b69c68f24 | |
SadlyNotSappho | ce75fe6572 | |
SadlyNotSappho | 90a305f55f |
|
@ -1,9 +1,13 @@
|
|||
use std::{fs, process};
|
||||
use std::{fs, process, path::Path};
|
||||
|
||||
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
|
||||
crate::ensure_exists(&replaced);
|
||||
|
||||
if Path::new(&format!("{cache_path}/{file_name}.json")).exists() {
|
||||
return
|
||||
}
|
||||
|
||||
let image = match reqwest::get(image_url).await {
|
||||
Ok(image) => match image.bytes().await {
|
||||
Ok(bytes) => bytes,
|
||||
|
@ -20,7 +24,7 @@ pub async fn download_image(cache_path: &str, image_url: String, file_name: Stri
|
|||
|
||||
// println!("{replaced}/{file_name}");
|
||||
|
||||
let wrote = fs::write(format!("{replaced}/{file_name}"), image);
|
||||
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);
|
||||
|
|
|
@ -39,7 +39,7 @@ pub fn create_config(file: &str) -> bool {
|
|||
|
||||
let file_path = std::path::Path::new(&path); // Path without the filename
|
||||
|
||||
if file_path.parent().is_some() {
|
||||
if file_path.parent().is_some() && !file_path.exists() {
|
||||
// if the file_path has a parent folder
|
||||
println!("lib::create_config: creating folder {path}");
|
||||
match fs::create_dir_all(&path) {
|
||||
|
@ -144,6 +144,7 @@ pub struct Page {
|
|||
pub next_page: Option<String>,
|
||||
pub prev_page: Option<String>,
|
||||
pub image: String,
|
||||
pub cached: bool
|
||||
}
|
||||
|
||||
pub async fn get_page(date: &str) -> Page {
|
||||
|
@ -190,10 +191,15 @@ pub async fn get_page(date: &str) -> Page {
|
|||
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 {
|
||||
date: date.to_string(),
|
||||
next_page,
|
||||
prev_page,
|
||||
image,
|
||||
cached,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,5 +22,5 @@ async fn main() {
|
|||
let current_page = ggg::get_page(&config.latest_date).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, format!("{}.jpg", current_page.date)).await;
|
||||
cache::download_image(&config.cache_folder.replace('~', &ggg::get_home()[..]), current_page.image, current_page.date).await;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue