diff options
author | metamuffin <metamuffin@disroot.org> | 2024-01-23 04:01:14 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-01-23 04:01:14 +0100 |
commit | 0318a9f925f2507200a5b9b714f20560b770d7b0 (patch) | |
tree | c05c066b4e8c09409ebc5bbd87556e2767922f06 /import/src/tmdb.rs | |
parent | df7b37c2f5a7d7ace4e831595ae691aae816d83a (diff) | |
download | jellything-0318a9f925f2507200a5b9b714f20560b770d7b0.tar jellything-0318a9f925f2507200a5b9b714f20560b770d7b0.tar.bz2 jellything-0318a9f925f2507200a5b9b714f20560b770d7b0.tar.zst |
bug in tmdb date parser and some more error handling
Diffstat (limited to 'import/src/tmdb.rs')
-rw-r--r-- | import/src/tmdb.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/import/src/tmdb.rs b/import/src/tmdb.rs index b651223..c349edf 100644 --- a/import/src/tmdb.rs +++ b/import/src/tmdb.rs @@ -5,7 +5,7 @@ use std::{fmt::Display, sync::Arc}; which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin <metamuffin.org> */ -use anyhow::Context; +use anyhow::{anyhow, bail, Context}; use bincode::{Decode, Encode}; use jellybase::cache::{async_cache_file, async_cache_memory}; use jellycommon::{ @@ -22,6 +22,7 @@ use tokio::io::AsyncWriteExt; pub struct Tmdb { client: Client, + image_client: Client, key: String, } @@ -34,8 +35,10 @@ impl Tmdb { )])) .build() .unwrap(); + let image_client = ClientBuilder::new().build().unwrap(); Self { client, + image_client, key: api_key.to_owned(), } } @@ -99,7 +102,10 @@ impl Tmdb { pub async fn image(&self, path: &str) -> anyhow::Result<AssetLocation> { async_cache_file(&["api-tmdb-image", path], |mut file| async move { info!("downloading image {path:?}"); - let mut res = reqwest::get(&format!("https://image.tmdb.org/t/p/original{path}")) + let mut res = self + .image_client + .get(&format!("https://image.tmdb.org/t/p/original{path}")) + .send() .await? .error_for_status()?; while let Some(chunk) = res.chunk().await? { @@ -111,7 +117,12 @@ impl Tmdb { } } -pub fn parse_release_date(d: &str) -> anyhow::Result<i64> { +pub fn parse_release_date(d: &str) -> anyhow::Result<Option<i64>> { + if d.is_empty() { + return Ok(None); + } else if d.len() < 10 { + bail!(anyhow!("date string too short")) + } let (year, month, day) = (&d[0..4], &d[5..7], &d[8..10]); let (year, month, day) = ( year.parse().context("parsing year")?, @@ -127,7 +138,7 @@ pub fn parse_release_date(d: &str) -> anyhow::Result<i64> { p.hour_mod_12 = Some(0); p.minute = Some(0); p.second = Some(0); - Ok(p.to_datetime_with_timezone(&Utc)?.timestamp_millis()) + Ok(Some(p.to_datetime_with_timezone(&Utc)?.timestamp_millis())) } #[derive(Debug, Clone, Deserialize, Encode, Decode)] |