1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#[derive(thiserror::Error, Debug)] pub enum WaifuPicsError { #[error("reqwest error")] ReqwestError(#[from] reqwest::Error), #[error("no picture found")] NoPicture, } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum CategorySFW { Waifu, Neko, Shinobu, Megumin, Bully, Cuddle, Cry, Hug, Awoo, Kiss, Lick, Pat, Smug, Bonk, Yeet, Blush, Smile, Wave, Highfive, Handhold, Nom, Bite, Glomp, Slap, Kill, Kick, Happy, Wink, Poke, Dance, Cringe, } impl CategorySFW { pub fn to_url_path(self) -> &'static str { match self { CategorySFW::Waifu => "sfw/waifu", CategorySFW::Neko => "sfw/neko", CategorySFW::Shinobu => "sfw/shinobu", CategorySFW::Megumin => "sfw/megumin", CategorySFW::Bully => "sfw/bully", CategorySFW::Cuddle => "sfw/cuddle", CategorySFW::Cry => "sfw/cry", CategorySFW::Hug => "sfw/hug", CategorySFW::Awoo => "sfw/awoo", CategorySFW::Kiss => "sfw/kiss", CategorySFW::Lick => "sfw/lick", CategorySFW::Pat => "sfw/pat", CategorySFW::Smug => "sfw/smug", CategorySFW::Bonk => "sfw/bonk", CategorySFW::Yeet => "sfw/smile", CategorySFW::Blush => "sfw/blush", CategorySFW::Smile => "sfw/smile", CategorySFW::Wave => "sfw/wave", CategorySFW::Highfive => "sfw/highfive", CategorySFW::Handhold => "sfw/handhold", CategorySFW::Nom => "sfw/nom", CategorySFW::Bite => "sfw/bite", CategorySFW::Glomp => "sfw/glomp", CategorySFW::Slap => "sfw/slap", CategorySFW::Kill => "sfw/kill", CategorySFW::Kick => "sfw/kick", CategorySFW::Happy => "sfw/happy", CategorySFW::Wink => "sfw/wink", CategorySFW::Poke => "sfw/poke", CategorySFW::Dance => "sfw/dance", CategorySFW::Cringe => "sfw/cringe", } } } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum CategoryNSFW { Waifu, Neko, Trap, Blowjob, } impl CategoryNSFW { pub fn to_url_path(self) -> &'static str { match self { CategoryNSFW::Waifu => "nsfw/waifu", CategoryNSFW::Neko => "nsfw/neko", CategoryNSFW::Trap => "nsfw/trap", CategoryNSFW::Blowjob => "nsfw/blowjob", } } } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Category { Sfw(CategorySFW), Nsfw(CategoryNSFW), } impl Category { fn to_url_path(self) -> &'static str { match self { Self::Sfw(c) => c.to_url_path(), Self::Nsfw(c) => c.to_url_path(), } } } impl From<CategorySFW> for Category { fn from(c: CategorySFW) -> Self { Self::Sfw(c) } } impl From<CategoryNSFW> for Category { fn from(c: CategoryNSFW) -> Self { Self::Nsfw(c) } } pub const API_URL: &str = "https://api.waifu.pics"; pub async fn get_with_client( client: &reqwest::Client, category: impl Into<Category>, many: bool, ) -> Result<Vec<String>, reqwest::Error> { let category: Category = category.into(); use serde::Deserialize; #[derive(Deserialize)] struct WaifuPicture { url: String, } #[derive(Deserialize)] struct WaifuPictures { files: Vec<String>, } if many { let req_uri = format!("{}/many/{}", API_URL, category.to_url_path()); let r = client .post(&req_uri) .body("{}") .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .await? .json::<WaifuPictures>() .await?; Ok(r.files) } else { let req_uri = format!("{}/{}", API_URL, category.to_url_path()); let r = client .get(&req_uri) .send() .await? .json::<WaifuPicture>() .await?; Ok(vec![r.url]) } } pub async fn get_single_with_client( client: &reqwest::Client, category: impl Into<Category>, ) -> Result<String, WaifuPicsError> { Ok(get_with_client(client, category, false) .await? .pop() .ok_or(WaifuPicsError::NoPicture)?) } pub async fn get(category: impl Into<Category>) -> Result<String, WaifuPicsError> { get_single_with_client(&reqwest::Client::new(), category).await } pub async fn get_many_with_client( client: &reqwest::Client, category: impl Into<Category>, ) -> Result<Vec<String>, WaifuPicsError> { Ok(get_with_client(client, category, true).await?) } pub async fn get_many(category: impl Into<Category>) -> Result<Vec<String>, WaifuPicsError> { get_many_with_client(&reqwest::Client::new(), category).await }