diff options
Diffstat (limited to 'import/src/trakt.rs')
-rw-r--r-- | import/src/trakt.rs | 138 |
1 files changed, 104 insertions, 34 deletions
diff --git a/import/src/trakt.rs b/import/src/trakt.rs index 9674351..0441ad0 100644 --- a/import/src/trakt.rs +++ b/import/src/trakt.rs @@ -1,12 +1,12 @@ use bincode::{Decode, Encode}; use jellybase::cache::async_cache_memory; -use jellycommon::TraktKind; +use jellycommon::{Appearance, ObjectIds, PeopleGroup, Person, TraktKind}; use reqwest::{ header::{HeaderMap, HeaderName, HeaderValue}, Client, ClientBuilder, }; use serde::{Deserialize, Serialize}; -use std::{fmt::Display, sync::Arc}; +use std::{collections::BTreeMap, fmt::Display, sync::Arc}; pub struct Trakt { client: Client, @@ -81,6 +81,49 @@ impl Trakt { ) .await } + + pub async fn people( + &self, + kind: TraktKind, + id: u64, + extended: bool, + ) -> anyhow::Result<Arc<TraktPeople>> { + async_cache_memory( + &["api-trakt-people", &format!("{id} {extended}")], + || async move { + let url = format!( + "https://api.trakt.tv/{}/{}/people{}", + kind.plural(), + id, + optext2(extended) + ); + let res = self.client.get(url).send().await?.error_for_status()?; + Ok(res.json().await?) + }, + ) + .await + } +} + +#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)] +pub struct TraktPeople { + pub cast: Vec<TraktAppearance>, + pub crew: BTreeMap<TraktPeopleGroup, Vec<TraktAppearance>>, +} + +#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)] +pub struct TraktAppearance { + #[serde(default)] + pub jobs: Vec<String>, + #[serde(default)] + pub characters: Vec<String>, + pub person: TraktPerson, +} + +#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)] +pub struct TraktPerson { + pub name: String, + pub ids: ObjectIds, } fn optext(extended: bool) -> &'static str { @@ -130,11 +173,69 @@ impl TraktKindObject { } } +#[derive( + Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, Clone, Copy, +)] +pub enum TraktPeopleGroup { + #[serde(rename = "production")] + Production, + #[serde(rename = "art")] + Art, + #[serde(rename = "crew")] + Crew, + #[serde(rename = "costume & make-up")] + CostumeMakeup, + #[serde(rename = "directing")] + Directing, + #[serde(rename = "writing")] + Writing, + #[serde(rename = "sound")] + Sound, + #[serde(rename = "camera")] + Camera, + #[serde(rename = "visual effects")] + VisualEffects, + #[serde(rename = "lighting")] + Lighting, + #[serde(rename = "editing")] + Editing, +} +impl TraktPeopleGroup { + pub fn a(self) -> PeopleGroup { + match self { + TraktPeopleGroup::Production => PeopleGroup::Production, + TraktPeopleGroup::Art => PeopleGroup::Art, + TraktPeopleGroup::Crew => PeopleGroup::Crew, + TraktPeopleGroup::CostumeMakeup => PeopleGroup::CostumeMakeup, + TraktPeopleGroup::Directing => PeopleGroup::Directing, + TraktPeopleGroup::Writing => PeopleGroup::Writing, + TraktPeopleGroup::Sound => PeopleGroup::Sound, + TraktPeopleGroup::Camera => PeopleGroup::Camera, + TraktPeopleGroup::VisualEffects => PeopleGroup::Vfx, + TraktPeopleGroup::Lighting => PeopleGroup::Lighting, + TraktPeopleGroup::Editing => PeopleGroup::Editing, + } + } +} +impl TraktAppearance { + pub fn a(&self) -> Appearance { + Appearance { + jobs: self.jobs.to_owned(), + characters: self.characters.to_owned(), + person: Person { + name: self.person.name.to_owned(), + asset: None, + ids: self.person.ids.to_owned(), + }, + } + } +} + #[derive(Debug, Serialize, Deserialize, Encode, Decode, Clone)] pub struct TraktMediaObject { pub title: String, pub year: Option<u32>, - pub ids: TraktMediaObjectIds, + pub ids: ObjectIds, pub tagline: Option<String>, pub overview: Option<String>, @@ -152,16 +253,6 @@ pub struct TraktMediaObject { pub genres: Option<Vec<String>>, } -#[derive(Debug, Serialize, Deserialize, Encode, Decode, Clone)] -pub struct TraktMediaObjectIds { - pub trakt: u64, - pub slug: Option<String>, - pub imdb: Option<String>, - pub tmdb: Option<u64>, - pub omdb: Option<u64>, - pub tvdb: Option<u64>, -} - impl Display for TraktSearchResult { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!( @@ -173,24 +264,3 @@ impl Display for TraktSearchResult { )) } } -impl Display for TraktMediaObjectIds { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("trakt")?; - if self.slug.is_some() { - f.write_str(",slug")?; - } - if self.tmdb.is_some() { - f.write_str(",tmdb")?; - } - if self.imdb.is_some() { - f.write_str(",imdb")?; - } - if self.tvdb.is_some() { - f.write_str(",tvdb")?; - } - if self.omdb.is_some() { - f.write_str(",omdb")?; - } - Ok(()) - } -} |