Added wallhaven downloading

This commit is contained in:
Benjamyn Love 2024-12-05 16:09:26 +11:00
parent c1a3083fab
commit edb02fd8ec
4 changed files with 82 additions and 73 deletions

View File

@ -62,6 +62,7 @@ pub struct Config {
pub wallpaper_dir: WallpaperDir, pub wallpaper_dir: WallpaperDir,
pub monitors: Monitors, pub monitors: Monitors,
pub wallhaven_api_key: String, pub wallhaven_api_key: String,
pub wallhaven_tmp_dir: String,
} }
impl Config { impl Config {
@ -102,7 +103,8 @@ impl Config {
} }
} }
let wallhaven_api_key = config.get("general", "wallhaven_api_key").unwrap(); let wallhaven_api_key = config.get("wallhaven", "api_key").unwrap();
let wallhaven_tmp_dir = config.get("wallhaven", "tmp_dir").unwrap();
Config { Config {
x_server: config.get("general", "x_server").unwrap(), x_server: config.get("general", "x_server").unwrap(),
@ -110,6 +112,7 @@ impl Config {
wallpaper_dir, wallpaper_dir,
monitors, monitors,
wallhaven_api_key, wallhaven_api_key,
wallhaven_tmp_dir,
} }
} }
} }

View File

