ontv 0.1.3

A rich desktop application for tracking tv shows
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::collections::BTreeSet;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, bail, Context, Result};
use chrono::{DateTime, NaiveDate, Utc};
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use relative_path::RelativePath;
use reqwest::header;
use reqwest::StatusCode;
use reqwest::{Method, RequestBuilder, Response, Url};
use serde::de::DeserializeOwned;
use serde::Deserialize;

use crate::api::common;
use crate::model::*;
use crate::service::{NewEpisode, UpdateSeries};

const BASE_URL: &str = "https://api.themoviedb.org/3";
const IMAGE_URL: &str = "https://image.tmdb.org";
const IDLE_TIMEOUT: Duration = Duration::from_secs(10);
const PARALLELISM: usize = 10;

struct State {
    base_url: Url,
    image_url: Url,
}

#[derive(Clone)]
pub(crate) struct Client {
    state: Arc<State>,
    client: reqwest::Client,
    api_key: Arc<str>,
}

impl Client {
    /// Construct a new client wrapping the given api key.
    pub(crate) fn new<S>(api_key: &S) -> Result<Self>
    where
        S: ?Sized + AsRef<str>,
    {
        Ok(Self {
            state: Arc::new(State {
                base_url: Url::parse(BASE_URL).expect("illegal base url"),
                image_url: Url::parse(IMAGE_URL).expect("illegal artworks url"),
            }),
            client: reqwest::ClientBuilder::new()
                .pool_idle_timeout(IDLE_TIMEOUT)
                .build()?,
            api_key: api_key.as_ref().into(),
        })
    }

    /// Set API key to the given value.
    pub(crate) fn set_api_key<S>(&mut self, api_key: &S)
    where
        S: ?Sized + AsRef<str>,
    {
        self.api_key = api_key.as_ref().into();
    }

    fn request<I>(&self, method: Method, segments: I) -> RequestBuilder
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        let mut url = self.state.base_url.clone();

        if let Ok(mut m) = url.path_segments_mut() {
            m.extend(segments);
        }

