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
//! This is a library crate for working with the [Modarchive](https://modarchive.org)
//! website, it is very barebones and simple to work with, please check out the
//! documentation for [`ModInfo`] and its methods for more info, do be sure to look
//! at the examples aswell!
//!
//! (This is the Reborn update, v0.5.x)
//!
//! ## Example: Get module info as a struct using a module id
//! ```rust
//! use trackermeta::ModInfo;
//!
//! fn main() {
//!     let modinfo = ModInfo::get(51772).unwrap();
//!     println!("{:#?}", modinfo);
//! }
//! ```
//!
//! ## Example: Resolve filename to id then use id to get the info as struct
//! ```rust
//! use trackermeta::ModInfo;
//!
//! fn main() {
//!     let modid = ModInfo::resolve_filename("noway.s3m").unwrap()[0].id;
//!     let modinfo = ModInfo::get(modid).unwrap();
//!     println!("{:#?}", modinfo);
//! }
//! ```
//!
//! There are more examples other than these which showcase more, remember
//! to check the `examples` directory!
//!
//! [Modarchive]: https://modarchive.org
#![allow(clippy::needless_doctest_main)]
#![forbid(unsafe_code)]

use chrono::prelude::{DateTime, Utc};

// https://stackoverflow.com/a/64148190
fn iso8601_time(st: &std::time::SystemTime) -> String {
    let dt: DateTime<Utc> = (*st).into();
    format!("{}", dt.format("%+"))
}

/// Error enum for functions in the crate that return a [`Result`]
#[derive(Debug)]
pub enum Error {
    NotFound,
}

/// Simple struct to represent a search result, id and filename will be provided in each
#[derive(Debug)]
pub struct ModSearch {
    pub id: u32,
    pub filename: String,
}

/// Struct containing all of the info about a module
#[derive(Debug)]
pub struct ModInfo {
    /// The module ID of the module on modarchive
    pub id: u32,
    /// The filename of the module
    pub filename: String,
    /// The title of the module
    pub title: String,
    /// The file size of the module, use the
    /// crate `byte-unit` to convert them to other units
    pub size: String,
    /// The MD5 hash of the module file as a string
    pub md5: String,
    /// The format of the module, for example `XM`, `IT`
    /// or `MOD` and more, basically the extension of the
    /// module file
    pub format: String,
    /// Spotlit module or not
    pub spotlit: bool,
    /// Download count of the module at the time of scraping
    pub download_count: u32,
    /// Times the module has been favourited at the time of
    /// scraping
    pub fav_count: u32,
    /// The time when it was scraped
    pub scrape_time: String,
    /// The channel count of the module
    pub channel_count: u32,
    /// The genre of the module
    pub genre: String,
    /// The upload date of the module
    pub upload_date: String,
    /// The instrument text of the module
    pub instrument_text: String,
}

cfg_if::cfg_if! {
    if #[cfg(feature = "infinity-retry")] {
        fn inner_request(mod_id: u32) -> String{
            loop {
                match ureq::get(
                    format!(
                        "https://modarchive.org/index.php?request=view_by_moduleid&query={}",
                        mod_id
                    )
                    .as_str(),
                )
                .timeout(std::time::Duration::from_secs(60))
                .call() {
                    Ok(req) => {
                        return req.into_string().unwrap()
                    }
                    Err(_) => continue,
                };
            }
        }
    } else {
        fn inner_request(mod_id: u32) -> String {
            let body = ureq::get(
                format!(
                    "https://modarchive.org/index.php?request=view_by_moduleid&query={}",
                    mod_id
                )
                .as_str(),
            )
            .timeout(std::time::Duration::from_secs(60))
            .call()
            .unwrap()
            .into_string()
            .unwrap();

            body
        }
    }
}

