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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// =======================Imports===============================

use bytes::Bytes;
use reqwest;
use reqwest::{header::AUTHORIZATION, Client, Error};
use serde::Deserialize;
use std::{borrow::Cow, collections::HashMap};

// ======================API response structs====================

#[derive(Deserialize, Debug)]
pub struct IndexAPIResponse {
    pub support_server: String,
    pub endpoints: Vec<String>,
    pub wrappers: HashMap<String, WrapperInfo>,
}

#[derive(Deserialize, Debug)]
pub struct WrapperInfo {
    pub author: String,
    pub url: String,
}

#[derive(Deserialize, Debug)]
pub struct ColorAPIResponse {
    pub blackorwhite_text: String,
    pub brightness: i32,
    pub hex: String,
    pub image: String,
    pub image_gradient: String,
    pub int: i64,
    pub name: String,
    pub rgb: String,
    pub rgb_values: HashMap<String, i32>,
    pub shade: Vec<String>,
    pub tint: Vec<String>,
}

// Add the `.as_cow` method to the Bytes struct.

pub trait CowValue {
    fn as_cow(&self) -> Cow<[u8]>;
}

impl CowValue for Bytes {
    fn as_cow(&self) -> Cow<[u8]> {
        Cow::from((&*self) as &[u8])
    }
}

// =====================Enums for image endpoints.=======================================
#[derive(Debug)]
pub enum MinecraftIcons {
    NONE = 0,
    GRASS_BLOCK = 1,
    DIAMOND = 2,
    DIAMOND_SWORD = 3,
    CREEPER = 4,
    PIG = 5,
    TNT = 6,
    COOKIE = 7,
    HEART = 8,
    BED = 9,
    CAKE = 10,
    SIGN = 11,
    RAIL = 12,
    CRAFTING_BENCH = 13,
    REDSTONE = 14,
    FIRE = 15,
    COBWEB = 16,
    CHEST = 17,
    FURNACE = 18,
    BOOK = 19,
    STONE_BLOCK = 20,
    WOODEN_PLANK_BLOCK = 21,
    IRON_INGOT = 22,
    GOLD_INGOT = 23,
    WOODEN_DOOR = 24,
    IRON_DOOR = 25,
    DIAMOND_CHESTPLATE = 26,
    FLINT_AND_STEEL = 27,
    GLASS_BOTTLE = 28,
    SPLASH_POTION = 29,
    CREEPER_SPAWNEGG = 30,
    COAL = 31,
    IRON_SWORD = 32,
    BOW = 33,
    ARROW = 34,
    IRON_CHESTPLATE = 35,
    BUCKET = 36,
    BUCKET_WITH_WATER = 37,
    BUCKET_WITH_LAVA = 38,
    BUCKET_WITH_MILK = 39,
    DIAMOND_BOOTS = 40,
    WOODEN_HOE = 41,
    BREAD = 42,
    WOODEN_SWORD = 43,
    BONE = 44,
    OAK_LOG = 45,
}

// ========================== UTILITIES =========================================
pub fn parse_filter(text: &mut str) -> &str {
    let uppercase = text.to_ascii_uppercase();
    let data = match &*uppercase {
        "BLUR" => "blur",
        "INVERT" => "invert",
        "BLACK_AND_WHITE" => "b&w",
        "DEEPFRY" => "deepfry",
        "SEPIA" => "sepia",
        "PIXELATE" => "pixelate",
        "MAGIK" => "magik",
        "JPEGIFY" => "jpegify",
        "WIDE" => "wide",
        "SNOW" => "snow",
        "GAY" => "gay",
        "COMMUNIST" => "communist",
        _ => "magik",
    };
    data
}