        self.client
            .request(method, url)
            .header(reqwest::header::CONTENT_TYPE, "application/json")
    }

    /// Request with (hopefully cached) authorization.
    fn request_with_auth<I>(&self, method: Method, segments: I) -> RequestBuilder
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        self.request(method, segments)
            .query(&[("api_key", self.api_key.as_ref())])
    }

    /// Search series result.
    pub(crate) async fn search_series(&self, query: &str) -> Result<Vec<SearchSeries>> {
        let res = self
            .request_with_auth(Method::GET, &["search", "tv"])
            .query(&[&("query", query)])
            .send()
            .await?;

        let data: Data<Vec<Row>> = response("search/tv", res).await?;
        let mut output = Vec::with_capacity(data.results.len());

        for row in data.results {
            let first_aired = match row.first_air_date {
                Some(first_aired) if !first_aired.is_empty() => Some(str::parse(&first_aired)?),
                _ => None,
            };

            let poster = row.poster_path.as_deref().and_then(ImageV2::tmdb);

            output.push(SearchSeries {
                id: RemoteSeriesId::Tmdb { id: row.id },
                name: row.name.unwrap_or_default(),
                poster,
                overview: row.overview.unwrap_or_default(),
                first_aired,
            });
        }

        return Ok(output);

        #[derive(Deserialize)]
        struct Row {
            id: u32,
            #[serde(default)]
            name: Option<String>,
            #[serde(default)]
            overview: Option<String>,
            #[serde(default)]
            poster_path: Option<String>,
            #[serde(default)]
            first_air_date: Option<String>,
        }
    }

    /// Search movies result.
    pub(crate) async fn search_movies(&self, query: &str) -> Result<Vec<SearchMovie>> {
        let res = self
            .request_with_auth(Method::GET, &["search", "movie"])
            .query(&[&("query", query)])
            .send()
            .await?;

        let data: Data<Vec<Row>> = response("search/movie", res).await?;
        let mut output = Vec::with_capacity(data.results.len());

        for row in data.results {
            let poster = row.poster_path.as_deref().and_then(ImageV2::tmdb);

            let release_date = match row.release_date {
                Some(release_date) if !release_date.is_empty() => Some(str::parse(&release_date)?),
                _ => None,
            };

            output.push(SearchMovie {
                id: RemoteMovieId::Tmdb { id: row.id },
                title: row.original_title.unwrap_or_default(),
                poster,
                overview: row.overview.unwrap_or_default(),
                release_date,
            });
        }

        return Ok(output);

        #[derive(Deserialize)]
        struct Row {
            id: u32,
            #[serde(default)]
            original_title: Option<String>,
            #[serde(default)]
            overview: Option<String>,
            #[serde(default)]
            poster_path: Option<String>,
            #[serde(default)]
            release_date: Option<String>,
        }
    }

    /// Download series information.
    pub(crate) async fn series(
        &self,
        id: u32,
        lookup: impl common::LookupSeriesId,
        if_none_match: Option<&Etag>,
    ) -> Result<
        Option<(
            UpdateSeries,
            BTreeSet<RemoteSeriesId>,
            Option<Etag>,
            Option<DateTime<Utc>>,
            Vec<Season>,
        )>,
    > {
        let mut details = self.request_with_auth(Method::GET, &["tv", &id.to_string()]);

        if let Some(etag) = if_none_match {
            details = details.header(header::IF_NONE_MATCH, etag.as_ref());
        }

        let details = details.send().await?;

        if details.status() == StatusCode::NOT_MODIFIED {
            return Ok(None);
        }

        let external_ids = self
            .request_with_auth(Method::GET, &["tv", &id.to_string(), "external_ids"])
            .send()
            .await?;

        let images = self
            .request_with_auth(Method::GET, &["tv", &id.to_string(), "images"])
            .send()
            .await?;

        let last_modified = common::parse_last_modified(&details)?;
        let last_etag = common::parse_etag(&details);

        let (external_ids, details, images) = tokio::try_join!(
            response::<ExternalIds>("tv/{id}/external_ids", external_ids),
            response::<Details>("tv/{id}/details", details),
            response::<Images>("tv/{id}/images", images)
        )?;

        let remote_id = RemoteSeriesId::Tmdb { id: details.id };

        let mut remote_ids = BTreeSet::from([remote_id]);

        for remote_id in external_ids.as_remote_series() {
            remote_ids.insert(remote_id?);
        }

        // Try to lookup the series by known remote ids.
        let id = lookup
            .lookup(remote_ids.iter().copied())
            .unwrap_or_else(SeriesId::random);

        let mut graphics = SeriesGraphics::default();
        graphics.poster = details.poster_path.as_deref().and_then(ImageV2::tmdb);

        for image in images.posters {
            graphics.posters.extend(ImageV2::tmdb(&image.file_path));
        }

        graphics.banner = details.backdrop_path.as_deref().and_then(ImageV2::tmdb);

        for image in images.backdrops {
            graphics.banners.extend(ImageV2::tmdb(&image.file_path));
        }

        let series = UpdateSeries {
            id,
            title: details.name.unwrap_or_default(),
            first_air_date: details.first_air_date,
            overview: details.overview.unwrap_or_default(),
            graphics,
            remote_id,
        };

        let mut seasons = Vec::with_capacity(details.seasons.len());

        for s in details.seasons {
            let mut graphics = SeasonGraphics::default();
            graphics.poster = s.poster_path.as_deref().and_then(ImageV2::tmdb);

            seasons.push(Season {
                number: match s.season_number {
                    Some(n) if n > 0 => SeasonNumber::Number(n),
                    _ => SeasonNumber::Specials,
                },
                air_date: s.air_date,
                name: s.name,
                overview: s.overview.unwrap_or_default(),
                compat_poster: None,
                graphics,
            });
        }

        return Ok(Some((
            series,
            remote_ids,
            last_etag,
            last_modified,
            seasons,
        )));

        #[derive(Deserialize)]
        struct Details {
            id: u32,
            #[serde(default)]
            name: Option<String>,
            #[serde(default)]
            overview: Option<String>,
            #[serde(default)]
            poster_path: Option<String>,
            #[serde(default)]
            backdrop_path: Option<String>,
            #[serde(default)]
            first_air_date: Option<NaiveDate>,
            #[serde(default)]
            seasons: Vec<SeasonDetails>,
        }

        #[derive(Deserialize)]
        struct SeasonDetails {
            season_number: Option<u32>,
            #[serde(default)]
            air_date: Option<NaiveDate>,
            #[serde(default)]
            name: Option<String>,
            #[serde(default)]
            overview: Option<String>,
            #[serde(default)]
            poster_path: Option<String>,
        }
    }

    /// Download episodes.
    pub(crate) async fn download_episodes(
        &self,
        series_id: u32,
        season: SeasonNumber,
        lookup: impl common::LookupEpisodeId,
    ) -> Result<Vec<NewEpisode>> {
        let season_number = match season {
            SeasonNumber::Specials => 0,
            SeasonNumber::Number(n) => n,
        };

        let details = self
            .request_with_auth(
                Method::GET,
                &[
                    "tv",
                    &series_id.to_string(),
                    "season",
                    &season_number.to_string(),
                ],
            )
            .send()
            .await?;

        let details: Details = response("tv/{id}/season/{number}", details).await?;

        let mut episodes = Vec::new();
        let mut output = Vec::with_capacity(details.episodes.len());
        let mut it = details.episodes.into_iter();

        let mut tasks = FuturesUnordered::new();

        let mut index = 0..;

        loop {
            while tasks.len() < PARALLELISM {
                let Some(e) = it.next() else {
                    break;
                };

                let remote_id = RemoteEpisodeId::Tmdb { id: e.id };

                tasks.push(self.download_remote_ids(
                    index.next().context("overflow")?,
                    remote_id,
                    series_id,
                    season_number,
                    e,
                    &lookup,
                ));
            }

            let Some(result) = tasks.next().await else {
                break;
            };

            let d = result?;
            output.push(d);
        }

        output.sort_by(|a, b| a.key.cmp(&b.key));

        for d in output {
            let mut graphics = EpisodeGraphics::default();
            graphics.filename = d.episode.still_path.as_deref().and_then(ImageV2::tmdb);

            let episode = Episode {
                id: d.id,
                name: d.episode.name,
                overview: d.episode.overview.unwrap_or_default(),
                absolute_number: None,
                season,
                number: d.episode.episode_number,
                aired: d.episode.air_date,
                compat_filename: None,
                graphics,
                remote_id: Some(d.remote_id),
            };

            episodes.push(NewEpisode {
                episode,
                remote_ids: d.remote_ids,
            });
        }

        return Ok(episodes);

        #[derive(Deserialize)]
        struct Details {
            #[serde(default)]
            episodes: Vec<EpisodeDetail>,
        }
    }

    async fn download_remote_ids(
        &self,
        index: usize,
        remote_id: RemoteEpisodeId,
        series_id: u32,
        season_number: u32,
        episode: EpisodeDetail,
        lookup: &impl common::LookupEpisodeId,
    ) -> Result<DownloadEpisode> {
        tracing::trace!(
            "downloading remote ids for: series: {series_id}, season: {season_number}, episode: {}",
            episode.episode_number
        );

        let id = lookup.lookup([remote_id]);

        let external_ids = self
            .episode_external_ids(series_id, season_number, episode.episode_number)
            .await?;

        let external_ids = match external_ids {
            Some(external_ids) => external_ids,
            None => {
                tracing::warn!(
                    "missing external ids for: series: {series_id}, season: {season_number}, episode: {}",
                    episode.episode_number
                );

                ExternalIds::default()
            }
        };

        let mut remote_ids = BTreeSet::from([remote_id]);

        for remote_id in external_ids.as_remote_episodes() {
            remote_ids.insert(remote_id?);
        }

        let id = match id {
            Some(id) => id,
            None => lookup
                .lookup(remote_ids.iter().copied())
                .unwrap_or_else(EpisodeId::random),
        };

        Ok(DownloadEpisode {
            key: (episode.episode_number, index),
            remote_ids,
            remote_id,
            id,
            episode,
        })
    }

    /// Get external IDs for an episode.
    async fn episode_external_ids(
        &self,
        season_id: u32,
        season_number: u32,
        episode_number: u32,
    ) -> Result<Option<ExternalIds>> {
        let path = [
            "tv",
            &season_id.to_string(),
            "season",
            &season_number.to_string(),
            "episode",
            &episode_number.to_string(),
            "external_ids",
        ];

        let external_ids = self.request_with_auth(Method::GET, &path).send().await?;

        if external_ids.status() == StatusCode::NOT_FOUND {
            return Ok(None);
        }

        let external_ids = response::<ExternalIds>(
            "tv/{id}/season/{season}/episode/{episode}/external_ids",
            external_ids,
        )
        .await?;
        Ok(Some(external_ids))
    }

    /// Load image data from path.
    pub(crate) async fn download_image_path(&self, path: &RelativePath) -> Result<Vec<u8>> {
        let mut url = self.state.image_url.clone();

        if let Ok(mut segments) = url.path_segments_mut() {
            segments.extend(["t", "p", "original"]);

            for c in path.components() {
                segments.push(c.as_str());
            }
        }

        let res = self.client.get(url).send().await?;

        if !res.status().is_success() {
            bail!("{path}: failed to download image: {}", res.status());
        }

        Ok(res.bytes().await?.to_vec())
    }
}