impl ModInfo {
    /// Probably the singular most important function in this crate, takes a mod id (can be
    /// generated at random, deliberately entered or acquired by resolving a filename and
    /// picking a search result), and then gives you a full [`ModInfo`] struct.
    pub fn get(mod_id: u32) -> Result<ModInfo, crate::Error> {
        let body = inner_request(mod_id);

        let dom = tl::parse(body.as_ref(), tl::ParserOptions::default()).unwrap();
        let parser = dom.parser();

        let id = mod_id;
        let scrape_time = iso8601_time(&std::time::SystemTime::now());

        let valid = dom.get_elements_by_class_name("mod-page-archive-info")
                .next()
                .is_some();

        if !valid {
            return Err(crate::Error::NotFound);
        }

        let filename = {
            dom.get_elements_by_class_name("module-sub-header")
                .next()
                .unwrap() // we can unwrap because if its absent we've already errored up above
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace('(', "")
                .replace(')', "")
        };

        let title = {
            escaper::decode_html(
                &dom.query_selector("h1")
                    .and_then(|mut iter| iter.next())
                    .unwrap()
                    .get(parser)
                    .unwrap()
                    .inner_text(parser)
                    .replace(&format!(" ({})", &filename), ""),
            )
            .unwrap()
        };

        let size = {
            dom.query_selector("li.stats")
                // get the 8th hit (nth starts from 0)
                .and_then(|mut iter| iter.nth(7))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Uncompressed Size: ", "")
        };

        let md5 = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(4))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("MD5: ", "")
        };

        let format = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(5))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Format: ", "")
        };

        let spotlit = dom.get_elements_by_class_name("mod-page-featured")
                .next()
                .is_some();

        let download_count = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(2))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Downloads: ", "")
                .parse()
                .unwrap()
        };

        let fav_count = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(3))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Favourited: ", "")
                .replace(" times", "")
                .parse()
                .unwrap()
        };

        let channel_count = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(6))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Channels: ", "")
                .parse()
                .unwrap()
        };

        dom.query_selector("a.master");

        let genre = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.nth(8))
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .replace("Genre: ", "")
        };

        let upload_date = {
            dom.query_selector("li.stats")
                .and_then(|mut iter| iter.next())
                .unwrap()
                .get(parser)
                .unwrap()
                .inner_text(parser)
                .split(" times since ")
                .nth(1)
                .unwrap()
                .replace(" :D", "")
                .trim()
                .into()
        };

        let instrument_text = {
            escaper::decode_html(
                &dom.query_selector("pre")
                    .and_then(|mut iter| iter.nth(1))
                    .unwrap()
                    .get(parser)
                    .unwrap()
                    .inner_text(parser),
            )
            .unwrap()
            .trim()
            .into()
        };

        Ok(ModInfo {
            id,
            filename,
            title,
            size,
            md5,
            format,
            spotlit,
            download_count,
            fav_count,
            scrape_time,
            channel_count,
            genre,
            upload_date,
            instrument_text,
        })
    }

    /// Returns a modarchive download link for the given module, you can get this struct by using
    /// [`ModInfo::get()`], or search using [`ModInfo::resolve_filename()`], if you're using the
    /// resolver function please consider using the [`ModSearch::get_download_link()`] method
    /// instead.
    pub fn get_download_link(&self) -> String {
        format!(
            "https://api.modarchive.org/downloads.php?moduleid={}#{}",
            self.id, self.filename
        )
    }

    /// Searches for your string on Modarchive and returns the results on the first page (a.k.a
    /// only up to the first 40) as a vector of [`ModSearch`]
    pub fn resolve_filename(filename: &str) -> Result<Vec<ModSearch>, crate::Error> {
        let body: String = ureq::get(
                format!(
                    "https://modarchive.org/index.php?request=search&query={}&submit=Find&search_type=filename",
                    filename
                )
                .as_str(),
            )
            .call()
            .unwrap()
            .into_string()
            .unwrap();

        let dom = tl::parse(&body, tl::ParserOptions::default()).unwrap();
        let parser = dom.parser();

        let status = dom.query_selector("h1.site-wide-page-head-title");

        match status {
            Some(_) => {}
            None => return Err(crate::Error::NotFound),
        };
        // from this point on we can unwrap after each query selector
        // because our info will for sure be present.

        let links: Vec<ModSearch> = dom
            .query_selector("a.standard-link[title]")
            .unwrap()
            .map(|nodehandle| {
                let node = nodehandle.get(parser).unwrap();

                let id = match node.as_tag().unwrap().attributes().get("href") {
                    Some(Some(a)) => a
                        .as_utf8_str()
                        .split("query=")
                        .nth(1)
                        .unwrap()
                        .parse()
                        .unwrap(),
                    Some(None) => unreachable!(),
                    None => unreachable!(),
                };

                let filename = node.inner_text(parser).into();

                ModSearch { id, filename }
            })
            .collect();

        Ok(links)
    }
}

impl ModSearch {
    /// Get the download link of this specific module.
    pub fn get_download_link(&self) -> String {
        format!(
            "https://api.modarchive.org/downloads.php?moduleid={}#{}",
            self.id, self.filename
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::ModInfo;

    #[test]
    fn instr_text() {
        let instr_text = ModInfo::get(61772).unwrap().instrument_text;
        assert_eq!(
            instr_text,
            "7th  Dance

             By:
 Jari Ylamaki aka Yrde
  27.11.2000 HELSINKI

            Finland
           SITE :
  www.mp3.com/Yrde"
        );
    }

    #[test]
    fn invalid_modid() {
        let invalid = ModInfo::get(30638);
        assert!(invalid.is_err());
    }

    #[test]
    fn valid_modid() {
        let valid = ModInfo::get(99356);
        assert!(valid.is_ok());
    }

    #[test]
    fn spotlit_modid() {
        let module = ModInfo::get(158263).unwrap();
        assert_eq!(module.spotlit, true);
    }

    #[test]
    fn name_resolving() {
        let mod_search = ModInfo::resolve_filename("virtual-monotone.mod");
        let mod_search = &mod_search.unwrap()[0];
        assert_eq!(mod_search.id, 88676);
        assert_eq!(
            mod_search.get_download_link().as_str(),
            "https://api.modarchive.org/downloads.php?moduleid=88676#virtual-monotone.mod"
        );
    }

    #[test]
    fn dl_link_modinfo() {
        let modinfo = ModInfo::get(41070).unwrap();
        assert_eq!(
            modinfo.get_download_link().as_str(),
            "https://api.modarchive.org/downloads.php?moduleid=41070#fading_horizont.mod"
        );
    }
}