@ -17,6 +17,12 @@ impl fmt::Display for Wallpaper {
} }
} }
impl Wallpaper {
pub fn new(path: String) -> Wallpaper {
Wallpaper { path: path }
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Wallpapers { pub struct Wallpapers {
ultrawide: Vec<Wallpaper>, ultrawide: Vec<Wallpaper>,

View File

@ -1,4 +1,6 @@
mod config; mod config;
use std::fmt::format;
use config::*; use config::*;
mod enums; mod enums;
mod files; mod files;
@ -19,11 +21,11 @@ fn main() {
// Initialise the `Wallpapers` struct with a clone of the config // Initialise the `Wallpapers` struct with a clone of the config
let mut wallpapers = Wallpapers::new(config.clone()); let mut wallpapers = Wallpapers::new(config.clone());
let api = WallHavenAPI::new(config.wallhaven_api_key); let api = WallHavenAPI::new(config.wallhaven_api_key, config.wallhaven_tmp_dir.clone());
let search_terms = vec![String::from("godzilla")]; let search_terms = vec![String::from("tsundere")];
let purities = vec![Purity::Sfw]; let purities = vec![Purity::Sketchy];
let categories = vec![Category::Anime, Category::General]; let categories = vec![Category::Anime];
let query = WallHavenQuery::new( let query = WallHavenQuery::new(
api, api,
@ -36,6 +38,8 @@ fn main() {
let results = query.run(); let results = query.run();
let wall = results.get_random();
println!("{}", results); println!("{}", results);
// // Load all wallpapers based on the config file specs // // Load all wallpapers based on the config file specs
// wallpapers.load_all(); // wallpapers.load_all();

View File

@ -3,8 +3,12 @@ use dbus::arg::RefArg;
use reqwest::*; use reqwest::*;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::fs::File;
use std::io;
use urlencoding::encode; use urlencoding::encode;
use crate::{Wallpaper, Wallpapers};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Thumbs { struct Thumbs {
large: String, large: String,
@ -35,8 +39,8 @@ impl fmt::Display for Meta {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct WallHavenResponse { pub struct WallHavenResponse {
data: Vec<WallHavenWallpaper>, pub data: Vec<WallHavenWallpaper>,
meta: Meta, pub meta: Meta,
} }
impl fmt::Display for WallHavenResponse { impl fmt::Display for WallHavenResponse {
@ -49,8 +53,14 @@ impl fmt::Display for WallHavenResponse {
} }
} }
impl WallHavenResponse {
pub fn get_random(&self) -> &WallHavenWallpaper {
self.data.get(0).unwrap()
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct WallHavenWallpaper { pub struct WallHavenWallpaper {
id: String, id: String,
url: String, url: String,
short_url: String, short_url: String,
@ -73,53 +83,40 @@ struct WallHavenWallpaper {
impl fmt::Display for WallHavenWallpaper { impl fmt::Display for WallHavenWallpaper {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "ID: {}\nURL: {}", &self.id, &self.path) write!(
f,
"ID: {}\tURL: {} ({})",
&self.id, &self.path, &self.purity
)
} }
} }
pub struct Tag { impl WallHavenWallpaper {
pub text: String, pub fn download(self, path: String) -> bool {
let image_resp = blocking::get(self.path).unwrap();
let mut outfile = File::create(path).unwrap();
io::copy(&mut image_resp.text().unwrap().as_bytes(), &mut outfile).unwrap();
true
} }
impl Tag { pub fn to_wallpaper(&self) -> Wallpaper {
pub fn new(text: String) -> Tag { Wallpaper::new(String::new())
Tag { text: text }
}
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", &self.text)
} }
} }
pub struct WallHavenAPI { pub struct WallHavenAPI {
pub api_key: String, pub api_key: String,
pub tags: Vec<Tag>, pub tmp_dir: String,
} }
impl WallHavenAPI { impl WallHavenAPI {
pub fn new(api_key: String) -> WallHavenAPI { pub fn new(api_key: String, tmp_dir: String) -> WallHavenAPI {
WallHavenAPI { WallHavenAPI {
api_key: api_key, api_key: api_key,
tags: Vec::<Tag>::new(), tmp_dir: tmp_dir,
} }
} }
pub fn add_tag(&mut self, text: String) {
self.tags.push(Tag::new(text));
}
}
impl fmt::Display for WallHavenAPI {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "API_KEY: REDACTED")?;
write!(f, "Tags: [")?;
for v in &self.tags {
write!(f, "{}, ", v)?;
}
write!(f, "]")
}
} }
pub enum Ratio { pub enum Ratio {
@ -232,48 +229,47 @@ impl WallHavenQuery {
} }
} }
pub fn test_request() { // pub fn test_request() {
let wallhaven = WallHavenAPI::new(String::from("mhodnIcgcbrmqLgErE0YmfZnC13sj7hU")); // // let body = blocking::get("https://wallhaven.cc/api/v1/search?q=anime+skirt&ratios=21x9");
// let body = blocking::get("https://wallhaven.cc/api/v1/search?q=anime+skirt&ratios=21x9");
// // println!("{}", body.unwrap().json::<HashMap<String, String>>()); // // // println!("{}", body.unwrap().json::<HashMap<String, String>>());
// let data: WallHavenResponse = // // let data: WallHavenResponse =
// serde_json::from_str(body.unwrap().text().unwrap().as_str()).unwrap(); // // serde_json::from_str(body.unwrap().text().unwrap().as_str()).unwrap();
// // for entry in data["data"] { // // // for entry in data["data"] {
// // let v: serde_jwson::Value = serde_json::from_str(entry.as_str().unwrap()).unwrap(); // // // let v: serde_jwson::Value = serde_json::from_str(entry.as_str().unwrap()).unwrap();
// // println!("{}", v); // // // println!("{}", v);
// // } // // // }
// println!("{}", data); // // println!("{}", data);
// wallhaven.add_tag(String::from("Anime")); // // wallhaven.add_tag(String::from("Anime"));
// wallhaven.add_tag(String::from("Anime3")); // // wallhaven.add_tag(String::from("Anime3"));
// wallhaven.add_tag(String::from("Anime2")); // // wallhaven.add_tag(String::from("Anime2"));
// wallhaven.add_tag(String::from("Anime4")); // // wallhaven.add_tag(String::from("Anime4"));
// wallhaven.add_tag(String::from("Anime5")); // // wallhaven.add_tag(String::from("Anime5"));
// println!("{}", wallhaven) // // println!("{}", wallhaven)
let mut search_terms = Vec::<String>::new(); // let mut search_terms = Vec::<String>::new();
// search_terms.push(String::from("sexy")); // // search_terms.push(String::from("sexy"));
search_terms.push(String::from("crab")); // search_terms.push(String::from("crab"));
let mut purities = Vec::<Purity>::new(); // let mut purities = Vec::<Purity>::new();
purities.push(Purity::Nsfw); // purities.push(Purity::Nsfw);
purities.push(Purity::Sketchy); // purities.push(Purity::Sketchy);
let mut categories = Vec::<Category>::new(); // let mut categories = Vec::<Category>::new();
categories.push(Category::Anime); // categories.push(Category::Anime);
let request = WallHavenQuery::new( // let request = WallHavenQuery::new(
wallhaven, // wallhaven,
search_terms, // search_terms,
purities, // purities,
categories, // categories,
Ratio::Horizontal, // Ratio::Horizontal,
1, // 1,
); // );
let response = request.run(); // let response = request.run();
print!("{}", response); // print!("{}", response);
} // }