pub fn parse_icon(text: &mut str) -> MinecraftIcons {
    let uppercase = text.to_ascii_uppercase();
    let data = match &*uppercase {
        "GRASS_BLOCK" => MinecraftIcons::GRASS_BLOCK,
        "DIAMOND" => MinecraftIcons::DIAMOND,
        "DIAMOND_SWORD" => MinecraftIcons::DIAMOND_SWORD,
        "CREEPER" => MinecraftIcons::CREEPER,
        "PIG" => MinecraftIcons::PIG,
        "TNT" => MinecraftIcons::TNT,
        "COOKIE" => MinecraftIcons::COOKIE,
        "HEART" => MinecraftIcons::HEART,
        "BED" => MinecraftIcons::BED,
        "CAKE" => MinecraftIcons::CAKE,
        "SIGN" => MinecraftIcons::SIGN,
        "RAIL" => MinecraftIcons::RAIL,
        "CRAFTING_BENCH" => MinecraftIcons::CRAFTING_BENCH,
        "REDSTONE" => MinecraftIcons::REDSTONE,
        "FIRE" => MinecraftIcons::FIRE,
        "COBWEB" => MinecraftIcons::COBWEB,
        "CHEST" => MinecraftIcons::CHEST,
        "FURNACE" => MinecraftIcons::FURNACE,
        "BOOK" => MinecraftIcons::BOOK,
        "STONE_BLOCK" => MinecraftIcons::STONE_BLOCK,
        "WOODEN_PLANK_BLOCK" => MinecraftIcons::WOODEN_PLANK_BLOCK,
        "IRON_INGOT" => MinecraftIcons::IRON_INGOT,
        "GOLD_INGOT" => MinecraftIcons::GOLD_INGOT,
        "WOODEN_DOOR" => MinecraftIcons::WOODEN_DOOR,
        "IRON_DOOR" => MinecraftIcons::IRON_DOOR,
        "DIAMOND_CHESTPLATE" => MinecraftIcons::DIAMOND_CHESTPLATE,
        "FLINT_AND_STEEL" => MinecraftIcons::FLINT_AND_STEEL,
        "GLASS_BOTTLE" => MinecraftIcons::GLASS_BOTTLE,
        "SPLASH_POTION" => MinecraftIcons::SPLASH_POTION,
        "CREEPER_SPAWNEGG" => MinecraftIcons::CREEPER_SPAWNEGG,
        "COAL" => MinecraftIcons::COAL,
        "IRON_SWORD" => MinecraftIcons::IRON_SWORD,
        "BOW" => MinecraftIcons::BOW,
        "ARROW" => MinecraftIcons::ARROW,
        "IRON_CHESTPLATE" => MinecraftIcons::IRON_CHESTPLATE,
        "BUCKET" => MinecraftIcons::BUCKET,
        "BUCKET_WTH_WATER" => MinecraftIcons::BUCKET_WITH_WATER,
        "BUCKET_WITH_LAVA" => MinecraftIcons::BUCKET_WITH_LAVA,
        "BUCKER_WITH_MILK" => MinecraftIcons::BUCKET_WITH_MILK,
        "DIAMOND_BOOTS" => MinecraftIcons::DIAMOND_BOOTS,
        "WOODEN_HOE" => MinecraftIcons::WOODEN_HOE,
        "BREAD" => MinecraftIcons::BREAD,
        "WOODEN_SWORD" => MinecraftIcons::WOODEN_SWORD,
        "BONE" => MinecraftIcons::BONE,
        "OAK_LOG" => MinecraftIcons::OAK_LOG,
        _ => MinecraftIcons::NONE,
    };
    data
}
//================================ AlexClient struct=================================

pub struct AlexClient {
    key: &'static str,
    client: Client,
}

impl AlexClient {
    // Make a new instance of the client
    pub fn new(key: &'static str) -> AlexClient {
        let client = Client::new();
        AlexClient { key, client }
    }

    // Returns the index page JSON.
    pub fn index(&self) -> IndexAPIResponse {
        let data = reqwest::blocking::get("https://api.alexflipnote.dev/")
            .expect("Could not fetch data")
            .json::<IndexAPIResponse>()
            .expect("Could not parse JSON");
        data
    }

    // Color Endpoint
    pub async fn color(&self, hex: &str) -> Result<ColorAPIResponse, Error> {
        let mut url = String::from("https://api.alexflipnote.dev/color/");
        url.push_str(&hex);
        let req = self
            .client
            .get(&url)
            .header(AUTHORIZATION, self.key)
            .send()
            .await?;
        let data = req
            .json::<ColorAPIResponse>()
            .await
            .expect("Could not parse JSON");
        Ok(data)
    }

    // ============================IMAGE ENDPOINTS=================================
    async fn fetch_image(&self, endpoint: &str, parameters: &str) -> Result<Bytes, Error> {
        let mut base = String::from("https://api.alexflipnote.dev/");
        base.push_str(&endpoint);
        base.push_str(&parameters);
        let req = self
            .client
            .get(&base)
            .header(AUTHORIZATION, self.key)
            .send()
            .await?;
        let data = req.bytes().await.expect("Could not parse Bytes");
        Ok(data)
    }

    pub async fn drake(&self, top: &str, bottom: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("drake", &*format!("?top={0}&bottom={1}", &top, &bottom))
            .await;
        data
    }

