Skip to content

Commit

Permalink
construct a version if they are none delivered (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frooastside authored Jul 11, 2024
1 parent 3aeaf87 commit 3c86033
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/media/anime/episode.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -27,7 +28,7 @@ pub struct EpisodeVersion {
pub original: bool,

#[cfg(feature = "__test_strict")]
variant: crate::StrictValue,
pub(crate) variant: crate::StrictValue,
}

impl EpisodeVersion {
Expand Down Expand Up @@ -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<EpisodeVersion>,

#[cfg(feature = "__test_strict")]
Expand Down Expand Up @@ -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::<Season>(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.
Expand Down Expand Up @@ -247,15 +251,17 @@ impl Episode {
#[async_trait::async_trait]
impl Media for Episode {
async fn from_id(crunchyroll: &Crunchyroll, id: impl AsRef<str> + Send) -> Result<Self> {
Ok(request_media(
let mut episode: Episode = request_media(
crunchyroll.executor.clone(),
format!(
"https://www.crunchyroll.com/content/v2/cms/episodes/{}",
id.as_ref()
),
)
.await?
.remove(0))
.remove(0);
fix_empty_episode_versions(&mut episode);
Ok(episode)
}

async fn __set_executor(&mut self, executor: Arc<Executor>) {
Expand Down
16 changes: 12 additions & 4 deletions src/media/anime/season.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -21,7 +22,7 @@ pub struct SeasonVersion {
pub original: bool,

#[cfg(feature = "__test_strict")]
variant: crate::StrictValue,
pub(crate) variant: crate::StrictValue,
}

impl SeasonVersion {
Expand Down Expand Up @@ -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<SeasonVersion>,

#[cfg(feature = "__test_strict")]
Expand Down Expand Up @@ -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<Episode> = 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.
Expand Down Expand Up @@ -164,15 +170,17 @@ impl Season {
#[async_trait::async_trait]
impl Media for Season {
async fn from_id(crunchyroll: &Crunchyroll, id: impl AsRef<str> + Send) -> Result<Self> {
Ok(request_media(
let mut season: Season = request_media(
crunchyroll.executor.clone(),
format!(
"https://www.crunchyroll.com/content/v2/cms/seasons/{}",
id.as_ref()
),
)
.await?
.remove(0))
.remove(0);
fix_empty_season_versions(&mut season);
Ok(season)
}

async fn __set_executor(&mut self, executor: Arc<Executor>) {
Expand Down
7 changes: 6 additions & 1 deletion src/media/anime/series.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Season> = 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.
Expand Down
38 changes: 38 additions & 0 deletions src/media/anime/util.rs
Original file line number Diff line number Diff line change
@@ -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<S: AsRef<str>>(slug_title: S) -> crate::Locale {
split_locale_from_slug_title(slug_title).1
Expand Down Expand Up @@ -41,3 +46,36 @@ pub(crate) fn real_dedup_vec<T: Clone + Eq>(input: &mut Vec<T>) {
}
*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(),
})
}
}

0 comments on commit 3c86033

Please sign in to comment.