aboutsummaryrefslogtreecommitdiff
path: root/import/src/plugins/tmdb.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2026-01-24 23:06:33 +0100
committermetamuffin <metamuffin@disroot.org>2026-01-24 23:06:33 +0100
commit2bcccb18a6cb8bf836f57c3d86f759b19699def2 (patch)
treeef55a10c6d9703677a983b8ca900fb4578a08eb3 /import/src/plugins/tmdb.rs
parentb2e88a8beabf04adc28947cf82996e8692a68b71 (diff)
downloadjellything-2bcccb18a6cb8bf836f57c3d86f759b19699def2.tar
jellything-2bcccb18a6cb8bf836f57c3d86f759b19699def2.tar.bz2
jellything-2bcccb18a6cb8bf836f57c3d86f759b19699def2.tar.zst
cache as object
Diffstat (limited to 'import/src/plugins/tmdb.rs')
-rw-r--r--import/src/plugins/tmdb.rs160
1 files changed, 91 insertions, 69 deletions
diff --git a/import/src/plugins/tmdb.rs b/import/src/plugins/tmdb.rs
index cf50938..ea32c40 100644
--- a/import/src/plugins/tmdb.rs
+++ b/import/src/plugins/tmdb.rs
@@ -5,11 +5,11 @@
*/
use crate::{
USER_AGENT,
- plugins::{PluginContext, ImportPlugin, PluginInfo},
+ plugins::{ImportPlugin, PluginContext, PluginInfo},
};
use anyhow::{Context, Result, anyhow, bail};
use chrono::{Utc, format::Parsed};
-use jellycache::{EscapeKey, HashKey, cache_memory, cache_store};
+use jellycache::{Cache, EscapeKey, HashKey};
use jellycommon::*;
use jellydb::table::RowNum;
use log::info;
@@ -49,95 +49,117 @@ impl Tmdb {
key: api_key.to_owned(),
}
}
- pub fn search(&self, kind: TmdbKind, query: &str, rt: &Handle) -> Result<Arc<TmdbQuery>> {
- cache_memory(
- &format!("ext/tmdb/search/{kind}-{}.json", HashKey(query)),
- move || {
+ pub fn search(
+ &self,
+ cache: &Cache,
+ kind: TmdbKind,
+ query: &str,
+ rt: &Handle,
+ ) -> Result<Arc<TmdbQuery>> {
+ cache
+ .cache_memory(
+ &format!("ext/tmdb/search/{kind}-{}.json", HashKey(query)),
+ move || {
+ rt.block_on(async {
+ info!("searching tmdb: {query:?}");
+ Ok(self
+ .client
+ .get(format!(
+ "https://api.themoviedb.org/3/search/{kind}?query={}?api_key={}",
+ query.replace(" ", "+"),
+ self.key
+ ))
+ .send()
+ .await?
+ .error_for_status()?
+ .json::<TmdbQuery>()
+ .await?)
+ })
+ },
+ )
+ .context("tmdb search")
+ }
+ pub fn details(
+ &self,
+ cache: &Cache,
+ kind: TmdbKind,
+ id: u64,
+ rt: &Handle,
+ ) -> Result<Arc<TmdbDetails>> {
+ cache
+ .cache_memory(&format!("ext/tmdb/details/{kind}-{id}.json"), move || {
rt.block_on(async {
- info!("searching tmdb: {query:?}");
+ info!("fetching details: {id:?}");
Ok(self
.client
.get(format!(
- "https://api.themoviedb.org/3/search/{kind}?query={}?api_key={}",
- query.replace(" ", "+"),
- self.key
+ "https://api.themoviedb.org/3/{kind}/{id}?api_key={}",
+ self.key,
))
.send()
.await?
.error_for_status()?
- .json::<TmdbQuery>()
+ .json()
.await?)
})
- },
- )
- .context("tmdb search")
- }
- pub fn details(&self, kind: TmdbKind, id: u64, rt: &Handle) -> Result<Arc<TmdbDetails>> {
- cache_memory(&format!("ext/tmdb/details/{kind}-{id}.json"), move || {
- rt.block_on(async {
- info!("fetching details: {id:?}");
- Ok(self
- .client
- .get(format!(
- "https://api.themoviedb.org/3/{kind}/{id}?api_key={}",
- self.key,
- ))
- .send()
- .await?
- .error_for_status()?
- .json()
- .await?)
})
- })
- .context("tmdb details")
+ .context("tmdb details")
}
- pub fn person_image(&self, id: u64, rt: &Handle) -> Result<Arc<TmdbPersonImage>> {
- cache_memory(&format!("ext/tmdb/person/images/{id}.json"), move || {
- rt.block_on(async {
- Ok(self
- .client
- .get(format!(
- "https://api.themoviedb.org/3/person/{id}/images?api_key={}",
- self.key,
- ))
- .send()
- .await?
- .error_for_status()?
- .json()
- .await?)
- })
- })
- .context("tmdb person images")
- }
- pub fn image(&self, path: &str, rt: &Handle) -> Result<String> {
- cache_store(
- format!("ext/tmdb/image/{}.image", EscapeKey(path)),
- move || {
+ pub fn person_image(
+ &self,
+ cache: &Cache,
+ id: u64,
+ rt: &Handle,
+ ) -> Result<Arc<TmdbPersonImage>> {
+ cache
+ .cache_memory(&format!("ext/tmdb/person/images/{id}.json"), move || {
rt.block_on(async {
- info!("downloading image {path:?}");
Ok(self
- .image_client
- .get(format!("https://image.tmdb.org/t/p/original{path}"))
+ .client
+ .get(format!(
+ "https://api.themoviedb.org/3/person/{id}/images?api_key={}",
+ self.key,
+ ))
.send()
.await?
.error_for_status()?
- .bytes()
- .await?
- .to_vec())
+ .json()
+ .await?)
})
- },
- )
- .context("tmdb image download")
+ })
+ .context("tmdb person images")
+ }
+ pub fn image(&self, cache: &Cache, path: &str, rt: &Handle) -> Result<String> {
+ cache
+ .store(
+ format!("ext/tmdb/image/{}.image", EscapeKey(path)),
+ move || {
+ rt.block_on(async {
+ info!("downloading image {path:?}");
+ Ok(self
+ .image_client
+ .get(format!("https://image.tmdb.org/t/p/original{path}"))
+ .send()
+ .await?
+ .error_for_status()?
+ .bytes()
+ .await?
+ .to_vec())
+ })
+ },
+ )
+ .context("tmdb image download")
}
pub fn episode_details(
&self,
+ cache: &Cache,
series_id: u64,
season: u64,
episode: u64,
rt: &Handle,
) -> Result<Arc<TmdbEpisode>> {
- cache_memory(&format!("ext/tmdb/episode-details/{series_id}-S{season}-E{episode}.json"), move || {
+ cache.cache_memory(&format!("ext/tmdb/episode-details/{series_id}-S{season}-E{episode}.json"), move || {
rt.block_on(async {
info!("tmdb episode details {series_id} S={season} E={episode}");
Ok(self
@@ -189,17 +211,17 @@ impl Tmdb {
return Ok(());
};
- let details = self.details(tmdb_kind, tmdb_id, ct.rt)?;
+ let details = self.details(&ct.dba.cache, tmdb_kind, tmdb_id, ct.rt)?;
let backdrop = details
.backdrop_path
.as_ref()
- .map(|path| self.image(&path, ct.rt))
+ .map(|path| self.image(&ct.dba.cache, &path, ct.rt))
.transpose()
.context("backdrop image")?;
let poster = details
.poster_path
.as_ref()
- .map(|path| self.image(&path, ct.rt))
+ .map(|path| self.image(&ct.dba.cache, &path, ct.rt))
.transpose()
.context("poster image")?;
@@ -268,11 +290,11 @@ impl Tmdb {
let Some(series_id) = series_id else {
return Ok(());
};
- let details = self.episode_details(series_id, season, episode, ct.rt)?;
+ let details = self.episode_details(&ct.dba.cache, series_id, season, episode, ct.rt)?;
let cover = details
.still_path
.as_ref()
- .map(|path| self.image(&path, ct.rt))
+ .map(|path| self.image(&ct.dba.cache, &path, ct.rt))
.transpose()
.context("still image download")?;
let release_date = parse_release_date(&details.air_date)?;