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
use futures::future::ready;
use futures::stream::Stream;
use futures::stream::StreamExt;

use crate::Error;
use crate::Title;
use crate::TitleType;
use crate::stream_titles;
use crate::stream_titles_filtered;

pub async fn get_movies_filtered(ids: &[u64]) -> Result<Vec<Movie>, Error> {
	let mut stream = stream_movies_filtered(ids).await?;
	let mut titles = Vec::with_capacity(ids.len());
	while let Some(result) = stream.next().await {
		// This is expanded out to a bunch of lines to facilitate inserting printf debugging
		titles.push(match result {
			Ok(v) => v,
			Err(e) => {
				return Err(e.into())
			}
		});
	}
	Ok(titles)
}

#[derive(Debug)]
pub struct Movie {
	pub imdb_id: u64,
	pub title: String,
	pub is_adult: bool,
	pub year: u16,
	pub runtime_minutes: Option<u16>,
	pub genres: Vec<String>
}

impl Movie {
	pub(crate) fn from_title(input: &Title) -> Option<Result<Self, Error>> {
		match input.title_type {
			TitleType::Movie => Some(Ok(Self{
				imdb_id: input.imdb_id,
				title: input.primary_title.clone(),
				is_adult: input.is_adult != 0,
				year: match input.start_year {
					Some(v) => v,
					None => match input.end_year {
						Some(v) => v,
						None => return Some(Err(Error::YearMissing.into()))
					}
				},
				//year: input.start_year,
				runtime_minutes: input.runtime_minutes,
				genres: input.genres.clone()
			})),
			_ => None
		}
	}

	fn from_wrapped_title(input: Result<Title, Error>) -> Option<Result<Self, Error>> {
		match input {
			Ok(t) => Self::from_title(&t),
			Err(e) => Some(Err(e))
		}
	}
}

pub async fn stream_movies_filtered(ids: &[u64]) -> Result<impl Stream<Item = Result<Movie, Error>> + '_, Error> {
	let stream = stream_titles_filtered(ids).await?;
	Ok(stream.filter_map(|r| ready(Movie::from_wrapped_title(r))))
}

pub async fn stream_movies() -> Result<impl Stream<Item = Result<Movie, Error>>, Error> {
	let stream = stream_titles().await?;
	Ok(stream.filter_map(|r| ready(Movie::from_wrapped_title(r))))
}