/// Converting a response from JSON.
async fn response<T>(what: &'static str, res: Response) -> Result<T>
where
    T: DeserializeOwned,
{
    async fn inner<T>(what: &'static str, res: Response) -> Result<T>
    where
        T: DeserializeOwned,
    {
        if !res.status().is_success() {
            bail!("{}: {}", res.status(), res.text().await?);
        }

        let output = res.bytes().await?;

        if tracing::enabled!(tracing::Level::TRACE) {
            let text = String::from_utf8_lossy(&output);
            tracing::trace!("{what}: {text}");
        }

        Ok(serde_json::from_slice(&output)?)
    }

    inner(what, res).await.with_context(|| anyhow!("{what}"))
}

#[derive(Default, Deserialize)]
struct ExternalIds {
    imdb_id: Option<String>,
    tvdb_id: Option<u32>,
}

impl ExternalIds {
    /// Coerce into remote series ids.
    pub(crate) fn as_remote_series(&self) -> impl Iterator<Item = Result<RemoteSeriesId>> {
        let a = self.tvdb_id.map(|id| Ok(RemoteSeriesId::Tvdb { id }));

        let b = self.imdb_id.as_ref().and_then(|id| {
            if id.is_empty() {
                return None;
            }

            let id = match Raw::new(&id).context("imdb id overflow") {
                Ok(id) => id,
                Err(e) => return Some(Err(e)),
            };

            Some(Ok(RemoteSeriesId::Imdb { id }))
        });

        a.into_iter().chain(b)
    }

    /// Coerce into remote episode ids.
    pub(crate) fn as_remote_episodes(&self) -> impl Iterator<Item = Result<RemoteEpisodeId>> {
        let a = self.tvdb_id.map(|id| Ok(RemoteEpisodeId::Tvdb { id }));

        let b = self.imdb_id.as_ref().and_then(|id| {
            if id.is_empty() {
                return None;
            }

            let id = match Raw::new(&id).context("imdb id overflow") {
                Ok(id) => id,
                Err(e) => return Some(Err(e)),
            };

            Some(Ok(RemoteEpisodeId::Imdb { id }))
        });

        a.into_iter().chain(b)
    }
}

#[derive(Deserialize)]
struct Image {
    file_path: String,
}

#[derive(Deserialize)]
struct Images {
    #[serde(default)]
    backdrops: Vec<Image>,
    #[serde(default)]
    posters: Vec<Image>,
}

#[derive(Deserialize)]
struct Data<T> {
    results: T,
}

#[derive(Deserialize)]
struct EpisodeDetail {
    id: u32,
    episode_number: u32,
    #[serde(default)]
    air_date: Option<NaiveDate>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    overview: Option<String>,
    #[serde(default)]
    still_path: Option<String>,
}

struct DownloadEpisode {
    key: (u32, usize),
    remote_ids: BTreeSet<RemoteEpisodeId>,
    remote_id: RemoteEpisodeId,
    id: EpisodeId,
    episode: EpisodeDetail,
}