diff options
Diffstat (limited to 'import/src/tmdb.rs')
-rw-r--r-- | import/src/tmdb.rs | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/import/src/tmdb.rs b/import/src/tmdb.rs index c38d50e..3780524 100644 --- a/import/src/tmdb.rs +++ b/import/src/tmdb.rs @@ -72,28 +72,47 @@ pub struct TmdbProductionCompany { pub logo_path: Option<String>, } -pub fn tmdb_search(kind: &str, query: &str, key: &str) -> anyhow::Result<TmdbQuery> { +#[derive(Debug, Clone, Copy)] +pub enum TmdbKind { + Tv, + Movie, +} +impl TmdbKind { + pub fn as_str(&self) -> &'static str { + match self { + TmdbKind::Tv => "tv", + TmdbKind::Movie => "movie", + } + } +} + +pub async fn tmdb_search(kind: TmdbKind, query: &str, key: &str) -> anyhow::Result<TmdbQuery> { info!("searching tmdb: {query:?}"); - Ok(reqwest::blocking::get(&format!( - "https://api.themoviedb.org/3/search/{kind}?query={}&api_key={key}", + Ok(reqwest::get(&format!( + "https://api.themoviedb.org/3/search/{}?query={}&api_key={key}", + kind.as_str(), query.replace(" ", "+") - ))? - .json::<TmdbQuery>()?) + )) + .await? + .json::<TmdbQuery>() + .await?) } -pub fn tmdb_details(kind: &str, id: u64, key: &str) -> anyhow::Result<TmdbDetails> { +pub async fn tmdb_details(kind: TmdbKind, id: u64, key: &str) -> anyhow::Result<TmdbDetails> { info!("fetching details: {id:?}"); - Ok(reqwest::blocking::get(&format!( - "https://api.themoviedb.org/3/{kind}/{id}?api_key={key}" - ))? - .json()?) + Ok(reqwest::get(&format!( + "https://api.themoviedb.org/3/{}/{id}?api_key={key}", + kind.as_str() + )) + .await? + .json() + .await?) } -pub fn tmdb_image(path: &str, out: &mut impl Write) -> anyhow::Result<()> { +pub async fn tmdb_image(path: &str) -> anyhow::Result<Vec<u8>> { info!("downloading image {path:?}"); - let mut res = reqwest::blocking::get(&format!("https://image.tmdb.org/t/p/original{path}"))?; - res.copy_to(out)?; - Ok(()) + let res = reqwest::get(&format!("https://image.tmdb.org/t/p/original{path}")).await?; + Ok(res.bytes().await?.to_vec()) } pub fn parse_release_date(d: &str) -> anyhow::Result<DateTime<Utc>> { |