Added wallhaven initial support

This commit is contained in:
Benjamyn Love 2024-11-20 21:34:59 +11:00
parent 77824176f6
commit edb48a32a0
4 changed files with 1666 additions and 22 deletions

1419
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -8,4 +8,7 @@ configparser = "3.0.4"
dbus = "0.9.7" dbus = "0.9.7"
glob = "0.3.1" glob = "0.3.1"
rand = "0.8.5" rand = "0.8.5"
reqwest = { version = "0.12.9", features = ["json", "blocking"] }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
untildify = { path = "../untildify" } untildify = { path = "../untildify" }

View File

@ -5,34 +5,37 @@ mod files;
use enums::WallpaperHandler; use enums::WallpaperHandler;
use files::*; use files::*;
mod handlers; mod handlers;
mod wallhaven;
use wallhaven::*;
use untildify::untildify; use untildify::untildify;
fn main() { fn main() {
test_request();
// Load the config file from the default path // Load the config file from the default path
let path = untildify("~/.config/wallpaperctl/wallpaperctl.ini"); // let path = untildify("~/.config/wallpaperctl/wallpaperctl.ini");
let config = Config::from_config(path); // let config = Config::from_config(path);
// 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());
// Load all wallpapers based on the config file specs // // Load all wallpapers based on the config file specs
wallpapers.load_all(); // wallpapers.load_all();
// Check the configured Wallpaper engine and run the change_wallpapers() method from the respective handlers // // Check the configured Wallpaper engine and run the change_wallpapers() method from the respective handlers
let wallpaper_engine = config.wallpaper_engine; // let wallpaper_engine = config.wallpaper_engine;
match wallpaper_engine { // match wallpaper_engine {
WallpaperHandler::Plasma => { // WallpaperHandler::Plasma => {
handlers::plasma::change_wallpapers(&wallpapers); // handlers::plasma::change_wallpapers(&wallpapers);
} // }
WallpaperHandler::Feh => { // WallpaperHandler::Feh => {
handlers::feh::change_wallpapers(&wallpapers); // handlers::feh::change_wallpapers(&wallpapers);
} // }
WallpaperHandler::Gnome => { // WallpaperHandler::Gnome => {
handlers::gnome::change_wallpapers(&wallpapers); // handlers::gnome::change_wallpapers(&wallpapers);
} // }
_ => { // _ => {
println!("Error: Unknown wallpaper engine"); // println!("Error: Unknown wallpaper engine");
} // }
} // }
} }

219
src/wallhaven.rs Normal file
View File

@ -0,0 +1,219 @@
use core::fmt;
use reqwest::*;
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize)]
struct Thumbs {
large: String,
original: String,
small: String,
}
impl fmt::Display for Thumbs {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "large: {}\noriginal: {}", &self.large, &self.original)
}
}
#[derive(Serialize, Deserialize)]
struct Meta {
current_page: i32,
last_page: i32,
per_page: i32,
total: i32,
query: String,
seed: Option<String>,
}
impl fmt::Display for Meta {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(
f,
"query: {}\ncurrent_page: {}",
&self.query, &self.current_page
)
}
}
#[derive(Serialize, Deserialize)]
struct WallHavenResponse {
data: Vec<WallHavenWallpaper>,
meta: Meta,
}
impl fmt::Display for WallHavenResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
for entry in &self.data {
write! {f, "{}\n", entry}.unwrap();
}
write!(f, "{}", &self.meta).unwrap();
Ok(())
}
}
#[derive(Serialize, Deserialize)]
struct WallHavenWallpaper {
id: String,
url: String,
short_url: String,
views: i64,
favorites: i64,
source: String,
purity: String,
category: String,
dimension_x: i32,
dimension_y: i32,
resolution: String,
ratio: String,
file_size: i64,
file_type: String,
created_at: String,
colors: Vec<String>,
path: String,
thumbs: Thumbs,
}
impl fmt::Display for WallHavenWallpaper {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "ID: {}\nURL: {}", &self.id, &self.url)
}
}
pub struct Tag {
pub text: String,
}
impl Tag {
pub fn new(text: String) -> Tag {
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 api_key: String,
pub tags: Vec<Tag>,
}
impl WallHavenAPI {
pub fn new(api_key: String) -> WallHavenAPI {
WallHavenAPI {
api_key: api_key,
tags: Vec::<Tag>::new(),
}
}
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, "]")
}
}
enum Ratio {
Ultrawide,
Horizontal,
Vertical,
}
enum Purity {
Sfw,
Sketchy,
Nsfw,
}
enum Category {
General,
People,
Anime,
}
struct WallHavenQuery {
api: WallHavenAPI,
search_terms: Vec<String>,
purity: String,
category: String,
ratio: String,
page: i32,
}
impl WallHavenQuery {
pub fn new(
api: WallHavenAPI,
search_terms: Vec<String>,
purity: Purity,
category: Category,
ratio: Ratio,
page: i32,
) -> WallHavenQuery {
let mut purity_value = String::new();
match purity {
Purity::Sfw => purity_value = String::from("sfw"),
Purity::Nsfw => purity_value = String::from("nsfw"),
Purity::Sketchy => purity_value = String::from("sketchy"),
};
let mut ratio_value = String::new();
match ratio {
Ratio::Horizontal => ratio_value = String::from("16x9"),
Ratio::Ultrawide => ratio_value = String::from("21x9"),
Ratio::Vertical => ratio_value = String::from("9x16"),
};
let mut category_value = String::new();
match category {
Category::Anime => category_value = String::from("anime"),
Category::People => category_value = String::from("people"),
Category::General => category_value = String::from("general"),
};
WallHavenQuery {
api: api,
search_terms: search_terms,
purity: purity_value,
category: category_value,
ratio: ratio_value,
page: page,
}
}
pub fn run(&self) -> WallHavenResponse {
// Build the query string, URL encode it then send off the request
// Return the WallHavenResponse object
}
}
pub fn test_request() {
let mut wallhaven = WallHavenAPI::new(String::from("mhodnIcgcbrmqLgErE0YmfZnC13sj7hU"));
let body =
reqwest::blocking::get("https://wallhaven.cc/api/v1/search?q=anime+skirt&ratios=21x9");
// println!("{}", body.unwrap().json::<HashMap<String, String>>());
let data: WallHavenResponse =
serde_json::from_str(body.unwrap().text().unwrap().as_str()).unwrap();
// for entry in data["data"] {
// let v: serde_json::Value = serde_json::from_str(entry.as_str().unwrap()).unwrap();
// println!("{}", v);
// }
println!("{}", data);
wallhaven.add_tag(String::from("Anime"));
wallhaven.add_tag(String::from("Anime3"));
wallhaven.add_tag(String::from("Anime2"));
wallhaven.add_tag(String::from("Anime4"));
wallhaven.add_tag(String::from("Anime5"));
println!("{}", wallhaven)
}