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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! # Intro
//! 
//! Hi my english isnt the best so sorry if there are some errors in my grammer. I made this lil crate, just for fun, with only a few features,
//! so I wouldnt recommend using it but of course I would be happy if you use it none the less.
//! 
//! 
//! There may be some parts where you need to know the R34 API cause i forgot something. So check out their API
//! 
//! 
//!  <https://api.rule34.xxx/>

#![allow(dead_code)]

use core::fmt;
use serde_json::Value;
use std::{collections::HashMap, fmt::Display};

pub struct Request {
    /// Default API Access URL "https://api.rule34.xxx/index.php?page=dapi&s=post&q=index"
    pub api_url: String,
    /// Limitter for max Post per requests. R34s API limmits to max 1000 Posts per request. Default Setting is 1000.
    pub req_limit: usize,
    /// All R34 tags should work when exactly taken over. Default is empty.
    pub tags: Vec<&'static str>,
    /// Json Formatted or not Json Formatted: Default is true.
    pub json: bool,
    /// Hashmap with the three IDs:
    /// ID: Filter for ID of Post. (Recommended to use alone)!
    /// PID: Filter for Page ID.
    /// CID: Filter for Change ID (Not Recomended)!
    /// By Default all are empty.
    pub ids: HashMap<String, Option<usize>>,
}

fn format_id(ids: &HashMap<String, Option<usize>>, key: &str) -> String {
    match ids.get(key) {
        Some(Some(value)) => format!("&{}={}", key, value),
        _ => String::new(),
    }
}

impl Request {
    /// Creates a new Request by default settings
    pub fn new() -> Request {
        Request::default()
    }

    /// Sets the limit Filter for the request
    pub fn set_limit(mut self, limit: usize) -> Self {
        self.req_limit = limit;
        self
    }

    /// Adds a Tag to the request
    pub fn add_tag(mut self, tag: &'static str) -> Self {
        self.tags.push(tag);
        self
    }

    pub fn add_tags(mut self, mut tags: Vec<&'static str>) -> Self {
        self.tags.append(&mut tags);
        self
    }

    /// sets the CID of the request
    pub fn set_cid(mut self, cid: usize) -> Self {
        self.ids.insert("cid".to_string(), Some(cid));
        self
    }

    /// Sets the PID of the Request
    pub fn set_pid(mut self, pid: usize) -> Self {
        self.ids.insert("pid".to_string(), Some(pid));
        self
    }

    /// Sets the Post ID of the Request
    pub fn set_id(mut self, id: usize) -> Self {
        self.ids.insert("id".to_string(), Some(id));
        self
    }

    /// Activates a Json Formatted Request
    pub fn set_json_formatted(mut self, json: bool) -> Self {
        self.json = json;
        self
    }

    /// Returns the Final Request URL as a String
    pub fn to_req_url(&mut self) -> String {
        let api_url = self.api_url.clone();
        let req_limit = self.req_limit;
        let json = if self.json == true { r"&json=1" } else { "" };
        let tags = format!(r"&tags={}", self.tags.join(" "));

        let id = format_id(&self.ids, "id");
        let pid = format_id(&self.ids, "pid");
        let cid = format_id(&self.ids, "cid");

        let req_string = format!(
            r"{}{}&limit={}{}{}{}{}",
            api_url, tags, req_limit, json, id, pid, cid
        );

        req_string
    }
}

impl Default for Request {
    fn default() -> Self {
        let mut ids: HashMap<String, Option<usize>> = HashMap::new();
        ids.insert("id".to_string(), None);
        ids.insert("pid".to_string(), None);
        ids.insert("cid".to_string(), None);

        Request {
            api_url: String::from("https://api.rule34.xxx/index.php?page=dapi&s=post&q=index"),
            req_limit: 1000,
            tags: Vec::new(),
            json: true,
            ids,
        }
    }
}

/// A Parser for R34 Json responses.
///
/// Holds a HashMap of config options that can all be tweaked with the `set_conf()` function.
/// Explenation and possible inputs are found in the functions description.
pub struct R34JsonParser {
    pub conf: HashMap<&'static str, bool>,
}

