aboutsummaryrefslogtreecommitdiff
path: root/src/series.rs
blob: 3037e6a7b6f4920c3f2dad792fcc033d5e23175a (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
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: String,
    pub romaji: String,
    pub furigana: String,
    pub japanese: String,
}

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

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

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

    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 = name.data["english".into()].to_string();
            self.name.aliases = name.data["aliases".into()].to_string();
            self.name.romaji = name.data["romaji".into()].to_string();
            self.name.furigana = name.data["furigana".into()].to_string();
            self.name.japanese = name.data["japanese".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
}