aboutsummaryrefslogtreecommitdiff
path: root/src/series.rs
blob: eba41be87cb389ee406d8e70227fd00fc24c1660 (plain)
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
use super::tags;
use super::tags::Tag;
use super::dl_list;
use super::dl_list::DLListItem;
use super::section;
use super::section::Section;
use super::regex::Regex;
use super::tiles;

use std::collections::HashMap;

#[derive(Debug, Serialize)]
pub struct Names {
    pub english: String,
    pub aliases: Vec<String>,
    pub romaji: String,
    pub furigana: String,
    pub japanese: String,
}

#[derive(Debug, Serialize)]
pub struct Series {
    pub name: Names,
    pub tags: Vec<String>,
    pub image: String,
}

impl Names {
    pub fn new() -> Self {
        Names {
            english: String::new(),
            aliases: vec![],
            romaji: String::new(),
            furigana: String::new(),
            japanese: String::new(),
        }
    }
}

fn remove_ahref(s: &str) -> String {
    let re = Regex::new(r#"(?i)<A.*?>(.*)"#).unwrap();
    for cap in re.captures_iter(s) {
        return cap.at(1).unwrap().to_string();
    }

    s.into()
}

impl Series {
    pub fn new() -> Self {
        Series {
            name: Names::new(),
            tags: vec![],
            image: String::new(),
        }
    }

    pub fn parse(&mut self, buf: &str) {
        let mut sections = get_sections();
        section::process(&buf, &mut sections);

        let re_genre_tags = Regex::new(r#"(?is)Genre Tags.*?>(.*?)</td>"#).unwrap();
        let re_genre_tag = Regex::new(r#"[0-9]">(.*?)</A>"#).unwrap();

        for cap in re_genre_tags.captures_iter(&buf) {
            self.tags = re_genre_tag.captures_iter(cap.at(1).unwrap())
                                    .map(|v| v.at(1).unwrap().to_string())
                                    .collect();
        }

        {
            let name: &Section = &sections["name".into()];

            self.name.english = remove_ahref(&name.data["english".into()]);
            self.name.aliases = remove_ahref(&name.data["aliases".into()]).split(", ").map(|v| v.to_string()).collect();
            if self.name.aliases[0] == "" {
                self.name.aliases.remove(0);
            }
            self.name.romaji = remove_ahref(&name.data["romaji".into()]);
            self.name.furigana = remove_ahref(&name.data["furigana".into()]);
            self.name.japanese = remove_ahref(&name.data["japanese".into()]);

            self.image = (&sections["image".into()] as &Section).data["small".into()].to_string();
        }
    }
}

fn get_sections() -> HashMap<String, Section> {
    let mut s: HashMap<String, Section> = HashMap::new();

    s.insert("name".into(), Section::new("name", r#"(?is)English Title.*?<TD>(.*?)<.*?Aliases.*?<TD>(.*?)<.*?Romaji Title.*?<TD.*?>(.*?)</.*?Furigana Title.*?<TD.*?>(.*?)</.*?Japanese Title.*?<TD.*?>(.*?)</"#, vec!["english", "aliases", "romaji", "furigana", "japanese"]));
    s.insert("image".into(), Section::new("image", r#"(?is)</H3>.*?besttable.*?<IMG.*?src="(.*?)""#, vec!["small"]));

    s
}