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
use std::{collections::HashMap, path::Path};
use url::Url;

/// A Deviation
#[derive(Debug, serde::Deserialize)]
pub struct Deviation {
    // TODO: This is a number in a scraped deviation. Make either parse here.
    /// DeviantArt Author
    // pub author: Author,

    /// ?
    #[serde(rename = "blockReasons")]
    pub block_reasons: Vec<serde_json::Value>,

    /// Deviation ID
    #[serde(rename = "deviationId")]
    pub deviation_id: u64,

    /// Deviation Type
    #[serde(rename = "type")]
    pub kind: String,

    /// Image Url
    pub url: Url,

    /// Media Info
    pub media: DeviationMedia,

    /// Title
    pub title: String,

    /// Text content for literature
    #[serde(rename = "textContent")]
    pub text_content: Option<TextContext>,

    /// Whether this is downloadable
    #[serde(rename = "isDownloadable")]
    pub is_downloadable: bool,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

impl Deviation {
    /// Get the media url for this [`Deviation`].
    pub fn get_media_url(&self) -> Option<Url> {
        let mut url = self.media.base_uri.as_ref()?.clone();
        url.query_pairs_mut()
            .append_pair("token", self.media.token.first()?);
        Some(url)
    }

    /// Get the "download" url for this [`Deviation`].
    pub fn get_download_url(&self) -> Option<Url> {
        let mut url = self.media.base_uri.as_ref()?.clone();
        url.query_pairs_mut()
            .append_pair("token", self.media.token.get(1)?);
        Some(url)
    }

    /// Get the fullview url for this [`Deviation`].
    pub fn get_fullview_url(&self) -> Option<Url> {
        let mut url = self.media.base_uri.as_ref()?.clone();

        // Allow the "content" section of the path to not exist, but the fullview data MUST exist.
        if let Some(path) = self.media.get_fullview_media_type()?.content.as_ref() {
            let mut path_segments_mut = url.path_segments_mut().ok()?;

            for path in path.split('/').filter(|p| !p.is_empty()) {
                // Replace "<pretty-name>" with the actual pretty name.
                let pretty_name = self.media.pretty_name.as_ref()?;
                let path = path.replace("<prettyName>", pretty_name);
                path_segments_mut.push(&path);
            }
        }

        // We assume that a token is not provided in cases where it is not needed.
        // As such, this part is optional.
        // So far, a token is allowed to be missing when the "content" section of the fullview data is missing
        // Correct this if these assumptions are wrong.
        if let Some(token) = self.media.token.first() {
            url.query_pairs_mut().append_pair("token", token);
        }

        Some(url)
    }

    /// Get the GIF url for this [`Deviation`].
    pub fn get_gif_url(&self) -> Option<Url> {
        let mut url = self.media.get_gif_media_type()?.b.clone()?;
        url.query_pairs_mut()
            .append_pair("token", self.media.token.first()?);
        Some(url)
    }

    /// Get the best video url
    pub fn get_best_video_url(&self) -> Option<&Url> {
        let url = self.media.get_best_video_media_type()?.b.as_ref()?;
        Some(url)
    }

    /// Whether this is an image
    pub fn is_image(&self) -> bool {
        self.kind == "image"
    }

    /// Whether this is literature
    pub fn is_literature(&self) -> bool {
        self.kind == "literature"
    }

    /// Whether this is a film
    pub fn is_film(&self) -> bool {
        self.kind == "film"
    }

    /// Get the most "fitting" url to download an image.
    ///
    /// Usually, [`DeviationExtended`] holds better data than a [`Deviation`], so prefer that instead.
    pub fn get_image_download_url(&self) -> Option<Url> {
        // Try to get the download url.
        if let Some(url) = self.get_download_url() {
            return Some(url);
        }

        // If that fails, this is probably a gif, so we try to get the gif url.
        if let Some(url) = self.get_gif_url() {
            return Some(url);
        }

        // Otherwise, assume failure
        None
    }
    /// Try to get the original extension of this [`Deviation`]
    pub fn get_extension(&self) -> Option<&str> {
        if self.is_image() {
            let url = self
                .media
                .get_gif_media_type()
                .and_then(|media_type| media_type.b.as_ref())
                .or(self.media.base_uri.as_ref())?;
            Path::new(url.as_str()).extension()?.to_str()
        } else if self.is_literature() {
            None
        } else if self.is_film() {
            let url = self.media.get_best_video_media_type()?.b.as_ref()?;
            Path::new(url.as_str()).extension()?.to_str()
        } else {
            None
        }
    }
}

#[derive(Debug, serde::Deserialize)]
pub struct Author {
    /// is the user new
    #[serde(rename = "isNewDeviant")]
    pub is_new_deviant: bool,

