diff options
Diffstat (limited to 'import/src/lib.rs')
-rw-r--r-- | import/src/lib.rs | 69 |
1 files changed, 53 insertions, 16 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index ab74ecb..d6eb54f 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -40,9 +40,14 @@ use std::{ }; use tmdb::{parse_release_date, tmdb_image}; use tokio::{io::AsyncWriteExt, sync::Semaphore, task::spawn_blocking}; +use trakt::Trakt; static IMPORT_SEM: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(1)); +struct Apis { + trakt: Option<Trakt>, +} + pub async fn import(db: &DataAcid, fed: &Federation) -> anyhow::Result<()> { let permit = IMPORT_SEM.try_acquire()?; @@ -56,8 +61,13 @@ pub async fn import(db: &DataAcid, fed: &Federation) -> anyhow::Result<()> { drop(table); txn.commit()?; } + + let ap = Apis { + trakt: SECRETS.api.trakt.as_ref().map(|key| Trakt::new(key)), + }; + info!("loading sources..."); - import_path(CONF.library_path.clone(), vec![], db, fed) + import_path(CONF.library_path.clone(), vec![], db, fed, &ap) .await .context("indexing")?; info!("removing old nodes..."); @@ -155,11 +165,12 @@ fn compare_index_path(x: &[usize], y: &[usize]) -> Ordering { } #[async_recursion] -pub async fn import_path( +async fn import_path( path: PathBuf, index_path: Vec<usize>, db: &DataAcid, fed: &Federation, + ap: &Apis, ) -> anyhow::Result<()> { if path.is_dir() { let mut children_paths = path @@ -192,6 +203,7 @@ pub async fn import_path( }, db, fed, + ap, ) }) .collect(); @@ -209,7 +221,7 @@ pub async fn import_path( }; for s in opts.sources { - process_source(opts.id.clone(), s, &path, &index_path, db, fed).await?; + process_source(opts.id.clone(), s, &path, &index_path, db, fed, ap).await?; } } Ok(()) @@ -225,6 +237,7 @@ async fn process_source( index_path: &[usize], db: &DataAcid, fed: &Federation, + ap: &Apis, ) -> anyhow::Result<()> { let insert_node = move |id: &str, n: Node| -> anyhow::Result<()> { let txn = db.inner.begin_write()?; @@ -240,6 +253,42 @@ async fn process_source( }; match s { ImportSource::Override(n) => insert_node(&id, n)?, + ImportSource::Trakt { id: tid, kind } => { + let trakt_object = ap + .trakt + .as_ref() + .ok_or(anyhow!("trakt api key is required"))? + .lookup(kind, tid, true) + .await?; + + let mut node = Node::default(); + node.public.title = Some(trakt_object.title.to_owned()); + if let Some(overview) = &trakt_object.overview { + node.public.description = Some(overview.to_owned()) + } + if let Some(tagline) = &trakt_object.tagline { + node.public.tagline = Some(tagline.to_owned()) + } + if let Some(rating) = &trakt_object.rating { + node.public.ratings.insert(Rating::Trakt, *rating); + } + insert_node(&id, node)?; + + if let Some(tid) = trakt_object.ids.tmdb { + let mut index_path = index_path.to_vec(); + index_path.push(1); + process_source( + id, + ImportSource::Tmdb { id: tid }, + path, + &index_path, + db, + fed, + ap, + ) + .await?; + } + } ImportSource::Tmdb { id: tid } => { info!("tmdb lookup {id}"); let key = SECRETS @@ -324,6 +373,7 @@ async fn process_source( index_path, db, fed, + ap, ) .await .context(anyhow!("recursive media import: {:?}", f.path()))?; @@ -624,16 +674,3 @@ async fn cache_federation_asset( ) .await } - -// fn make_ident(s: &str) -> String { -// let mut out = String::new(); -// for s in s.chars() { -// match s { -// 'a'..='z' | '0'..='9' => out.push(s), -// 'A'..='Z' => out.push(s.to_ascii_lowercase()), -// '-' | ' ' | '_' | ':' => out.push('-'), -// _ => (), -// } -// } -// out -// } |