More wallhaven features

Updated ABI slightly
This commit is contained in:
Benjamyn Love 2024-11-22 14:08:07 +11:00
parent edb48a32a0
commit c1a3083fab
5 changed files with 140 additions and 51 deletions

7
Cargo.lock generated
View File

@ -661,6 +661,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"untildify", "untildify",
"urlencoding",
] ]
[[package]] [[package]]
@ -1279,6 +1280,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]] [[package]]
name = "utf16_iter" name = "utf16_iter"
version = "1.0.5" version = "1.0.5"

View File

@ -12,3 +12,4 @@ reqwest = { version = "0.12.9", features = ["json", "blocking"] }
serde = { version = "1.0.215", features = ["derive"] } serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133" serde_json = "1.0.133"
untildify = { path = "../untildify" } untildify = { path = "../untildify" }
urlencoding = "2.1.3"

View File

@ -61,6 +61,7 @@ pub struct Config {
pub wallpaper_engine: WallpaperHandler, pub wallpaper_engine: WallpaperHandler,
pub wallpaper_dir: WallpaperDir, pub wallpaper_dir: WallpaperDir,
pub monitors: Monitors, pub monitors: Monitors,
pub wallhaven_api_key: String,
} }
impl Config { impl Config {
@ -100,13 +101,15 @@ impl Config {
monitors.add(MonitorType::new(monitor.to_string())); monitors.add(MonitorType::new(monitor.to_string()));
} }
} }
//
let wallhaven_api_key = config.get("general", "wallhaven_api_key").unwrap();
Config { Config {
x_server: config.get("general", "x_server").unwrap(), x_server: config.get("general", "x_server").unwrap(),
wallpaper_engine, wallpaper_engine,
wallpaper_dir, wallpaper_dir,
monitors, monitors,
wallhaven_api_key,
} }
} }
} }

View File

