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
use crate::error::ErrorMatch;
use crate::pattern::{all_patterns, pattern};
use std::cmp::{max, min};

#[derive(Clone, Debug, Default)]
pub struct Metadata {
    title: String,
    season: Option<i32>,
    episode: Option<i32>,
    year: Option<i32>,
    resolution: Option<String>,
    quality: Option<String>,
    codec: Option<String>,
    audio: Option<String>,
    group: Option<String>,
    extended: bool,
    hardcoded: bool,
    proper: bool,
    repack: bool,
    widescreen: bool,
    unrated: bool,
    three_d: bool,
    imdb: Option<String>,
}

impl Metadata {
    pub fn from(name: &str) -> Result<Self, ErrorMatch> {
        let mut title_start = 0;
        let mut title_end = name.len() + 1;

        let mut captures = Vec::new();
        for (pname, p) in all_patterns() {
            let cap = p.captures(name).map(|caps| caps.get(0));
            captures.push((pname, cap));
            cap.map(|m| {
                m.map(|n| {
                    if p.before_title() {
                        title_start = max(title_start, n.end());
                    } else {
                        title_end = min(title_end, n.start());
                    }
                })
            });
        }

        if title_start >= title_end {
            return Err(ErrorMatch::new(captures));
        }

        let mut title = name[title_start..title_end - 1].to_string();
        if let Some(pos) = title.find('(') {
            title = title.split_at(pos).0.to_string();
        }
        title = title.trim_start_matches(" -").to_string();
        title = title.trim_end_matches(" -").to_string();
        if !title.contains(' ') && title.contains('.') {
            title = title.replace(".", " ")
        }
        title = title
            .replace("_", " ")
            .replacen("(", "", 1)
            .replacen("- ", "", 1)
            .trim()
            .to_string();

        let season = pattern("season").unwrap().captures(name).and_then(|caps| {
            caps.name("short")
                .or_else(|| caps.name("long"))
                .or_else(|| caps.name("dash"))
                .map(|m| m.as_str())
                .map(|s| s.parse().unwrap())
        });
        let episode = pattern("episode").unwrap().captures(name).and_then(|caps| {
            caps.name("short")
                .or_else(|| caps.name("long"))
                .or_else(|| caps.name("cross"))
                .or_else(|| caps.name("dash"))
                .map(|m| m.as_str())
                .map(|s| s.parse().unwrap())
        });
        let year = pattern("year").unwrap().captures(name).and_then(|caps| {
            caps.name("year")
                .map(|m| m.as_str())
                .map(|s| s.parse().unwrap())
        });
        let resolution = pattern("resolution")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()));
        let quality = pattern("quality")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(0).map(|m| m.as_str().to_string()));
        let codec = pattern("codec")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(0).map(|m| m.as_str().to_string()));
        let audio = pattern("audio")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(0).map(|m| m.as_str().to_string()));
        let group = pattern("group")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(2).map(|m| m.as_str().to_string()));
        let imdb = pattern("imdb")
            .unwrap()
            .captures(name)
            .and_then(|caps| caps.get(0).map(|m| m.as_str().to_string()));

        let extended = pattern("extended").unwrap().captures(name).is_some();
        let hardcoded = pattern("hardcoded").unwrap().captures(name).is_some();
        let proper = pattern("proper").unwrap().captures(name).is_some();
        let repack = pattern("repack").unwrap().captures(name).is_some();
        let widescreen = pattern("widescreen").unwrap().captures(name).is_some();
        let unrated = pattern("unrated").unwrap().captures(name).is_some();
        let three_d = pattern("three_d").unwrap().captures(name).is_some();

        Ok(Metadata {
            title,
            season,
            episode,
            year,
            resolution,
            quality,
            codec,
            audio,
            group,
            extended,
            hardcoded,
            proper,
            repack,
            widescreen,
            unrated,
            three_d,
            imdb,
        })
    }

    pub fn title(&self) -> &str {
        &self.title
    }
    pub fn season(&self) -> Option<i32> {
        self.season
    }
    pub fn episode(&self) -> Option<i32> {
        self.episode
    }
    pub fn year(&self) -> Option<i32> {
        self.year
    }
    pub fn resolution(&self) -> Option<&str> {
        self.resolution.as_deref()
    }
    pub fn quality(&self) -> Option<&str> {
        self.quality.as_deref()
    }
    pub fn codec(&self) -> Option<&str> {
        self.codec.as_deref()
    }
    pub fn audio(&self) -> Option<&str> {
        self.audio.as_deref()
    }
    pub fn group(&self) -> Option<&str> {
        self.group.as_deref()
    }
    pub fn imdb_tag(&self) -> Option<&str> {
        self.imdb.as_deref()
    }
    pub fn extended(&self) -> bool {
        self.extended
    }
    pub fn hardcoded(&self) -> bool {
        self.hardcoded
    }
    pub fn proper(&self) -> bool {
        self.proper
    }
    pub fn repack(&self) -> bool {
        self.repack
    }
    pub fn widescreen(&self) -> bool {
        self.widescreen
    }
    pub fn unrated(&self) -> bool {
        self.unrated
    }
    pub fn three_d(&self) -> bool {
        self.three_d
    }
}