aboutsummaryrefslogtreecommitdiff
path: root/src/character.rs
blob: f4686cf2195a918e85e2e4a9d80e14435794fc5d (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
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
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 romaji: String,
    pub japanese: String,
    pub aliases: Vec<String>,
}

#[derive(Debug, Serialize)]
pub struct Images {
    pub thumb: String,
    pub full: String,
}

#[derive(Debug, Serialize)]
pub struct Traits {
    pub official: Vec<DLListItem>,
    pub indexed: Vec<DLListItem>,
}

#[derive(Debug, Serialize)]
pub struct Character {
    pub name: Names,
    pub image: Images,
    pub tags: Vec<Tag>,
    pub traits: Traits,
    pub assignments: Vec<u32>,
    pub extra: Vec<DLListItem>,

    pub role: Option<String>,
}

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

impl Images {
    pub fn new() -> Self {
        Images {
            thumb: String::new(),
            full: String::new(),
        }
    }
}

impl Traits {
    pub fn new() -> Self {
        Traits {
            official: vec![],
            indexed: vec![],
        }
    }
}

impl Character {
    pub fn new() -> Self {
        Character {
            name: Names::new(),
            image: Images::new(),
            tags: vec![],
            traits: Traits::new(),
            assignments: vec![],
            extra: vec![],
            role: None,
        }
    }

    pub fn parse(&mut self, buf: &str) {
        let re_extras = Regex::new(r#"(?is)Extra Details \| [0-9]+</H3>.*?<dl>(.*?)</dl>"#)
                            .unwrap();
        let mut sections = get_sections();
        section::process(&buf, &mut sections);

        let caps = re_extras.captures(&buf);
        if caps.is_some() {
            self.extra = dl_list::parse(caps.unwrap().at(1).unwrap());
        }

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

            self.name.romaji = name.data["romaji".into()].clone();
            self.name.japanese = name.data["japanese".into()].clone();

            if name.data["aliases".into()].len() > 0 {
                self.name.aliases = name.data["aliases".into()]
                                        .split(", ")
                                        .map(|s| s.to_string())
                                        .collect();
            }

            self.image.thumb = image.data["thumb".into()].clone();
            self.image.full = image.data["full".into()].clone();

            if tags.data.contains_key("tags_raw".into()) {
                self.tags = tags::parse(&tags.data["tags_raw".into()]);
            }

            self.traits.official =
                dl_list::parse(&(&sections["traits"] as &Section).data["official_raw".into()]);
            self.traits.indexed =
                dl_list::parse(&(&sections["traits"] as &Section).data["indexed_raw".into()]);

            self.assignments = tiles::parse_tile_link_ids(&(&sections["assignments"] as &Section).data["raw".into()], "series");

            if misc.data["role".into()].len() > 0 {
                self.role = Some(misc.data["role".into()].clone());
            }
        }
    }
}

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

    s.insert("name".into(), Section::new("name", r#"(?is)Romaji Name.*?<TD>(.*?)\s?</TD>.*?Japanese Name.*?<TD>(.*?)\s?</TD>.*?Aliases.*?<TD>(.*?)\s?</TD>"#, vec!["romaji", "japanese", "aliases"]));
    s.insert("misc".into(),
             Section::new("misc",
                          r#"(?is)Role</TH>.*?<TD>(.*?)\s?</TD>"#,
                          vec!["role"]));
    s.insert("image".into(), Section::new("image", r#"(?is)<H3 id="section99">.*<img src="(.*?)" alt=.*?></a><p><a href="(.*?)">View Full Size Image"#, vec!["thumb", "full"]));
    s.insert("tags".into(),
             Section::new("tags",
                          r#"(?is)tagged as</P>.*?<TH>(.*?)</TH>"#,
                          vec!["tags_raw"]));
    s.insert("traits".into(), Section::new("traits", r#"(?is)indexed traits</P>.*?<dl>(.*?)</dl>.*?official traits\s?</P>.*?<dl>(.*?)</dl>"#, vec!["indexed_raw", "official_raw"]));
    s.insert("assignments".into(),
             Section::new("assignments",
                          r#"(?is)appears in the following</P>(.*?)</UL>"#,
                          vec!["raw"]));
    s.insert("chars_similar_traits".into(),
             Section::new("assignments",
                          r#"(?is)with Similar Traits</H3>(.*?)</UL>"#,
                          vec!["raw"]));

    s
}