@ -11,14 +11,32 @@ use wallhaven::*;
use untildify::untildify; use untildify::untildify;
fn main() { fn main() {
test_request(); // 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());
let api = WallHavenAPI::new(config.wallhaven_api_key);
let search_terms = vec![String::from("godzilla")];
let purities = vec![Purity::Sfw];
let categories = vec![Category::Anime, Category::General];
let query = WallHavenQuery::new(
api,
search_terms,
purities,
categories,
Ratio::Horizontal,
1,
);
let results = query.run();
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

@ -1,7 +1,9 @@
use core::fmt; use core::fmt;
use dbus::arg::RefArg;
use reqwest::*; use reqwest::*;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use urlencoding::encode;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Thumbs { struct Thumbs {
@ -20,23 +22,19 @@ impl fmt::Display for Thumbs {
struct Meta { struct Meta {
current_page: i32, current_page: i32,
last_page: i32, last_page: i32,
per_page: i32, per_page: String,
total: i32, total: i32,
query: String, query: Option<String>,
seed: Option<String>, seed: Option<String>,
} }
impl fmt::Display for Meta { impl fmt::Display for Meta {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!( write!(f, "current_page: {}", &self.current_page)
f,
"query: {}\ncurrent_page: {}",
&self.query, &self.current_page
)
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct WallHavenResponse { pub struct WallHavenResponse {
data: Vec<WallHavenWallpaper>, data: Vec<WallHavenWallpaper>,
meta: Meta, meta: Meta,
} }
@ -75,7 +73,7 @@ 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.url) write!(f, "ID: {}\nURL: {}", &self.id, &self.path)
} }
} }
@ -124,25 +122,25 @@ impl fmt::Display for WallHavenAPI {
} }
} }
enum Ratio { pub enum Ratio {
Ultrawide, Ultrawide,
Horizontal, Horizontal,
Vertical, Vertical,
} }
enum Purity { pub enum Purity {
Sfw, Sfw,
Sketchy, Sketchy,
Nsfw, Nsfw,
} }
enum Category { pub enum Category {
General, General,
People, People,
Anime, Anime,
} }
struct WallHavenQuery { pub struct WallHavenQuery {
api: WallHavenAPI, api: WallHavenAPI,
search_terms: Vec<String>, search_terms: Vec<String>,
purity: String, purity: String,
@ -155,29 +153,47 @@ impl WallHavenQuery {
pub fn new( pub fn new(
api: WallHavenAPI, api: WallHavenAPI,
search_terms: Vec<String>, search_terms: Vec<String>,
purity: Purity, purity: Vec<Purity>,
category: Category, category: Vec<Category>,
ratio: Ratio, ratio: Ratio,
page: i32, page: i32,
) -> WallHavenQuery { ) -> WallHavenQuery {
let mut purity_value = String::new(); let mut purity_value = String::from("000");
match purity { for p in purity {
Purity::Sfw => purity_value = String::from("sfw"), match p {
Purity::Nsfw => purity_value = String::from("nsfw"), Purity::Nsfw => {
Purity::Sketchy => purity_value = String::from("sketchy"), purity_value.replace_range(2..3, "1");
}; }
let mut ratio_value = String::new(); Purity::Sfw => {
purity_value.replace_range(0..1, "1");
}
Purity::Sketchy => {
purity_value.replace_range(1..2, "1");
}
}
}
let ratio_value: String;
match ratio { match ratio {
Ratio::Horizontal => ratio_value = String::from("16x9"), Ratio::Horizontal => ratio_value = String::from("16x9"),
Ratio::Ultrawide => ratio_value = String::from("21x9"), Ratio::Ultrawide => ratio_value = String::from("21x9"),
Ratio::Vertical => ratio_value = String::from("9x16"), Ratio::Vertical => ratio_value = String::from("9x16"),
}; };
let mut category_value = String::new(); let mut category_value = String::from("000");
match category { for c in category {
Category::Anime => category_value = String::from("anime"), match c {
Category::People => category_value = String::from("people"), Category::Anime => {
Category::General => category_value = String::from("general"), category_value.replace_range(1..2, "1");
}; }
Category::General => {
category_value.replace_range(0..1, "1");
}
Category::People => {
category_value.replace_range(2..3, "1");
}
}
}
WallHavenQuery { WallHavenQuery {
api: api, api: api,
search_terms: search_terms, search_terms: search_terms,
@ -190,30 +206,74 @@ impl WallHavenQuery {
pub fn run(&self) -> WallHavenResponse { pub fn run(&self) -> WallHavenResponse {
// Build the query string, URL encode it then send off the request // Build the query string, URL encode it then send off the request
let mut query_string = String::from("?q=");
let mut url = String::from("https://wallhaven.cc/api/v1/search");
let mut search_terms = String::new();
for term in &self.search_terms {
search_terms.push_str(term.as_str());
search_terms.push_str("+");
}
query_string.push_str(search_terms.as_str());
query_string.push_str("&ratios=");
query_string.push_str(self.ratio.as_str());
query_string.push_str("&purity=");
query_string.push_str(self.purity.as_str());
query_string.push_str("&categories=");
query_string.push_str(self.category.as_str());
query_string.push_str("&apikey=");
query_string.push_str(self.api.api_key.as_str());
url.push_str(query_string.as_str());
// let encoded = encode(url.as_str()).into_owned();
println!("{}", &url);
serde_json::from_str(blocking::get(url).unwrap().text().unwrap().as_str()).unwrap()
// Return the WallHavenResponse object // Return the WallHavenResponse object
} }
} }
pub fn test_request() { pub fn test_request() {
let mut wallhaven = WallHavenAPI::new(String::from("mhodnIcgcbrmqLgErE0YmfZnC13sj7hU")); let wallhaven = WallHavenAPI::new(String::from("mhodnIcgcbrmqLgErE0YmfZnC13sj7hU"));
let body = // let body = blocking::get("https://wallhaven.cc/api/v1/search?q=anime+skirt&ratios=21x9");
reqwest::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_json::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();
// search_terms.push(String::from("sexy"));
search_terms.push(String::from("crab"));
let mut purities = Vec::<Purity>::new();
purities.push(Purity::Nsfw);
purities.push(Purity::Sketchy);
let mut categories = Vec::<Category>::new();
categories.push(Category::Anime);
let request = WallHavenQuery::new(
wallhaven,
search_terms,
purities,
categories,
Ratio::Horizontal,
1,
);
let response = request.run();
print!("{}", response);
} }