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
//! A TMDB client implementation with request coalescing and client-side rate-limiting.

use std::sync::Arc;

/// The User-Agent is generated from the package name "dim" and the version so "dim/0.x.y.z"
pub const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

/// The base url used to access TMDB;
pub const TMDB_BASE_URL: &str = "https://api.themoviedb.org/3";

mod cache_control;
mod metadata_provider;
mod raw_client;

pub use metadata_provider::{MetadataProviderOf, Movies, TMDBMetadataProvider, TvShows};
use raw_client::{Cast, Genre, GenreList, SearchResponse, TMDBMediaObject, TvEpisodes, TvSeasons};

#[derive(Debug, displaydoc::Display, Clone, thiserror::Error)]
pub(self) enum TMDBClientRequestError {
    /// The body of a response was not value UTF-8.
    InvalidUTF8Body,
    /// the error comes from reqwest.
    ReqwestError(#[from] Arc<reqwest::Error>),
    /// Failed to receive result over channel: {0:?}
    RecvError(#[from] tokio::sync::broadcast::error::RecvError),
    /// Received {status:?} response code: {body:?}
    NonOkResponse {
        status: reqwest::StatusCode,
        body: String,
    },
}

impl TMDBClientRequestError {
    fn reqwest(err: reqwest::Error) -> Self {
        Self::ReqwestError(Arc::new(err))
    }
}

impl From<TMDBClientRequestError> for super::Error {
    fn from(this: TMDBClientRequestError) -> super::Error {
        use TMDBClientRequestError::*;

        match this {
            // We can generalize some errors into common ones from the upstream enum, but some have to
            // do with internal logic and are this mapped into `OtherError`. `OtherError` generally
            // indicates unexpected errors that cannot be handled. These usually mean something broke.
            //
            // FIXME: `InternalError` might be a more appropriate name.
            InvalidUTF8Body | ReqwestError(_) | RecvError(_) => {
                super::Error::OtherError(Arc::new(this))
            }

            NonOkResponse { status, body } => {
                let message = serde_json::from_str::<raw_client::TmdbError>(&body)
                    .map(|x| x.status_message)
                    .unwrap_or(body);

                super::Error::RemoteApiError {
                    code: status.as_u16(),
                    message,
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use chrono::{Datelike, Timelike};

    use super::*;
    use crate::{ExternalEpisode, ExternalMedia, ExternalQuery, ExternalQueryShow, ExternalSeason};

    fn make_letterkenny() -> ExternalMedia {
        let dt = chrono::Utc::now()
            .with_day(7)
            .unwrap()
            .with_month(2)
            .unwrap()
            .with_year(2016)
            .unwrap()
            .with_minute(0)
            .unwrap()
            .with_second(0)
            .unwrap()
            .with_nanosecond(0)
            .unwrap()
            .with_hour(0)
            .unwrap();

        ExternalMedia {
            external_id: "65798".into(),
            title: "Letterkenny".into(),
            description: Some("Letterkenny follows Wayne, a good-ol’ country boy in Letterkenny, Ontario trying to protect his homegrown way of life on the farm, against a world that is constantly evolving around him. The residents of Letterkenny belong to one of three groups: Hicks, Skids, and Hockey Players. The three groups are constantly feuding with each other over seemingly trivial matters; often ending with someone getting their ass kicked.".into()),
            release_date: Some(dt),
            posters: vec!["https://image.tmdb.org/t/p/w600_and_h900_bestv2/yvQGoc9GGTfOyPty5ASShT9tPBD.jpg".into()], 
            backdrops: vec!["https://image.tmdb.org/t/p/original/wdHK7RZNIGfskbGCIusSKN3vto6.jpg".into()], 
            genres: vec!["Comedy".into()], 
            rating: Some(8.0),
            duration: None
        }
    }

    #[tokio::test]
    async fn tmdb_search() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_shows: MetadataProviderOf<TvShows> = provider.tv_shows();

        let mut metadata = provider_shows
            .search("letterkenny", None)
            .await
            .expect("search results should exist");

        let letterkenny = make_letterkenny();

        metadata[0].rating.as_mut().map(|x| *x = 8.0);

        assert_eq!(metadata, vec![letterkenny]);
    }

    #[tokio::test]
    async fn tmdb_get_details() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_shows: MetadataProviderOf<TvShows> = provider.tv_shows();

        let mut media = provider_shows
            .search_by_id("65798")
            .await
            .expect("search results should exist");

        let letterkenny = make_letterkenny();

        media.rating.as_mut().map(|x| *x = 8.0);

        assert_eq!(letterkenny, media);
    }

    #[tokio::test]
    async fn tmdb_get_cast() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_movies: MetadataProviderOf<Movies> = provider.movies();

        let cast = provider_movies
            .cast("335984")
            .await
            .expect("cast should exist");

        assert_eq!(cast[0].external_id, "30614".to_string());
        assert_eq!(cast[0].name, "Ryan Gosling".to_string());
        assert_eq!(
            cast[0].profile_path,
            Some("https://image.tmdb.org/t/p/original/lyUyVARQKhGxaxy0FbPJCQRpiaW.jpg".to_string())
        );
        assert!(matches!(cast[0].character.as_str(), "K" | "\'K\'"));
    }

    #[tokio::test]
    async fn tmdb_get_seasons() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_shows: MetadataProviderOf<TvShows> = provider.tv_shows();

        let mut media = provider_shows
            .seasons_for_id("63639")
            .await
            .expect("search results should exist");

        assert_eq!(media.len(), 7);

        let last = media.pop().unwrap();

        assert_eq!(last.season_number, 6);

        let expected = ExternalSeason {
            external_id: "214858".into(),
            title: Some("Season 6".into()),
            description: Some("Holden and the crew of the Rocinante fight alongside the Combined Fleet of Earth and Mars to protect the Inner Planets from Marco Inaros and his Free Navy's campaign of death and destruction. Meanwhile, on a distant planet beyond the Rings, a new power rises.".into()),
            posters: vec!["https://image.tmdb.org/t/p/w600_and_h900_bestv2/smJPN02aTJcMVQ4z02CINKjg6L0.jpg".into()],
            season_number: 6
        };

        assert_eq!(last, expected);
    }

    #[tokio::test]
    async fn tmdb_get_episodes() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_shows: MetadataProviderOf<TvShows> = provider.tv_shows();

        let mut media = provider_shows
            .episodes_for_season("63639", 3)
            .await
            .expect("search results should exist");

        assert_eq!(media.len(), 13);

        let last = media.pop().unwrap();

        let expected = ExternalEpisode {
            external_id: "1503262".into(), 
            title: Some("Abaddon's Gate".into()),
            description: Some("Holden and his allies must stop Ashford and his team from destroying the Ring, and perhaps all of humanity.".into()),
            episode_number: 13,
            stills: vec!["https://image.tmdb.org/t/p/original/nE5kS7hHGmv3bTGVL1hlsVQKXo4.jpg".into()],
            duration: None
        };

        assert_eq!(last, expected);
    }

    #[tokio::test]
    async fn once_upon_get_year() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_movies: MetadataProviderOf<Movies> = provider.movies();

        let res = provider_movies
            .search_by_id("466272")
            .await
            .expect("movie should exist");

        assert_eq!(res.release_date.unwrap().year(), 2019);
    }

    #[tokio::test]
    async fn johhny_test_seasons() {
        let provider = TMDBMetadataProvider::new("38c372f5bc572c8aadde7a802638534e");
        let provider_shows: MetadataProviderOf<TvShows> = provider.tv_shows();

        provider_shows
            .seasons_for_id("1769")
            .await
            .expect("Failed to get seasons.");
    }

    #[tokio::test]
    async fn deserialize_letterkenny() {
        let body = r#"{"id": 1234,"name": "letter kenny"}"#;
        serde_json::from_str::<TMDBMediaObject>(&body).unwrap();
    }
}