From d62c68d3a52103719d980f2bdfd9b88f61cd48a0 Mon Sep 17 00:00:00 2001 From: Simon Benezan Date: Fri, 12 Jul 2024 01:02:20 +0200 Subject: [PATCH] construct a version if they are none delivered --- src/media/anime/episode.rs | 16 +++++++++++----- src/media/anime/season.rs | 16 ++++++++++++---- src/media/anime/series.rs | 7 ++++++- src/media/anime/util.rs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/media/anime/episode.rs b/src/media/anime/episode.rs index e7737e9..b1df434 100644 --- a/src/media/anime/episode.rs +++ b/src/media/anime/episode.rs @@ -1,5 +1,6 @@ use crate::common::{Image, Request}; use crate::crunchyroll::Executor; +use crate::media::anime::util::{fix_empty_episode_versions, fix_empty_season_versions}; use crate::media::util::request_media; use crate::media::Media; use crate::{Crunchyroll, Locale, Result, Season, Series}; @@ -27,7 +28,7 @@ pub struct EpisodeVersion { pub original: bool, #[cfg(feature = "__test_strict")] - variant: crate::StrictValue, + pub(crate) variant: crate::StrictValue, } impl EpisodeVersion { @@ -149,6 +150,7 @@ pub struct Episode { pub eligible_region: String, /// All versions of this episode (same episode but each entry has a different language). + #[serde(deserialize_with = "crate::internal::serde::deserialize_maybe_null_to_default")] pub versions: Vec, #[cfg(feature = "__test_strict")] @@ -206,9 +208,11 @@ impl Episode { "https://www.crunchyroll.com/content/v2/cms/seasons/{}", self.season_id ); - Ok(request_media(self.executor.clone(), endpoint) + let mut season: Season = request_media::(self.executor.clone(), endpoint) .await? - .remove(0)) + .remove(0); + fix_empty_season_versions(&mut season); + Ok(season) } /// Show in which audios this [`Episode`] is also available. @@ -247,7 +251,7 @@ impl Episode { #[async_trait::async_trait] impl Media for Episode { async fn from_id(crunchyroll: &Crunchyroll, id: impl AsRef + Send) -> Result { - Ok(request_media( + let mut episode: Episode = request_media( crunchyroll.executor.clone(), format!( "https://www.crunchyroll.com/content/v2/cms/episodes/{}", @@ -255,7 +259,9 @@ impl Media for Episode { ), ) .await? - .remove(0)) + .remove(0); + fix_empty_episode_versions(&mut episode); + Ok(episode) } async fn __set_executor(&mut self, executor: Arc) { diff --git a/src/media/anime/season.rs b/src/media/anime/season.rs index 64b537f..ce03320 100644 --- a/src/media/anime/season.rs +++ b/src/media/anime/season.rs @@ -1,5 +1,6 @@ use crate::common::Request; use crate::crunchyroll::Executor; +use crate::media::anime::util::{fix_empty_episode_versions, fix_empty_season_versions}; use crate::media::util::request_media; use crate::media::Media; use crate::{Crunchyroll, Episode, Locale, Result, Series}; @@ -21,7 +22,7 @@ pub struct SeasonVersion { pub original: bool, #[cfg(feature = "__test_strict")] - variant: crate::StrictValue, + pub(crate) variant: crate::StrictValue, } impl SeasonVersion { @@ -92,6 +93,7 @@ pub struct Season { pub availability_notes: String, /// All versions of this season (same season but each entry has a different language). + #[serde(deserialize_with = "crate::internal::serde::deserialize_maybe_null_to_default")] pub versions: Vec, #[cfg(feature = "__test_strict")] @@ -125,7 +127,11 @@ impl Season { "https://www.crunchyroll.com/content/v2/cms/seasons/{}/episodes", self.id ); - request_media(self.executor.clone(), endpoint).await + let mut episodes: Vec = request_media(self.executor.clone(), endpoint).await?; + for episode in &mut episodes { + fix_empty_episode_versions(episode); + } + Ok(episodes) } /// Show in which audios this [`Season`] is also available. @@ -164,7 +170,7 @@ impl Season { #[async_trait::async_trait] impl Media for Season { async fn from_id(crunchyroll: &Crunchyroll, id: impl AsRef + Send) -> Result { - Ok(request_media( + let mut season: Season = request_media( crunchyroll.executor.clone(), format!( "https://www.crunchyroll.com/content/v2/cms/seasons/{}", @@ -172,7 +178,9 @@ impl Media for Season { ), ) .await? - .remove(0)) + .remove(0); + fix_empty_season_versions(&mut season); + Ok(season) } async fn __set_executor(&mut self, executor: Arc) { diff --git a/src/media/anime/series.rs b/src/media/anime/series.rs index e52da09..959d09b 100644 --- a/src/media/anime/series.rs +++ b/src/media/anime/series.rs @@ -1,5 +1,6 @@ use crate::categories::Category; use crate::crunchyroll::Executor; +use crate::media::anime::util::fix_empty_season_versions; use crate::media::util::request_media; use crate::media::{Media, PosterImages}; use crate::{Crunchyroll, Locale, MusicVideo, Result, Season}; @@ -136,7 +137,11 @@ impl Series { "https://www.crunchyroll.com/content/v2/cms/series/{}/seasons", self.id ); - request_media(self.executor.clone(), endpoint).await + let mut seasons: Vec = request_media(self.executor.clone(), endpoint).await?; + for season in &mut seasons { + fix_empty_season_versions(season); + } + Ok(seasons) } /// Get music videos which are related to this series. diff --git a/src/media/anime/util.rs b/src/media/anime/util.rs index 86c6334..d1ba722 100644 --- a/src/media/anime/util.rs +++ b/src/media/anime/util.rs @@ -1,3 +1,8 @@ +#[cfg(feature = "__test_strict")] +use crate::internal::strict::StrictValue; +use crate::media::{EpisodeVersion, SeasonVersion}; +use crate::{Episode, Locale, Season}; + #[cfg(feature = "experimental-stabilizations")] pub(crate) fn parse_locale_from_slug_title>(slug_title: S) -> crate::Locale { split_locale_from_slug_title(slug_title).1 @@ -41,3 +46,36 @@ pub(crate) fn real_dedup_vec(input: &mut Vec) { } *input = dedup } + +pub(crate) fn fix_empty_season_versions(season: &mut Season) { + if season.versions.is_empty() { + season.versions.push(SeasonVersion { + executor: season.executor.clone(), + id: season.id.clone(), + audio_locale: season + .audio_locales + .first() + .unwrap_or(&Locale::ja_JP) + .clone(), + original: true, + #[cfg(feature = "__test_strict")] + variant: StrictValue::default(), + }) + } +} + +pub(crate) fn fix_empty_episode_versions(episode: &mut Episode) { + if episode.versions.is_empty() { + episode.versions.push(EpisodeVersion { + executor: episode.executor.clone(), + id: episode.id.clone(), + media_id: String::new(), + audio_locale: episode.audio_locale.clone(), + season_id: episode.season_id.clone(), + is_premium_only: episode.is_premium_only, + original: true, + #[cfg(feature = "__test_strict")] + variant: StrictValue::default(), + }) + } +}