impl Default for R34JsonParser {
    fn default() -> Self {
        let mut conf: HashMap<&str, bool> = HashMap::new();
        conf.insert("file_url", true);
        conf.insert("image", true);
        conf.insert("tags", true);
        conf.insert("width", true);
        conf.insert("height", true);
        conf.insert("sample", true);
        conf.insert("samlpe_url", true);
        conf.insert("sample_width", true);
        conf.insert("sample_height", true);
        conf.insert("source", true);
        conf.insert("id", true);
        conf.insert("score", true);
        conf.insert("parent_id", true);
        conf.insert("comment_count", true);
        conf.insert("preview_url", true);
        conf.insert("owner", true);
        conf.insert("rating", true);

        R34JsonParser { conf }
    }
}

impl R34JsonParser {
    /// Takes a valid json string and returns a Vector of Posts with all information configured.
    pub fn parse_json(&mut self, s: &str) -> Vec<Post> {
        let v: Value = serde_json::from_str(s).unwrap();

        let mut post_vec: Vec<Post> = Vec::new();

        if let Value::Array(a) = v {
            for obj in a {
                let mut post = Post::default();

                for (k, b) in &mut self.conf {
                    match (k, b) {
                        (&"file_url", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("file_url") {
                                    post.file_url = s.clone();
                                }
                            }
                        }
                        (&"image", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("image") {
                                    post.image = s.clone();
                                }
                            }
                        }
                        (&"tags", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("tags") {
                                    post.tags = s
                                        .clone()
                                        .split_whitespace()
                                        .map(move |s| s.to_string())
                                        .collect();
                                }
                            }
                        }
                        (&"width", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("width") {
                                    post.width = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"height", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("height") {
                                    post.height = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"sample", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Bool(b)) = map.get("sample") {
                                    post.sample = b.clone();
                                }
                            }
                        }
                        (&"sample_url", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("sample_url") {
                                    post.sample_url = s.clone();
                                }
                            }
                        }
                        (&"sample_width", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("sample_width") {
                                    post.sample_width = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"sample_height", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("sample_height") {
                                    post.sample_height = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"source", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("source") {
                                    post.source = s.clone();
                                }
                            }
                        }
                        (&"id", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("id") {
                                    post.id = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"score", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("score") {
                                    post.score = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"parent_id", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("parent_id") {
                                    post.parent_id = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"comment_count", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::Number(n)) = map.get("comment_count") {
                                    post.comment_count = n.as_u64().unwrap();
                                }
                            }
                        }
                        (&"preview_url", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("preview_url") {
                                    post.preview_url = s.clone();
                                }
                            }
                        }
                        (&"owner", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("owner") {
                                    post.owner = s.clone();
                                }
                            }
                        }
                        (&"rating", true) => {
                            if let Value::Object(ref map) = obj {
                                if let Some(Value::String(s)) = map.get("rating") {
                                    match s.as_str() {
                                        "explicit" => post.rating = Some(Rating::Explicit),
                                        "safe" => post.rating = Some(Rating::Safe),
                                        "questionable" => post.rating = Some(Rating::Questionable),
                                        _ => post.rating = None,
                                    }
                                    post.file_url = s.clone();
                                }
                            }
                        }
                        _ => (),
                    }
                }

                post_vec.push(post);
            }
        }
        post_vec
    }

    /// Set Conifg options with the name of the field and a bool.
    ///
    /// Will be changed to enum or something
    /// All keys/fieldnames are &str's e.g. `"file_url"` `"id"`
    ///
    ///
    /// Possible Keys:
    /// ```
    /// "id"
    /// "parent_id"
    /// "image"
    /// "tags"
    /// "source"
    /// "owner"
    /// "score"
    /// "comment_count"
    /// "rating"
    /// "sample"
    /// "file_url"
    /// "sample_url"
    /// "preview_url"
    /// "width"
    /// "height"
    /// "sample_width"
    /// "sample_height"
    /// ```
    pub fn set_conf(mut self, key: &'static str, set: bool) -> Self {
        *self.conf.get_mut(&key).unwrap() = set;
        self
    }
}