    /// User UUID
    #[serde(rename = "useridUuid")]
    pub userid_uuid: String,

    /// User icon url
    pub usericon: Url,

    /// User ID
    #[serde(rename = "userId")]
    pub user_id: u64,

    /// Username
    pub username: String,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

/// The media field of a [`Deviation`].
#[derive(Debug, serde::Deserialize)]
pub struct DeviationMedia {
    /// The base uri
    #[serde(rename = "baseUri")]
    pub base_uri: Option<Url>,

    /// Image token
    #[serde(default)]
    pub token: Vec<String>,

    /// Types
    pub types: Vec<MediaType>,

    /// Pretty Name
    #[serde(rename = "prettyName")]
    pub pretty_name: Option<String>,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

impl DeviationMedia {
    /// Try to get the fullview [`MediaType`].
    pub fn get_fullview_media_type(&self) -> Option<&MediaType> {
        self.types.iter().find(|t| t.is_fullview())
    }

    /// Try to get the gif [`MediaType`].
    pub fn get_gif_media_type(&self) -> Option<&MediaType> {
        self.types.iter().find(|t| t.is_gif())
    }

    /// Try to get the video [`MediaType`]
    pub fn get_best_video_media_type(&self) -> Option<&MediaType> {
        self.types
            .iter()
            .filter(|media_type| media_type.is_video())
            .max_by_key(|media_type| media_type.width)
    }
}

/// DeviantArt [`DeviationMedia`] media type.
#[derive(Debug, serde::Deserialize)]
pub struct MediaType {
    /// The content. A uri used with base_uri.
    #[serde(rename = "c")]
    pub content: Option<String>,

    /// Image Height
    #[serde(rename = "h")]
    pub height: u64,

    /// ?
    // pub r: u64,

    /// The kind of media
    #[serde(rename = "t")]
    pub kind: String,

    /// Image Width
    #[serde(rename = "w")]
    pub width: u64,

    /// ?
    // pub f: Option<u64>,

    /// ?
    pub b: Option<Url>,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

impl MediaType {
    /// Whether this is the fullview
    pub fn is_fullview(&self) -> bool {
        self.kind == "fullview"
    }

    /// Whether this is a gif
    pub fn is_gif(&self) -> bool {
        self.kind == "gif"
    }

    /// Whether this is a video
    pub fn is_video(&self) -> bool {
        self.kind == "video"
    }
}

/// Text Content for literature
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct TextContext {
    /// Excerpt of text
    pub excerpt: String,

    /// Html data
    pub html: Html,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

/// Text Context html
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Html {
    /// ?
    pub features: String,

    /// Text markup data
    pub markup: Option<String>,

    /// The kind of text data
    #[serde(rename = "type")]
    pub kind: String,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

impl Html {
    /// Try to parse the markup field
    pub fn get_markup(&self) -> Option<Result<Markup, serde_json::Error>> {
        let markup = self.markup.as_ref()?;
        Some(serde_json::from_str(markup))
    }
}

/// Text Context Html Markup
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Markup {
    /// Blocks of marked-up text
    pub blocks: Vec<Block>,

    /// ?
    #[serde(rename = "entityMap")]
    pub entity_map: serde_json::Value,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

/// A Markup block
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Block {
    /// ?
    pub data: serde_json::Value,

    /// ?
    pub depth: u64,

    /// ?
    pub key: String,

    /// Text data
    pub text: String,

    #[serde(rename = "type")]
    pub kind: String,

    /// Unknown K/Vs
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}