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
use reqwest::{Client as HttpClient};
use std::sync::Arc;
use serde::Deserialize;
use crate::{
    make_request,
    endpoint,
    model::*,
    HttpResult
};
use std::fmt::{Display, Formatter, Result as FmtResult};

pub struct Images {
    pub http: Arc<HttpClient>
}

impl Images {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        Self {
            http: http_client
        }
    }

    ///Gets a random image based in a given tag
    ///
    /// # Example
    /// ```rust,ignore
    /// if let Ok(res) = client.images.random_image("doge", false).await {
    ///     match res {
    ///         Ok(image) => {
    ///             //do something with the image
    ///         },
    ///         Err(why) => {
    ///             //do something with the <ImageError> struct
    ///         }
    ///     }
    /// }
    /// ```
    pub async fn random_image(&self, tag: impl ToString, nsfw: bool) -> HttpResult<Image, ImageError>{
        if tag.to_string().is_empty() { panic!("Tag param cannot be empty") }

        let builder = self.http.clone().get(endpoint("/images/random-image").as_str())
            .query(&[("tag", tag.to_string())])
            .query(&[("nsfw", nsfw)]);


        make_request::<Image, ImageError>(builder).await
    }

    ///Gets a random meme from reddit
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(meme) = client.images.random_meme().await {
    ///     //do something here
    /// }
    /// ```
    pub async fn random_meme(&self) -> reqwest::Result<RedditImage>{
        let response = self.http.clone().get(endpoint("/images/random-meme").as_str())
            .send()
            .await?;

        let image = response.json::<RedditImage>().await?;
        Ok(image)
    }

    ///Gets a random cute image
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(aww) = client.images.random_aww().await {
    ///     //do something with the image
    /// }
    /// ```
    pub async fn random_aww(&self) -> reqwest::Result<RedditImage>{
        return self.http.clone().get(endpoint("/images/random-aww").as_str())
            .send()
            .await?
            .json::<RedditImage>()
            .await
    }

    ///Gets a random post from a given subreddit
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(res) = client.images.random_reddit("Technology", true, SpanType::Day).await {
    ///     match res {
    ///         Ok(red) => {
    ///             //do something with the reddit image
    ///         },
    ///         Err(why) => {
    ///             //do something with the <ImageError> struct
    ///         }
    ///     }
    /// }
    /// ```
    pub async fn random_reddit(&self, subreddit: impl ToString, remove_nsfw: bool, span: SpanType) -> HttpResult<RedditImage, ImageError>{
        if subreddit.to_string().is_empty() { panic!("You have to specify a subreddit to search in") }

        let builder = self.http.clone().get(endpoint(format!("/images/rand-reddit/{}", subreddit.to_string())).as_str())
            .query(&[("remove_nsfw", remove_nsfw)])
            .query(&[("span", span.to_string())]);

        make_request::<RedditImage, ImageError>(builder).await
    }

    ///Gets a random WikiHow image
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(wiki_image) = client.images.random_wikihow(false).await {
    ///     //do something with the image
    /// }
    /// ```
    pub async fn random_wikihow(&self, nsfw: bool) -> reqwest::Result<WikiHowImage> {
        return self.http.clone().get(endpoint("/images/random-wikihow").as_str())
            .query(&[("nsfw", nsfw)])
            .send()
            .await?
            .json::<WikiHowImage>()
            .await
    }

    ///Gets a list of all tags available
    ///
    /// # Example
    ///
    /// if let Ok(tags) = client.images.get_tags().await {
    ///     //do something with all tags
    /// }
    /// ```
    pub async fn get_tags(&self) -> reqwest::Result<TagList> {
        return self.http.clone().get(endpoint("/images/tags").as_str())
            .send()
            .await?
            .json::<TagList>()
            .await
    }

    ///Gets an image using its Snowflake
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(res) = client.images.get_image("i-8ta8p52f-27").await {
    //     match res {
    //         Ok(img) => {
    //             //do something with the image
    //         },
    //         Err(why) => {
    //             //do something with the <ImageError> struct
    //         }
    //     }
    // }
    /// ```
    pub async fn get_image(&self, sf: impl AsRef<str>) -> HttpResult<Image, ImageError> {
        if sf.as_ref().is_empty() { panic!("Snowflake id cannot be empty") }

        let builder = self.http.clone().get(endpoint(format!("/images/image/{}", sf.as_ref())).as_str());

        make_request::<Image, ImageError>(builder).await
    }

    ///Get a tag using its name
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(tag) = client.images.get_tag("doge").await {
    ///     //do something with the tag
    /// }
    /// ```
    pub async fn get_tag(&self, tag: impl AsRef<str>) -> reqwest::Result<TagList> {
        if tag.as_ref().is_empty() { panic!("Tag cannot be empty") }

        let response = self.http.clone().get(endpoint(format!("/images/tags/{}", tag.as_ref())).as_str())
            .send()
            .await?;

        response.json::<TagList>().await
    }

    ///Get a random NSFW image
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if let Ok(nsfw) = client.images.random_nsfw(true).await {
    ///     //do something with the image
    /// }
    /// ```
    pub async fn random_nsfw(&self, gifs: bool) -> reqwest::Result<RedditImage> {
        let response = self.http.clone().get(endpoint("/images/random-nsfw").as_str())
            .query(&[("gifs", gifs)])
            .send()
            .await?;

        response.json::<RedditImage>().await
    }
}

pub enum SpanType {
    Hour,
    Day,
    Week,
    Month,
    Year,
    All
}

impl Display for SpanType {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        match self {
            SpanType::Hour => write!(f, "hour"),
            SpanType::Day => write!(f, "day"),
            SpanType::Week => write!(f, "week"),
            SpanType::Month => write!(f, "month"),
            SpanType::Year => write!(f, "year"),
            SpanType::All => write!(f, "all")
        }
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct Image {
    pub url: String,
    pub snowflake: String,
    pub nsfw: bool,
    pub tag: String
}

#[derive(Clone, Debug, Deserialize)]
pub struct RedditImage {
    pub title: String,
    pub image_url: String,
    pub source: String,
    pub subreddit: String,
    pub upvotes: u64,
    pub downvotes: u64,
    pub comments: u64,
    pub created_at: f64,
    pub nsfw: bool,
    pub author: String,
    pub awards: u64
}

#[derive(Clone, Debug, Deserialize)]
pub struct WikiHowImage {
    pub url: String,
    pub title: String,
    pub nsfw: bool,
    pub article_url: String
}

#[derive(Clone, Debug, Deserialize)]
pub struct TagModel {
    pub name: String,
    pub nsfw: bool,
}

#[derive(Clone, Debug, Deserialize)]
pub struct TagList {
    pub models: Option<Vec<TagModel>>,
    pub tags: Vec<String>,
    pub nsfw_tags: Option<Vec<String>>
}