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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use hyper::{body::HttpBody, Client};
use hyper_tls::HttpsConnector;
use parse_display::Display;
use serde::{Deserialize, Serialize};

use std::fmt;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Status {
    Ok,
    Other(String),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Torrent {
    pub url: String,
    pub hash: String,
    pub quality: String,
    #[serde(rename = "type")]
    pub _type: String,
    pub seeds: u32,
    pub peers: u32,
    pub size: String,
    pub size_bytes: u64,
    pub date_uploaded: String,
    pub date_uploaded_unix: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Actor {
    name: String,
    character_name: String,
    imdb_code: String,
    url_small_image: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Movie {
    pub id: u32,
    pub url: String,
    pub imdb_code: String,
    pub title: String,
    pub title_english: String,
    pub title_long: String,
    pub slug: String,
    pub year: u32,
    pub rating: f32,
    pub runtime: u32,
    pub genres: Vec<String>,
    pub summary: Option<String>,
    pub description_intro: Option<String>,
    pub description_full: String,
    pub synopsis: Option<String>,
    pub yt_trailer_code: String,
    pub language: String,
    pub mpa_rating: String,
    pub background_image: String,
    pub background_image_original: String,
    pub small_cover_image: String,
    pub medium_cover_image: String,
    pub large_cover_image: String,
    pub medium_screenshot_image1: Option<String>,
    pub medium_screenshot_image2: Option<String>,
    pub medium_screenshot_image3: Option<String>,
    pub large_screenshot_image1: Option<String>,
    pub large_screenshot_image2: Option<String>,
    pub large_screenshot_image3: Option<String>,
    pub state: Option<Status>,
    pub torrents: Vec<Torrent>,
    pub date_uploaded: String,
    pub date_uploaded_unix: u64,
    pub download_count: Option<u32>,
    pub like_count: Option<u32>,
    pub cast: Option<Vec<Actor>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MovieDetail {
    pub movie: Movie,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MovieList {
    pub movie_count: u32,
    pub limit: u32,
    pub page_number: u32,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub movies: Vec<Movie>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum Data {
    MovieList(MovieList),
    MovieDetails(MovieDetail),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Response {
    pub status: Status,
    pub status_message: String,
    pub data: Option<Data>,
}

#[derive(Display, Copy, Clone, Debug)]
pub enum Quality {
    #[display("720p")]
    Q720p,
    #[display("1080p")]
    Q1080p,
    #[display("2160p")]
    Q2160p,
    #[display("3D")]
    Q3D,
}

#[derive(Display, Copy, Clone, Debug)]
#[display(style = "snake_case")]
pub enum Sort {
    Title,
    Year,
    Rating,
    Peers,
    Seeds,
    DownloadCount,
    LikeCount,
    DateAdded,
}

#[derive(Display, Copy, Clone, Debug)]
#[display(style = "snake_case")]
pub enum Order {
    Desc,
    Asc,
}

pub trait ApiEndpoint {
    fn get_url(&self) -> String;
}

/// Add a query parameter to the given URL
fn add_query(url: &mut String, name: &str, value: Option<impl fmt::Display>) {
    if let Some(value) = value {
        url.push_str(&format!("{}={}&", name, value));
    }
}

/// Helper to execute an API endpoint
async fn execute(url: &str) -> Result<Data, Box<dyn std::error::Error + Send + Sync>> {
    let https = HttpsConnector::new();
    let client = Client::builder().build::<_, hyper::Body>(https);

    let mut res = client.get(url.parse()?).await?;
    let mut bytes = Vec::new();
    while let Some(next) = res.data().await {
        let chunk = next?;
        bytes.extend(chunk);
    }
    let body = String::from_utf8(bytes)?;
    let response: Response = serde_json::from_str(&body)?;
    if let Status::Other(status) = response.status {
        return Err(format!("{}: {}", status, response.status_message).into());
    }
    let data = response.data.ok_or("Data missing")?;
    Ok(data)
}

#[derive(Clone, Debug, Default)]
pub struct ListMovies<'a> {
    /// The limit of results per page that has been set
    limit: Option<u8>,
    /// Used to see the next page of movies, eg limit=15 and page=2 will show you movies 15-30
    page: Option<u32>,
    /// Used to filter by a given quality
    quality: Option<Quality>,
    /// Used to filter movie by a given minimum IMDb rating
    minimum_rating: Option<u8>,
    /// Used for movie search, matching on: Movie Title/IMDb Code, Actor Name/IMDb Code, Director
    /// Name/IMDb Code
    query_term: Option<&'a str>,
    /// Used to filter by a given genre (See http://www.imdb.com/genre/ for full list)
    genre: Option<&'a str>,
    /// Sorts the results by choosen value
    sort_by: Option<Sort>,
    /// Orders the results by either Ascending or Descending order
    order_by: Option<Order>,
    /// Returns the list with the Rotten Tomatoes rating included
    wirth_rt_ratings: Option<bool>,
}

impl<'a> ListMovies<'a> {
    pub fn new() -> ListMovies<'a> {
        ListMovies::default()
    }

    pub fn limit(&mut self, limit: u8) -> &mut Self {
        assert!(limit > 1 && limit <= 50, "limit out of range");
        self.limit = Some(limit);
        self
    }

    pub fn page(&mut self, page: u32) -> &mut Self {
        assert!(page > 1, "page out of range");
        self.page = Some(page);
        self
    }

    pub fn quality(&mut self, quality: Quality) -> &mut Self {
        self.quality = Some(quality);
        self
    }

    pub fn query_term(&mut self, query_term: &'a str) -> &mut Self {
        self.query_term = Some(query_term);
        self
    }

    pub fn genre(&mut self, genre: &'a str) -> &mut Self {
        self.genre = Some(genre);
        self
    }

    pub fn sort_by(&mut self, sort_by: Sort) -> &mut Self {
        self.sort_by = Some(sort_by);
        self
    }

    pub fn order_by(&mut self, order_by: Order) -> &mut Self {
        self.order_by = Some(order_by);
        self
    }

    pub fn wirth_rt_ratings(&mut self, wirth_rt_ratings: bool) -> &mut Self {
        self.wirth_rt_ratings = Some(wirth_rt_ratings);
        self
    }

    pub async fn execute(&self) -> Result<MovieList, Box<dyn std::error::Error + Send + Sync>> {
        let data = execute(&self.get_url()).await?;
        match data {
            Data::MovieList(movie_list) => Ok(movie_list),
            _ => Err("Wrong data received".into()),
        }
    }
}

impl<'a> ApiEndpoint for ListMovies<'a> {
    fn get_url(&self) -> String {
        let mut url = "https://yts.mx/api/v2/list_movies.json?".to_owned();

        add_query(&mut url, "limit", self.limit);
        add_query(&mut url, "page", self.page);
        add_query(&mut url, "quality", self.quality);
        add_query(&mut url, "minimum_rating", self.minimum_rating);
        add_query(&mut url, "query_term", self.query_term);
        add_query(&mut url, "genre", self.genre);
        add_query(&mut url, "sort_by", self.sort_by);
        add_query(&mut url, "order_by", self.order_by);
        add_query(&mut url, "wirth_rt_ratings", self.wirth_rt_ratings);
        url
    }
}

#[derive(Clone, Debug)]
pub struct MovieDetails {
    /// The ID of the movie
    movie_id: u32,
    /// When set the data returned will include the added image URLs
    with_images: Option<bool>,
    /// When set the data returned will include the added information about the cast
    with_cast: Option<bool>,
}

impl MovieDetails {
    pub fn new(movie_id: u32) -> MovieDetails {
        MovieDetails {
            movie_id,
            with_images: None,
            with_cast: None,
        }
    }

    pub fn with_images(&mut self, with_images: bool) -> &mut Self {
        self.with_images = Some(with_images);
        self
    }

    pub fn with_cast(&mut self, with_cast: bool) -> &mut Self {
        self.with_cast = Some(with_cast);
        self
    }

    pub async fn execute(&self) -> Result<MovieDetail, Box<dyn std::error::Error + Send + Sync>> {
        let data = execute(&self.get_url()).await?;
        match data {
            Data::MovieDetails(movie) => Ok(movie),
            _ => Err("Wrong data received".into()),
        }
    }
}

impl ApiEndpoint for MovieDetails {
    fn get_url(&self) -> String {
        let mut url = "https://yts.mx/api/v2/movie_details.json?".to_owned();
        add_query(&mut url, "movie_id", Some(self.movie_id));
        add_query(&mut url, "with_images", self.with_images);
        add_query(&mut url, "with_cast", self.with_cast);
        url
    }
}

#[deprecated(
    since = "0.2.0",
    note = "Use ListMovies::new().query_term(...).execute() instead"
)]
pub async fn list_movies(
    query_term: &str,
) -> Result<MovieList, Box<dyn std::error::Error + Send + Sync>> {
    ListMovies::new().query_term(query_term).execute().await
}

#[cfg(test)]
mod tests {
    static TEST_DATA: &str = include_str!("test/test.json");

    use super::*;

    #[test]
    fn list_movies_url_build_empty() {
        let url = ListMovies::new().get_url();
        assert_eq!(url, "https://yts.mx/api/v2/list_movies.json?");
    }

    #[test]
    fn list_movies_url_query_term() {
        let url = ListMovies::new().query_term("test").get_url();
        assert_eq!(
            url,
            "https://yts.mx/api/v2/list_movies.json?query_term=test&"
        );
    }

    #[test]
    fn deserialize_test_data() {
        let response: Response = serde_json::from_str(TEST_DATA).unwrap();
        assert_eq!(response.status, Status::Ok);
        assert_eq!(response.status_message, "Query was successful");
        let data = response.data.unwrap();
        let movie_list = match data {
            Data::MovieList(movie_list) => movie_list,
            _ => panic!("Wrong data"),
        };
        assert_eq!(movie_list.movie_count, 10);
        assert_eq!(movie_list.limit, 20);
        assert_eq!(movie_list.page_number, 1);
        assert_eq!(movie_list.movies.len(), 10);
    }

    #[test]
    fn deserialize_empty_test_data() {
        static TEST_DATA: &str = include_str!("test/test_empty.json");
        let response: Response = serde_json::from_str(TEST_DATA).unwrap();
        assert_eq!(response.status, Status::Ok);
        assert_eq!(response.status_message, "Query was successful");
        let data = response.data.unwrap();
        let movie_list = match data {
            Data::MovieList(movie_list) => movie_list,
            _ => panic!("Wrong data"),
        };
        assert_eq!(movie_list.movie_count, 0);
        assert_eq!(movie_list.limit, 20);
        assert_eq!(movie_list.page_number, 1);
        assert_eq!(movie_list.movies.len(), 0);
    }

    #[test]
    fn deserialize_movie_details() {
        static TEST_DATA: &str = include_str!("test/test_movie_details.json");
        let response: Response = serde_json::from_str(TEST_DATA).unwrap();
        assert_eq!(response.status, Status::Ok);
        assert_eq!(response.status_message, "Query was successful");
        let data = response.data.unwrap();
        let movie_details = match data {
            Data::MovieDetails(movie_details) => movie_details,
            _ => panic!("Wrong data"),
        };
        assert_eq!(movie_details.movie.id, 10);
    }

    #[test]
    fn deserialize_movie_details_full() {
        static TEST_DATA: &str = include_str!("test/test_movie_details_full.json");

        let response: Response = serde_json::from_str(TEST_DATA).unwrap();
        assert_eq!(response.status, Status::Ok);
        assert_eq!(response.status_message, "Query was successful");
        let data = response.data.unwrap();
        let movie_details = match data {
            Data::MovieDetails(movie_details) => movie_details,
            _ => panic!("Wrong data"),
        };
        assert_eq!(movie_details.movie.id, 15);
    }
}