/// Rating of the Post as enum.
/// Will maybe get removed in future!
#[derive(Clone, Copy)]
pub enum Rating {
    Explicit,
    Safe,
    Questionable,
}

impl fmt::Display for Rating {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let r = match *self {
            Self::Explicit => "explicit",
            Self::Safe => "safe",
            Self::Questionable => "questionable",
        };
        write!(f, "{}", r)
    }
}

/// Post struct. Holds all information about a Post
#[derive(Clone)]
pub struct Post {
    /// Url of Post's File
    pub file_url: String,
    /// Name of Post's File
    pub image: String,
    /// Post's Tags
    pub tags: Vec<String>,
    /// Width of Post's File
    pub width: u64,
    /// Height of Post's File
    pub height: u64,

    /// Tells if the Post has a Sample
    pub sample: bool,
    /// Url of Post's Sample
    pub sample_url: String,

    /// Height of Post's Sample File
    pub sample_width: u64,
    /// Height of Post's Sample File
    pub sample_height: u64,
    /// Source of Post e.g. Twitter User etc.
    pub source: String,
    /// ID of Post
    pub id: u64,
    /// Score of Post
    pub score: u64,
    /// ID of Post's Parent Post
    pub parent_id: u64,
    /// Amount of Comments on the Post
    pub comment_count: u64,
    /// Url of Post's Preview Image
    pub preview_url: String,
    /// Name of Post's Owner/Poster
    pub owner: String,
    /// Rating of Post e.g. Safe, Explicit or Questionable
    pub rating: Option<Rating>,
}

impl Display for Post {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, 
            "image: {}\nfile_url: {}\nwidth: {}\nheight: {}\ntags: {:?}\nid: {}\nowner: {}\nrating: {}\nsample_url: {}\nsample_width: {}\nsample_height: {}\nsource: {}\nscore: {}\nparent_id: {}\ncomment_count: {}\npreview_url: {}\n",
             self.image,  self.file_url, self.width,
             self.height, self.tags, self.id, 
             self.owner, self.rating.clone().unwrap(), self.sample_url,
            self.sample_width, self.sample_height,
            self.source, self.score, self.parent_id,
            self.comment_count, self.preview_url)
    }
}

impl Default for Post {
    fn default() -> Self {
        Post {
            file_url: String::new(),
            width: 0,
            height: 0,
            image: String::new(),
            tags: vec![],
            sample: false,
            sample_url: String::new(),
            sample_width: 0,
            sample_height: 0,
            source: String::new(),
            id: 1,
            score: 0,
            parent_id: 0,
            comment_count: 0,
            preview_url: String::new(),
            owner: String::new(),
            rating: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        fs,
        io::{Read, Write},
        time,
    };

    #[test]
    fn bench_json_parse_this() {
        let mut buf = String::from("");
        fs::File::open("./response.json")
            .unwrap()
            .read_to_string(&mut buf)
            .unwrap();
        let json = buf.as_str();

        let now = time::Instant::now();
        let _posts = super::R34JsonParser::default().parse_json(json);
        let elapsed = now.elapsed();
        println!("Full: {:?}", elapsed);

        // let _file = fs::OpenOptions::new()
        //     .write(true)
        //     .append(true)
        //     .open("./test.txt")
        //     .unwrap()
        //     .write(format!("Full: {}\n", elapsed.as_nanos()).as_bytes())
        //     .unwrap();
    }

    #[test]
    fn test_json_parse_this() {
        let mut buf = String::from("");
        fs::File::open("./response.json")
            .unwrap()
            .read_to_string(&mut buf)
            .unwrap();
        let json = buf.as_str();

        let posts = super::R34JsonParser::default().parse_json(json);
        let post = &posts[0];

        let _file = fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open("./test.txt")
            .unwrap()
            .write(format!("Full: {}\n", &post.to_string()).as_bytes())
            .unwrap();
    }
}