diff options
author | metamuffin <metamuffin@disroot.org> | 2023-12-22 16:01:58 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-12-22 16:01:58 +0100 |
commit | b14f5d3f194aa96d32fa4ced2e7cc5fc705ef22d (patch) | |
tree | 750f5fc3d44bb300cef1552e3106f4dac9498f8e /import/src/lib.rs | |
parent | 80e545d06c4a0f0841d4b40e3aff479ef8d864f9 (diff) | |
download | jellything-b14f5d3f194aa96d32fa4ced2e7cc5fc705ef22d.tar jellything-b14f5d3f194aa96d32fa4ced2e7cc5fc705ef22d.tar.bz2 jellything-b14f5d3f194aa96d32fa4ced2e7cc5fc705ef22d.tar.zst |
rework import system pt. 6: tmdb import
Diffstat (limited to 'import/src/lib.rs')
-rw-r--r-- | import/src/lib.rs | 141 |
1 files changed, 37 insertions, 104 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index e69e203..3f14960 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -28,13 +28,16 @@ use std::{ cmp::Ordering, ffi::OsStr, fs::File, - io::{BufReader, Write}, + io::BufReader, os::unix::prelude::OsStrExt, path::{Path, PathBuf}, sync::{Arc, LazyLock}, }; +use tmdb::tmdb_image; use tokio::{io::AsyncWriteExt, sync::Semaphore, task::spawn_blocking}; +use crate::tmdb::TmdbKind; + static IMPORT_SEM: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(1)); pub async fn import(db: &Database, fed: &Federation) -> anyhow::Result<()> { @@ -183,8 +186,39 @@ async fn process_source( }; match s { ImportSource::Override(n) => insert_node(&id, n)?, - ImportSource::Tmdb { id } => { - todo!() + ImportSource::Tmdb { id: tid } => { + let key = CONF + .tmdb_api_key + .as_ref() + .ok_or(anyhow!("no tmdb api key"))?; + let details = tmdb::tmdb_details(TmdbKind::Movie, tid, key).await?; + + let mut node = Node::default(); + + if let Some(poster) = details.poster_path { + node.private.poster = Some( + async_cache_file( + &["tmdb-asset", "poster", &format!("{tid}")], + |mut f| async move { Ok(f.write_all(&tmdb_image(&poster).await?).await?) }, + ) + .await?, + ); + } + if let Some(backdrop) = details.backdrop_path { + node.private.backdrop = Some( + async_cache_file( + &["tmdb-asset", "backdrop", &format!("{tid}")], + |mut f| async move { Ok(f.write_all(&tmdb_image(&backdrop).await?).await?) }, + ) + .await?, + ); + } + + node.public.tagline = details.tagline; + node.public.title = details.title; + node.public.description = Some(details.overview); + + insert_node(&id, node)?; } ImportSource::Media { location, .. } => { // TODO use ignore options @@ -314,107 +348,6 @@ fn merge_node(x: Node, y: Node) -> Node { } } -// #[async_recursion] -// pub async fn import_path( -// path: PathBuf, -// db: &Database, -// fed: &Federation, -// mut node_path: Vec<String>, -// ) -> anyhow::Result<(Vec<String>, usize)> { -// if path.is_dir() { -// let mpath = path.join("directory.json"); -// let children_paths = path.read_dir()?.map(Result::unwrap).filter_map(|e| { -// if e.path().extension() == Some(&OsStr::from_bytes(b"jelly")) -// || e.metadata().unwrap().is_dir() -// { -// Some(e.path()) -// } else { -// None -// } -// }); -// let identifier = if mpath.exists() { -// path.file_name().unwrap().to_str().unwrap().to_string() -// } else { -// node_path -// .last() -// .cloned() -// .ok_or(anyhow!("non-root node requires parent"))? -// }; - -// node_path.push(identifier.clone()); -// let mut all: FuturesUnordered<_> = children_paths -// .into_iter() -// .map(|p| import_path(p.clone(), db, fed, node_path.clone()).map_err(|e| (p, e))) -// .collect(); -// node_path.pop(); // we will set the dirs path later and need it to not be included - -// let mut children_ids = Vec::new(); -// let mut errs = 0; -// while let Some(k) = all.next().await { -// match k { -// core::result::Result::Ok((els, errs2)) => { -// errs += errs2; -// children_ids.extend(els) -// } -// Err((p, e)) => { -// errs += 1; -// error!("import of {p:?} failed: {e:?}") -// } -// } -// } -// if mpath.exists() { -// let mut node: Node = -// serde_json::from_reader(File::open(mpath).context("metadata missing")?)?; - -// node.public.children = children_ids; -// node.public.path = node_path; -// node.public.id = Some(identifier.to_owned()); -// info!("adding {identifier}"); -// db.node.insert(&identifier, &node)?; -// Ok((vec![identifier], errs)) -// } else { -// Ok((children_ids, errs)) -// } -// } else if path.is_file() { -// info!("loading {path:?}"); -// let datafile = File::open(path.clone()).context("cant load metadata")?; -// let mut node: Node = serde_json::from_reader(datafile).context("invalid metadata")?; -// let identifier = node.private.id.clone().unwrap_or_else(|| { -// path.file_name() -// .unwrap() -// .to_str() -// .unwrap() -// .strip_suffix(".json") -// .unwrap() -// .to_string() -// }); - -// let idents = if let Some(io) = node.private.import.take() { -// let session = fed -// .get_session(&io.host) -// .await -// .context("creating session")?; - -// import_remote(io, db, &session, identifier.clone(), node_path) -// .await -// .context("federated import")? -// } else { -// debug!("adding {identifier}"); -// node.public.path = node_path; -// node.public.id = Some(identifier.to_owned()); -// let did_insert = db.node.insert(&identifier, &node)?.is_none(); -// if did_insert { -// vec![identifier] -// } else { -// vec![] -// } -// }; -// Ok((idents, 0)) -// } else { -// bail!("did somebody really put a fifo or socket in the library?!") -// } -// } - static SEM_REMOTE_IMPORT: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(16)); #[async_recursion] |