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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
use forecast::{
ApiClient as DSClient,
ApiResponse,
ForecastRequestBuilder,
Lang,
Units
};
use geocoding::Opencage;
use kitsu::model::{
Response,
Anime,Manga
};
use kitsu::{
KitsuReqwestRequester,
Error as KitsuErr
};
use reqwest::header::{
Headers,
UserAgent,
ContentType,
Accept,
Authorization,
qitem
};
use reqwest::mime;
use reqwest::{
Client,
Result as ReqwestResult,
Response as ReqwestResponse
};
use std::collections::HashMap;
use std::env;
use urbandictionary::{
ReqwestUrbanDictionaryRequester,
Result as UrbanResult,
model::Response as UrbanResponse
};
const UA: &str = "wisp-bot";
// Endpoints
const DOG: &str = "https://dog.ceo/api/breeds/image/random";
const CAT: &str = "http://aws.random.cat/meow";
const DAD_JOKE: &str = "https://icanhazdadjoke.com";
const FURRY: &str = "https://e621.net/post/index.json";
const DBOTS: &str = "http://discordbots.org/api/bots";
// const BUNNY: &str = "https://api.bunnies.io/v2/loop/random/?media=gif,png";
const DUCK: &str = "https://random-d.uk/api/v1/random?type=gif";
const FOX: &str = "https://randomfox.ca/floof/";
const OWL: &str = "https://pics.floofybot.moe/owl";
const YO_MOMMA: &str = "https://api.yomomma.info/";
// const WAIFU_BACKSTORY: &str = "https://www.thiswaifudoesnotexist.net/snippet-"; // .txt
// const WAIFU_IMAGE: &str = "https://www.thiswaifudoesnotexist.net/example-"; // .jpg
// Deserialization Structs
// #[derive(Deserialize, Debug)]
// pub struct Bunny {
// pub id: String,
// pub media: Vec<String>,
// pub source: String,
// pub thisServed: u32,
// pub totalServed: u32
// }
#[derive(Deserialize, Debug)]
pub struct Dog {
pub message: String,
pub status: String
}
#[derive(Deserialize, Debug)]
pub struct Duck {
pub message: String,
pub url: String
}
#[derive(Deserialize, Debug)]
pub struct Cat {
pub file: String
}
#[derive(Deserialize, Debug)]
pub struct CreatedAt {
pub json_class: String,
pub s: usize,
pub n: usize
}
#[derive(Deserialize, Debug)]
pub struct Fox {
pub image: String,
pub link: String
}
#[derive(Deserialize, Debug)]
pub struct FurPost {
pub id: usize,
pub tags: String,
pub locked_tags: Option<String>,
pub description: String,
pub created_at: CreatedAt,
pub creator_id: usize,
pub author: String,
pub change: usize,
pub source: Option<String>,
pub score: isize,
pub fav_count: usize,
pub md5: Option<String>,
pub file_size: Option<usize>,
pub file_url: String,
pub file_ext: Option<String>,
pub preview_url: String,
pub preview_width: Option<usize>,
pub preview_height: Option<usize>,
pub sample_url: Option<String>,
pub sample_width: Option<usize>,
pub sample_height: Option<usize>,
pub rating: String,
pub status: String,
pub width: usize,
pub height: usize,
pub has_comments: bool,
pub has_notes: bool,
pub has_children: Option<bool>,
pub children: Option<String>,
pub parent_id: Option<usize>,
pub artist: Vec<String>,
pub sources: Option<Vec<String>>
}
#[derive(Deserialize, Debug)]
pub struct Owl {
pub image: String
}
#[derive(Deserialize, Debug)]
pub struct YoMomma {
pub joke: String
}
// The Client
pub struct ApiClient {
pub client: Client,
pub oc_client: Opencage,
}
impl ApiClient {
pub fn new() -> Self {
let client = Client::new();
let oc_key = env::var("OPENCAGE_KEY").expect("Expected key for OpenCage in environment.");
let oc_client = Opencage::new(oc_key);
ApiClient {
client,
oc_client
}
}
pub fn stats_update(&self, bot_id: u64, server_count: usize) -> ReqwestResult<ReqwestResponse> {
let mut headers = Headers::new();
headers.set(ContentType::json());
headers.set(Authorization(env::var("DBOTS_ORG_TOKEN").expect("Expected DiscordBots.org token in environment.")));
let mut data = HashMap::new();
data.insert("server_count", server_count);
self.client.post(format!("{}/{}/stats", DBOTS, bot_id).as_str())
.json(&data)
.send()
}
// pub fn bunny(&self) -> ReqwestResult<Bunny> {
// match self.client.get(BUNNY).send() {
// Ok(mut res) => {
// res.json::<Bunny>()
// },
// Err(why) => {
// error!("{:?}", why);
// Err(why)
// },
// }
// }
pub fn dog(&self) -> ReqwestResult<Dog> {
match self.client.get(DOG).send() {
Ok(mut res) => {
res.json::<Dog>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn duck(&self) -> ReqwestResult<Duck> {
match self.client.get(DUCK).send() {
Ok(mut res) => {
res.json::<Duck>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn cat(&self) -> ReqwestResult<Cat> {
match self.client.get(CAT).send() {
Ok(mut res) => {
res.json::<Cat>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn joke(&self) -> ReqwestResult<String> {
let mut headers = Headers::new();
headers.set(UserAgent::new(UA));
headers.set(Accept(vec![qitem(mime::TEXT_PLAIN)]));
match self.client.get(DAD_JOKE)
.headers(headers)
.send() {
Ok(mut res) => {
res.text()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn urban(&self, input: &str) -> UrbanResult<UrbanResponse> {
self.client.definitions(input)
}
pub fn furry<S: Into<String>>(&self, input: S, count: u32) -> ReqwestResult<Vec<FurPost>> {
let mut headers = Headers::new();
headers.set(UserAgent::new(UA));
match self.client.get(FURRY)
.headers(headers)
.query(&[("tags", input.into()+" order:random"), ("limit", count.to_string())])
.send() {
Ok(mut res) => {
res.json::<Vec<FurPost>>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn fox(&self) -> ReqwestResult<Fox> {
match self.client.get(FOX).send() {
Ok(mut res) => {
res.json::<Fox>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn owl(&self) -> ReqwestResult<Owl> {
match self.client.get(OWL).send() {
Ok(mut res) => {
res.json::<Owl>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn yo_momma(&self) -> ReqwestResult<YoMomma> {
match self.client.get(YO_MOMMA).send() {
Ok(mut res) => {
res.json::<YoMomma>()
},
Err(why) => {
error!("{:?}", why);
Err(why)
},
}
}
pub fn anime<S: Into<String>>(&self, input: S) -> Result<Response<Vec<Anime>>, KitsuErr> {
self.client.search_anime(|f| f.filter("text", input.into().trim()))
}
pub fn manga<S: Into<String>>(&self, input: S) -> Result<Response<Vec<Manga>>, KitsuErr> {
self.client.search_manga(|f| f.filter("text", input.into().trim()))
}
pub fn weather(&self, input: &str, units: Units) -> Option<(String, ReqwestResult<ApiResponse>)> {
match self.oc_client.forward_full(input, &None) {
Ok(data) => {
if !data.results.is_empty() {
let first = data.results.first().unwrap();
let city_info = format!("{}, {}, {}",
first.components.get("city").unwrap(),
first.components.get("state").unwrap(),
first.components.get("country").unwrap()
);
let ds_key = env::var("DARKSKY_KEY").expect("Expected DarkSky API Key in environment.");
let fc_req = Some(ForecastRequestBuilder::new(ds_key.as_str(), *first.geometry.get("lat").unwrap(), *first.geometry.get("lng").unwrap())
.lang(Lang::English)
.units(units)
.build());
if let Some(req) = fc_req {
let ds_client = DSClient::new(&self.client);
match ds_client.get_forecast(req) {
Ok(mut res) => {
return Some((city_info, res.json::<ApiResponse>()));
},
Err(why) => { return Some((city_info, Err(why))); },
}
}
}
},
Err(why) => { debug!("Failed to resolve location: {:?}", why); }
}
None
}
}
|