diff options
Diffstat (limited to 'import/src/lib.rs')
| -rw-r--r-- | import/src/lib.rs | 138 |
1 files changed, 64 insertions, 74 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index d12913f..3cbabdc 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -20,11 +20,10 @@ use crate::{tmdb::TmdbKind, trakt::TraktKind}; use acoustid::{acoustid_fingerprint, AcoustID}; use anyhow::{anyhow, bail, Context, Result}; use infojson::YVideo; -use jellycache::{cache_file, cache_memory}; +use jellycache::{cache, cache_memory, cache_store, CacheKey}; use jellycommon::{ - Appearance, Asset, Chapter, CreditCategory, IdentifierType, LocalTrack, MediaInfo, Node, - NodeID, NodeKind, PictureSlot, RatingType, SourceTrack, SourceTrackKind, TrackSource, - Visibility, + Appearance, Chapter, CreditCategory, IdentifierType, MediaInfo, Node, NodeID, NodeKind, + Picture, PictureSlot, RatingType, SourceTrack, SourceTrackKind, TrackSource, Visibility, }; use jellyimport_fallback_generator::generate_fallback; use jellyremuxer::{ @@ -39,7 +38,7 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, HashMap}, fs::{read_to_string, File}, - io::{self, BufReader, Write}, + io::{BufReader, Read}, path::{Path, PathBuf}, sync::{Arc, LazyLock, Mutex}, time::UNIX_EPOCH, @@ -262,23 +261,27 @@ fn import_file( match filename.as_ref() { "poster.jpeg" | "poster.webp" | "poster.png" => { info!("import poster at {path:?}"); - let path = cache_file("picture-file", path, |mut f| { - io::copy(&mut File::open(path)?, &mut f)?; - Ok(()) + let key = CacheKey::new_image(path); + cache(key, || { + let mut data = Vec::new(); + File::open(path)?.read_to_end(&mut data)?; + Ok(data) })?; db.update_node_init(parent, |node| { - node.pictures.insert(PictureSlot::Poster, Asset(path.0)); + node.pictures.insert(PictureSlot::Cover, Picture(key.0)); Ok(()) })?; } "backdrop.jpeg" | "backdrop.webp" | "backdrop.png" => { info!("import backdrop at {path:?}"); - let path = cache_file("picture-file", path, |mut f| { - io::copy(&mut File::open(path)?, &mut f)?; - Ok(()) + let key = CacheKey::new_image(path); + cache(key, || { + let mut data = Vec::new(); + File::open(path)?.read_to_end(&mut data)?; + Ok(data) })?; db.update_node_init(parent, |node| { - node.pictures.insert(PictureSlot::Backdrop, Asset(path.0)); + node.pictures.insert(PictureSlot::Backdrop, Picture(key.0)); Ok(()) })?; } @@ -353,7 +356,7 @@ fn import_file( } pub fn read_media_metadata(path: &Path) -> Result<Arc<matroska::Segment>> { - cache_memory("mkmeta-v4", path, move || { + cache_memory(CacheKey::new_json(path), move || { let media = File::open(path)?; let mut media = create_demuxer_autodetect(Box::new(media))?.ok_or(anyhow!("media format unknown"))?; @@ -400,9 +403,8 @@ fn import_media_file( .flat_map(|a| &a.files) .find(|a| a.name.starts_with("cover") && a.media_type.starts_with("image/")) .map(|att| { - cache_file("att-cover-v2", path, move |mut file| { - file.write_all(&att.data)?; - Ok(()) + cache_store(CacheKey::new_image(("cover", path)), || { + Ok(att.data.clone()) }) }) .transpose()?; @@ -488,13 +490,13 @@ fn import_media_file( } if iflags.use_acoustid { - let fp = rthandle.block_on(acoustid_fingerprint(path))?; - if let Some((atid, mbid)) = rthandle.block_on( - apis.acoustid - .as_ref() - .ok_or(anyhow!("need acoustid"))? - .get_atid_mbid(&fp), - )? { + let fp = acoustid_fingerprint(path)?; + if let Some((atid, mbid)) = apis + .acoustid + .as_ref() + .ok_or(anyhow!("need acoustid"))? + .get_atid_mbid(&fp, rthandle)? + { eids.insert(IdentifierType::AcoustIdTrack, atid); eids.insert(IdentifierType::MusicbrainzRecording, mbid); }; @@ -517,7 +519,7 @@ fn import_media_file( node.identifiers.extend(eids); if let Some(cover) = cover { - node.pictures.insert(PictureSlot::Cover, Asset(cover.0)); + node.pictures.insert(PictureSlot::Cover, Picture(cover.0)); } if let Some(ct) = tags.get("CONTENT_TYPE") { @@ -664,18 +666,17 @@ fn import_media_file( } if let Some(trakt_id) = trakt_id { let trakt = apis.trakt.as_ref().ok_or(anyhow!("trakt required"))?; - let seasons = rthandle.block_on(trakt.show_seasons(trakt_id))?; + let seasons = trakt.show_seasons(trakt_id, rthandle)?; if seasons.iter().any(|x| x.number == season) { - let episodes = rthandle.block_on(trakt.show_season_episodes(trakt_id, season))?; + let episodes = trakt.show_season_episodes(trakt_id, season, rthandle)?; let mut poster = None; if let Some(tmdb) = &apis.tmdb { - let trakt_details = - rthandle.block_on(trakt.lookup(TraktKind::Show, trakt_id))?; + let trakt_details = trakt.lookup(TraktKind::Show, trakt_id, rthandle)?; if let Some(tmdb_id) = trakt_details.ids.tmdb { let tmdb_details = - rthandle.block_on(tmdb.episode_details(tmdb_id, season, episode))?; + tmdb.episode_details(tmdb_id, season, episode, rthandle)?; if let Some(still) = &tmdb_details.still_path { - poster = Some(Asset(rthandle.block_on(tmdb.image(still))?.0)) + poster = Some(Picture(tmdb.image(still, rthandle)?.0)) } } } @@ -685,7 +686,7 @@ fn import_media_file( node.index = Some(episode.number); node.title = Some(episode.title.clone()); if let Some(poster) = poster { - node.pictures.insert(PictureSlot::Poster, poster); + node.pictures.insert(PictureSlot::Cover, poster); } node.description = episode.overview.clone().or(node.description.clone()); node.ratings.insert(RatingType::Trakt, episode.rating); @@ -768,7 +769,7 @@ fn apply_musicbrainz_recording( node: NodeID, mbid: String, ) -> Result<()> { - let rec = rthandle.block_on(apis.musicbrainz.lookup_recording(mbid))?; + let rec = apis.musicbrainz.lookup_recording(mbid, rthandle)?; db.update_node_init(node, |node| { node.title = Some(rec.title.clone()); @@ -800,8 +801,9 @@ fn apply_musicbrainz_recording( if let Some((note, group)) = a { let artist = rel.artist.as_ref().unwrap(); - let artist = - rthandle.block_on(apis.musicbrainz.lookup_artist(artist.id.clone()))?; + let artist = apis + .musicbrainz + .lookup_artist(artist.id.clone(), rthandle)?; let mut image_1 = None; let mut image_2 = None; @@ -811,13 +813,13 @@ fn apply_musicbrainz_recording( WIKIDATA => { let url = rel.url.as_ref().unwrap().resource.clone(); if let Some(id) = url.strip_prefix("https://www.wikidata.org/wiki/") { - if let Some(filename) = rthandle - .block_on(apis.wikidata.query_image_path(id.to_owned()))? + if let Some(filename) = + apis.wikidata.query_image_path(id.to_owned(), rthandle)? { - let path = rthandle.block_on( - apis.wikimedia_commons.image_by_filename(filename), - )?; - image_1 = Some(Asset(path.0)); + let path = apis + .wikimedia_commons + .image_by_filename(filename, rthandle)?; + image_1 = Some(Picture(path.0)); } } } @@ -825,10 +827,8 @@ fn apply_musicbrainz_recording( let url = rel.url.as_ref().unwrap().resource.clone(); if let Some(id) = url.strip_prefix("https://vgmdb.net/artist/") { let id = id.parse::<u64>().context("parse vgmdb id")?; - if let Some(path) = - rthandle.block_on(apis.vgmdb.get_artist_image(id))? - { - image_2 = Some(Asset(path.0)); + if let Some(path) = apis.vgmdb.get_artist_image(id, rthandle)? { + image_2 = Some(Picture(path.0)); } } } @@ -843,10 +843,9 @@ fn apply_musicbrainz_recording( let headshot = match image_1.or(image_2) { Some(x) => x, - None => Asset( - cache_file("person-headshot-fallback", &artist.sort_name, |mut file| { - generate_fallback(&artist.sort_name, &mut file)?; - Ok(()) + None => Picture( + cache_store(CacheKey::new_image(("fallback", &artist.sort_name)), || { + generate_fallback(&artist.sort_name) })? .0, ), @@ -879,12 +878,8 @@ fn apply_trakt_tmdb( ) -> Result<()> { let trakt_id: u64 = trakt_id.parse().context("parse trakt id")?; if let (Some(trakt), Some(tmdb)) = (&apis.trakt, &apis.tmdb) { - let data = rthandle - .block_on(trakt.lookup(trakt_kind, trakt_id)) - .context("trakt lookup")?; - let people = rthandle - .block_on(trakt.people(trakt_kind, trakt_id)) - .context("trakt people lookup")?; + let data = trakt.lookup(trakt_kind, trakt_id, rthandle)?; + let people = trakt.people(trakt_kind, trakt_id, rthandle)?; let mut people_map = BTreeMap::<CreditCategory, Vec<Appearance>>::new(); for p in people.cast.iter() { @@ -903,29 +898,24 @@ fn apply_trakt_tmdb( let mut backdrop = None; let mut poster = None; if let Some(tmdb_id) = data.ids.tmdb { - let data = rthandle - .block_on(tmdb.details( - match trakt_kind { - TraktKind::Movie => TmdbKind::Movie, - TraktKind::Show => TmdbKind::Tv, - _ => TmdbKind::Movie, - }, - tmdb_id, - )) - .context("tmdb details")?; + let data = tmdb.details( + match trakt_kind { + TraktKind::Movie => TmdbKind::Movie, + TraktKind::Show => TmdbKind::Tv, + _ => TmdbKind::Movie, + }, + tmdb_id, + rthandle, + )?; tmdb_data = Some(data.clone()); if let Some(path) = &data.backdrop_path { - let im = rthandle - .block_on(tmdb.image(path)) - .context("tmdb backdrop image")?; - backdrop = Some(Asset(im.0)); + let im = tmdb.image(path, rthandle).context("tmdb backdrop image")?; + backdrop = Some(Picture(im.0)); } if let Some(path) = &data.poster_path { - let im = rthandle - .block_on(tmdb.image(path)) - .context("tmdb poster image")?; - poster = Some(Asset(im.0)); + let im = tmdb.image(path, rthandle).context("tmdb poster image")?; + poster = Some(Picture(im.0)); } // for p in people_map.values_mut().flatten() { @@ -960,7 +950,7 @@ fn apply_trakt_tmdb( node.ratings.insert(RatingType::Trakt, *rating); } if let Some(poster) = poster { - node.pictures.insert(PictureSlot::Poster, poster); + node.pictures.insert(PictureSlot::Cover, poster); } if let Some(backdrop) = backdrop { node.pictures.insert(PictureSlot::Backdrop, backdrop); |