    pub async fn achievement(&self, text: &str, icon: &i32) -> Result<Bytes, Error> {
        let icon_parsed = match &*icon {
            0 => String::from(""),
            _ => format!("&icon={}", icon),
        };
        let data = self
            .fetch_image("achievement", &*format!("?text={0}{1}", text, icon_parsed))
            .await;
        data
    }

    pub async fn amiajoke(&self, url: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("amiajoke", &*format!("?image={0}", url))
            .await;
        data
    }

    pub async fn bad(&self, url: &str) -> Result<Bytes, Error> {
        let data = self.fetch_image("bad", &*format!("?image={0}", url)).await;
        data
    }

    pub async fn birb(&self) -> Result<Bytes, Error> {
        let data = self.fetch_image("birb", "").await;
        data
    }

    pub async fn calling(&self, text: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("calling", &format!("?text={0}", text))
            .await;
        data
    }

    pub async fn captcha(&self, text: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("captcha", &format!("?text={0}", text))
            .await;
        data
    }

    pub async fn cats(&self) -> Result<Bytes, Error> {
        let data = self.fetch_image("cats", "").await;
        data
    }

    pub async fn colorify(&self, url: &str, color: &str, background: &str) -> Result<Bytes, Error> {
        let color_parsed = match &*color.to_ascii_lowercase() {
            "none" => "".to_string(),
            "null" => "".to_string(),
            "0" => "".to_string(),
            "false" => "".to_string(),
            _ => format!("&c={}", color),
        };
        let background_parsed = match &*background.to_ascii_lowercase() {
            "none" => "".to_string(),
            "null" => "".to_string(),
            "0" => "".to_string(),
            "false" => "".to_string(),
            _ => format!("&b={}", background),
        };

        let data = self
            .fetch_image(
                "colourify",
                &format!("?image={0}{1}{2}", url, color_parsed, background_parsed),
            )
            .await;
        data
    }

    pub async fn didyoumean(&self, top: &str, bottom: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image(
                "didyoumean",
                &*format!("?top={0}&bottom={1}", &top, &bottom),
            )
            .await;
        data
    }

    pub async fn dogs(&self) -> Result<Bytes, Error> {
        let data = self.fetch_image("dogs", "").await;
        data
    }

    pub async fn facts(&self, text: &str) -> Result<Bytes, Error> {
        let data = self.fetch_image("facts", &format!("?text={0}", text)).await;
        data
    }

    pub async fn filter(&self, name: &str, text: &str) -> Result<Bytes, Error> {
        let name_parsed = name.to_string();
        let data = self
            .fetch_image("filter", &*format!("/{0}?image={1}", name_parsed, text))
            .await;
        data
    }

    pub async fn floor(&self, img: &str, text: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("floor", &format!("?image={0}&text={1}", &img, &text))
            .await;
        data
    }

    pub async fn fml(&self) -> Result<Bytes, Error> {
        let data = self.fetch_image("fml", "").await;
        data
    }

    pub async fn jokeoverhead(&self, url: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("jokeoverhead", &*format!("?image={0}", url))
            .await;
        data
    }

    pub async fn pornhub(&self, text: &str, text2: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("pornhub", &*format!("?text={0}&text2={1}", &text, &text2))
            .await;
        data
    }

    pub async fn sadcat(&self) -> Result<Bytes, Error> {
        let data = self.fetch_image("sadcat", "").await;
        data
    }

    pub async fn salty(&self, url: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("salty", &*format!("?image={0}", url))
            .await;
        data
    }

    pub async fn scroll(&self, text: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("scroll", &*format!("?text={0}", text))
            .await;
        data
    }

    pub async fn shame(&self, url: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("shame", &*format!("?image={0}", url))
            .await;
        data
    }

    pub async fn ship(&self, user: &str, user2: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("ship", &*format!("?user={0}&user2={1}", &user, &user2))
            .await;
        data
    }

    pub async fn supreme(&self, text: &str, mode: &str) -> Result<Bytes, Error> {
        let mode_parsed = match &*mode.to_ascii_lowercase() {
            "light" => "&light=true",
            "dark" => "&dark=true",
            _ => "",
        };
        let data = self
            .fetch_image("supreme", &*format!("?text={0}{1}", &text, &mode_parsed))
            .await;
        data
    }

    pub async fn trash(&self, face: &str, trash: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("trash", &*format!("?face={0}&trash={1}", &face, &trash))
            .await;
        data
    }

    pub async fn what(&self, image: &str) -> Result<Bytes, Error> {
        let data = self
            .fetch_image("what", &*format!("?image={0}", &image))
            .await;
        data
    }
}