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
264
265
266
267
268
269
270
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn search_by_title_test(){
assert_eq!(search_by_title(String::from("shrek"),
&String::from("21e783b3")).unwrap().Title,
"Shrek");
assert_eq!(search_by_title(String::from("shrek"),
&String::from("21e783b3")).unwrap().Year,
"2001");
}
#[test]
fn search_by_id_test(){
assert_eq!(search_by_id(String::from("tt0126029"),
String::from("21e783b3")).unwrap().Title,
"Shrek");
assert_eq!(search_by_id(String::from("tt0126029"),
String::from("21e783b3")).unwrap().Year,
"2001");
}
#[test]
fn from_title_test(){
let film: Film = Film::from_title(String::from("Shrek"),
String::from("21e783b3")).unwrap();
assert_eq!(film.Title, "Shrek");
assert_eq!(film.Year, "2001");
assert_eq!(film.Runtime, "90 min");
assert!(Film::from_title(String::from("gobbeldygookasdfblu"),
String::from("asdf")).is_err());
}
#[test]
fn from_id_test(){
let film: Film = Film::from_id(String::from("tt0126029"),
String::from("21e783b3")).unwrap();
assert_eq!(film.Title, "Shrek");
assert_eq!(film.Year, "2001");
assert!(Film::from_id(String::from("gobbeldygookasdfblur"),
String::from("asdf")).is_err());
}
#[test]
fn test_search_for(){
println!("Testing search_for");
let results: Vec<Film> = match Film::search_for(String::from("shrek"),
String::from("21e783b3")
){
Ok(x) => x,
Err(e) => panic!("Fucked it!: {}", e)
};
println!("Got past search");
assert_eq!(results[0].Title, "Shrek");
assert_eq!(results[1].Title, "Shrek 2");
}
}
use serde::{Serialize, Deserialize};
use serde_json;
use reqwest;
use custom_error::custom_error;
use regex::Regex;
custom_error!{pub FilmError
FilmNotFound = "No film matching the given criteria was found",
NotEnoughResults = "Not enough results matching that title were found",
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Film{
Title: String,
Year: String,
Runtime: String,
Rated: String,
Released: String,
Genre: String,
Director: String,
Writer: String,
Actors: String,
Plot: String,
Language: String
}
impl Film{
pub fn from_title(title: String, key: String) -> Result<Film, FilmError>{
let film: Film = match search_by_title(title, &key){
Ok(x) => x,
Err(e) => return Err(FilmError::FilmNotFound)
};
Ok(film)
}
pub fn from_id(id: String, key: String) -> Result<Film, FilmError>{
let film: Film = match search_by_id(id, key){
Ok(x) => x,
Err(e) => return Err(FilmError::FilmNotFound)
};
Ok(film)
}
pub fn search_for(title: String,key: String) -> Result<Vec<Film>,FilmError>{
let mut results: Vec<Film> = Vec::new();
let formatter: Regex = Regex::new(r###""Title":"[\w\s]+?""###).unwrap();
println!("Initialised regex");
let mut data = reqwest::get(
&format!("http://www.omdbapi.com/?apikey={}&s={}", key, title)[..]
).unwrap();
println!("Got data");
let text = data.text().unwrap();
println!("Got text from data:\n{}\n", text);
let captures = formatter.find_iter(&text);
for i in captures{
let x = &i.as_str().chars().count();
let title = &i.as_str()[9..(*x - 1)];
results.push(Film::from_title(String::from(title),
String::from("21e783b3"))?);
}
Ok(results)
}
pub fn get_title(&self) -> String{ String::from(&self.Title) }
pub fn get_year(&self) -> String{ String::from(&self.Year) }
pub fn get_runtime(&self) -> String{ String::from(&self.Runtime) }
pub fn get_rated(&self) -> String{ String::from(&self.Rated) }
pub fn get_released(&self) -> String{ String::from(&self.Released) }
pub fn get_genre(&self) -> String{ String::from(&self.Genre) }
pub fn get_director(&self) -> String{ String::from(&self.Director) }
pub fn get_writer(&self) -> String{ String::from(&self.Writer) }
pub fn get_actors(&self) -> String{ String::from(&self.Actors) }
pub fn get_plot(&self) -> String{ String::from(&self.Plot) }
pub fn get_language(&self) -> String{ String::from(&self.Language) }
}
fn search_by_title(title:String, key:&String) -> Result<Film, serde_json::Error>{
let mut data = reqwest::get(
&format!("http://www.omdbapi.com/?apikey={}&t={}", key, title)[..]
).unwrap();
return match serde_json::from_str(&data.text().unwrap()){
Ok(x) => Ok(x),
Err(e) => Err(e)
}
}
fn search_by_id(id: String, key: String) -> Result<Film, serde_json::Error>{
let mut data = reqwest::get(
&format!("http://www.omdbapi.com/?apikey={}&i={}", key, id)[..]
).unwrap();
return match serde_json::from_str(&data.text().unwrap()){
Ok(x) => Ok(x),
Err(e) => Err(e)
}
}