diff --git a/src/config.rs b/src/config.rs index df3a3d3..dd999ce 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,6 +62,7 @@ pub struct Config { pub wallpaper_dir: WallpaperDir, pub monitors: Monitors, pub wallhaven_api_key: String, + pub wallhaven_tmp_dir: String, } 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 { x_server: config.get("general", "x_server").unwrap(), @@ -110,6 +112,7 @@ impl Config { wallpaper_dir, monitors, wallhaven_api_key, + wallhaven_tmp_dir, } } } diff --git a/src/files.rs b/src/files.rs index 3c2103b..baafe64 100644 --- a/src/files.rs +++ b/src/files.rs @@ -17,6 +17,12 @@ impl fmt::Display for Wallpaper { } } +impl Wallpaper { + pub fn new(path: String) -> Wallpaper { + Wallpaper { path: path } + } +} + #[derive(Debug, Clone)] pub struct Wallpapers { ultrawide: Vec, diff --git a/src/main.rs b/src/main.rs index 13c2530..44da0c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ mod config; +use std::fmt::format; + use config::*; mod enums; mod files; @@ -19,11 +21,11 @@ fn main() { // Initialise the `Wallpapers` struct with a clone of the config 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 purities = vec![Purity::Sfw]; - let categories = vec![Category::Anime, Category::General]; + let search_terms = vec![String::from("tsundere")]; + let purities = vec![Purity::Sketchy]; + let categories = vec![Category::Anime]; let query = WallHavenQuery::new( api, @@ -36,6 +38,8 @@ fn main() { let results = query.run(); + let wall = results.get_random(); + println!("{}", results); // // Load all wallpapers based on the config file specs // wallpapers.load_all(); diff --git a/src/wallhaven.rs b/src/wallhaven.rs index 2d70b9c..f376b95 100644 --- a/src/wallhaven.rs +++ b/src/wallhaven.rs @@ -3,8 +3,12 @@ use dbus::arg::RefArg; use reqwest::*; use serde::Deserialize; use serde::Serialize; +use std::fs::File; +use std::io; use urlencoding::encode; +use crate::{Wallpaper, Wallpapers}; + #[derive(Serialize, Deserialize)] struct Thumbs { large: String, @@ -35,8 +39,8 @@ impl fmt::Display for Meta { #[derive(Serialize, Deserialize)] pub struct WallHavenResponse { - data: Vec, - meta: Meta, + pub data: Vec, + pub meta: Meta, } 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)] -struct WallHavenWallpaper { +pub struct WallHavenWallpaper { id: String, url: String, short_url: String, @@ -73,53 +83,40 @@ struct WallHavenWallpaper { impl fmt::Display for WallHavenWallpaper { 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 { - pub text: String, -} +impl WallHavenWallpaper { + pub fn download(self, path: String) -> bool { + let image_resp = blocking::get(self.path).unwrap(); -impl Tag { - pub fn new(text: String) -> Tag { - Tag { text: text } + let mut outfile = File::create(path).unwrap(); + io::copy(&mut image_resp.text().unwrap().as_bytes(), &mut outfile).unwrap(); + true } -} -impl fmt::Display for Tag { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", &self.text) + pub fn to_wallpaper(&self) -> Wallpaper { + Wallpaper::new(String::new()) } } pub struct WallHavenAPI { pub api_key: String, - pub tags: Vec, + pub tmp_dir: String, } impl WallHavenAPI { - pub fn new(api_key: String) -> WallHavenAPI { + pub fn new(api_key: String, tmp_dir: String) -> WallHavenAPI { WallHavenAPI { api_key: api_key, - tags: Vec::::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 { @@ -232,48 +229,47 @@ impl WallHavenQuery { } } -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"); +// pub fn test_request() { +// // let body = blocking::get("https://wallhaven.cc/api/v1/search?q=anime+skirt&ratios=21x9"); - // // println!("{}", body.unwrap().json::>()); - // let data: WallHavenResponse = - // serde_json::from_str(body.unwrap().text().unwrap().as_str()).unwrap(); +// // // println!("{}", body.unwrap().json::>()); +// // let data: WallHavenResponse = +// // serde_json::from_str(body.unwrap().text().unwrap().as_str()).unwrap(); - // // for entry in data["data"] { - // // let v: serde_jwson::Value = serde_json::from_str(entry.as_str().unwrap()).unwrap(); - // // println!("{}", v); - // // } +// // // for entry in data["data"] { +// // // let v: serde_jwson::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) +// // 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) - let mut search_terms = Vec::::new(); - // search_terms.push(String::from("sexy")); - search_terms.push(String::from("crab")); +// let mut search_terms = Vec::::new(); +// // search_terms.push(String::from("sexy")); +// search_terms.push(String::from("crab")); - let mut purities = Vec::::new(); - purities.push(Purity::Nsfw); - purities.push(Purity::Sketchy); +// let mut purities = Vec::::new(); +// purities.push(Purity::Nsfw); +// purities.push(Purity::Sketchy); - let mut categories = Vec::::new(); - categories.push(Category::Anime); +// let mut categories = Vec::::new(); +// categories.push(Category::Anime); - let request = WallHavenQuery::new( - wallhaven, - search_terms, - purities, - categories, - Ratio::Horizontal, - 1, - ); +// let request = WallHavenQuery::new( +// wallhaven, +// search_terms, +// purities, +// categories, +// Ratio::Horizontal, +// 1, +// ); - let response = request.run(); +// let response = request.run(); - print!("{}", response); -} +// print!("{}", response); +// }