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
mod clap_wrap;
mod error;
pub use clap_wrap::*;
pub use error::*;
use lazy_regex::*;
use scraper::Selector;
use std::convert::TryFrom;
use std::fmt;
pub const URL_START: &str = "https://www.imdb.com/find?s=tt&q=";
pub static RESULT_SELECTOR: Lazy<Selector> =
Lazy::new(|| Selector::parse("td.result_text").unwrap());
static ID_REGEX: Lazy<Regex> = lazy_regex!("tt[0-9]+");
static NAME_REGEX: Lazy<Regex> = lazy_regex!(">.+?</a>");
const DIRT_MARGIN_NAME: (usize, usize) = (1, 4);
static GENRE_REGEX: Lazy<Regex> = lazy_regex!("\\([A-z]+(\\s[A-z]+)?\\)");
const DIRT_MARGIN_GENRE: (usize, usize) = (1, 1);
#[derive(Debug)]
pub struct SearchResult {
pub name: String,
pub id: String,
pub genre: Genre,
}
impl SearchResult {
fn find_name_in_fragment(fragment: &str) -> Result<&str> {
let m = NAME_REGEX
.find(fragment)
.ok_or(RunError::NameNotFound(fragment.into()))?;
let dirty_name = m.as_str();
let clean_name = &dirty_name[DIRT_MARGIN_NAME.0..dirty_name.len() - DIRT_MARGIN_NAME.1];
Ok(clean_name)
}
}
impl TryFrom<&str> for SearchResult {
type Error = RunError;
fn try_from(fragment: &str) -> Result<Self> {
let id = ID_REGEX
.find(fragment)
.ok_or(RunError::ImdbIdNotFound(fragment.into()))?
.as_str()
.into();
let name = SearchResult::find_name_in_fragment(fragment)?.to_string();
if cfg!(debug_assertions) && name.len() > 40 {
println!("DEBUG: Strangely long fragment: {:?}", fragment);
}
let genre_option = match GENRE_REGEX.find(fragment) {
Some(m) => {
let s = m.as_str();
&s[DIRT_MARGIN_GENRE.0..s.len() - DIRT_MARGIN_GENRE.1]
}
None => "",
};
let genre = Genre::from(genre_option);
Ok(SearchResult { name, id, genre })
}
}
impl fmt::Display for SearchResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.genre)
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Genre {
Movie,
TvSeries,
TvEpisode,
Short,
Video,
Other(String),
}
impl From<&str> for Genre {
fn from(s: &str) -> Self {
use Genre::*;
match s {
"Movie" | "" => Movie,
"TV Series" => TvSeries,
"TV Episode" => TvEpisode,
"Short" => Short,
"Video" => Video,
_ => Other(s.into()),
}
}
}
impl fmt::Display for Genre {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Genre::*;
write!(
f,
"{}",
match self {
Movie => "Movie",
TvSeries => "TV series",
TvEpisode => "TV episode",
Short => "Short",
Video => "Video",
Other(s) => s,
}
)
}
}
#[cfg(test)]
mod unit_tests {
use super::Genre::*;
use super::*;
const INPUTS: [&str; 10] = [
" <a href=\"/title/tt6856242/?ref_=fn_tt_tt_1\">The King's Man</a> (2021) ",
" <a href=\"/title/tt0405676/?ref_=fn_tt_tt_2\">All the King's Men</a> (2006) ",
" <a href=\"/title/tt2119547/?ref_=fn_tt_tt_3\">The Kingsmen</a> (2011) (Short) ",
" <a href=\"/title/tt0041113/?ref_=fn_tt_tt_4\">All the King's Men</a> (1949) ",
" <a href=\"/title/tt4649466/?ref_=fn_tt_tt_5\">Kingsman: The Golden Circle</a> (2017) ",
" <a href=\"/title/tt2802144/?ref_=fn_tt_tt_6\">Kingsman: The Secret Service</a> (2014) ",
" <a href=\"/title/tt0222577/?ref_=fn_tt_tt_7\">King's Men</a> (1975) (TV Series) ",
" <a href=\"/title/tt14642606/?ref_=fn_tt_tt_8\">The Kingsmen</a> (2017) (TV Episode) <br> <small>- Season 3 <sp
an class=\"ghost\">|</span> Episode 22 </small> <br><small>- <a href=\"/title/tt3319722/?ref_=fn_tt_tt_8a\">Gosp
el Music Showcase</a> (2011) (TV Series) </small> ",
" <a href=\"/title/tt0220969/?ref_=fn_tt_tt_9\">All the King's Men</a> (1999) (TV Movie) ",
" <a href=\"/title/tt0084793/?ref_=fn_tt_tt_10\">Tian xia di yi</a> (1983) ",
];
static SEARCH_RESULTS: Lazy<Vec<SearchResult>> = Lazy::new(|| {
INPUTS
.iter()
.map(|s| match SearchResult::try_from(*s) {
Ok(sr) => sr,
Err(why) => panic!("Failed to process test data: {}", why),
})
.collect()
});
#[test]
fn genre_from_str() {
assert_eq!(Genre::from(""), Movie);
assert_eq!(Genre::from("Movie"), Movie);
assert_eq!(Genre::from("TV Series"), TvSeries);
assert_eq!(Genre::from("TV Episode"), TvEpisode);
assert_eq!(Genre::from("Short"), Short);
assert_eq!(Genre::from("Video"), Video);
assert_eq!(Genre::from("foo"), Other("foo".into()));
assert_eq!(Genre::from("bar"), Other("bar".into()));
}
#[test]
fn name_searching() {
let names = [
"The King's Man",
"All the King's Men",
"The Kingsmen",
"All the King's Men",
"Kingsman: The Golden Circle",
"Kingsman: The Secret Service",
"King's Men",
"The Kingsmen",
"All the King's Men",
"Tian xia di yi",
];
names
.iter()
.zip(SEARCH_RESULTS.iter())
.for_each(|(name, sr)| {
assert_eq!(sr.name, *name);
});
}
#[test]
fn id_searching() {
let ids = [
"tt6856242",
"tt0405676",
"tt2119547",
"tt0041113",
"tt4649466",
"tt2802144",
"tt0222577",
"tt14642606",
"tt0220969",
"tt0084793",
];
ids.iter().zip(SEARCH_RESULTS.iter()).for_each(|(id, sr)| {
assert_eq!(sr.id, *id);
});
}
#[test]
fn genre_searching() {
let genres = [
Movie,
Movie,
Short,
Movie,
Movie,
Movie,
TvSeries,
TvEpisode,
Other("TV Movie".into()),
Movie,
];
genres
.iter()
.zip(SEARCH_RESULTS.iter())
.for_each(|(genre, sr)| {
assert_eq!(sr.genre, *genre);
});
}
#[test]
fn name_not_found() {
let fragments = [
"tt1234 (Movie)",
"The King's Man tt123124 (TV Episode)",
"<a href=\"/title/tt6856242/?ref_=fn_tt_tt_1\">The King's Man<a> (2021)",
];
for fragment in fragments.iter() {
match SearchResult::try_from(*fragment).unwrap_err() {
RunError::NameNotFound(_) => {}
e => panic!("Incorrect error type raised: {:?}", e),
}
}
}
#[test]
fn id_not_found() {
let fragments = [
"The King's Man (Movie)",
"The King's Man 123124 (TV Episode)",
"<a href=\"/title/tta6856242/?ref_=fn_tt_tt_1\">The King's Man</a> (2021)",
];
for fragment in fragments.iter() {
match SearchResult::try_from(*fragment).unwrap_err() {
RunError::ImdbIdNotFound(_) => {}
e => panic!("Incorrect error type raised: {:?}", e),
}
}
}
}