Skip to content

Commit

Permalink
construct a version if they are none delivered
Browse files Browse the repository at this point in the history
  • Loading branch information
Frooastside committed Jul 11, 2024
1 parent 3aeaf87 commit f05a3c1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
33 changes: 28 additions & 5 deletions src/media/anime/episode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::common::{Image, Request};
use crate::crunchyroll::Executor;
#[cfg(feature = "__test_strict")]
use crate::internal::strict::StrictValue;
use crate::media::util::request_media;
use crate::media::Media;
use crate::media::{fix_empty_season_versions, Media};
use crate::{Crunchyroll, Locale, Result, Season, Series};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -149,6 +151,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 +209,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 +252,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 Expand Up @@ -294,3 +301,19 @@ impl Media for Episode {
}
}
}

pub 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(),
})
}
}
36 changes: 31 additions & 5 deletions src/media/anime/season.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::common::Request;
use crate::crunchyroll::Executor;
#[cfg(feature = "__test_strict")]
use crate::internal::strict::StrictValue;
use crate::media::util::request_media;
use crate::media::Media;
use crate::media::{fix_empty_episode_versions, Media};
use crate::{Crunchyroll, Episode, Locale, Result, Series};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand All @@ -21,7 +23,7 @@ pub struct SeasonVersion {
pub original: bool,

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

impl SeasonVersion {
Expand Down Expand Up @@ -92,6 +94,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 +128,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 +171,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 Expand Up @@ -218,3 +227,20 @@ impl Media for Season {
}
}
}

pub 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(),
})
}
}
10 changes: 8 additions & 2 deletions src/media/anime/series.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::categories::Category;
use crate::crunchyroll::Executor;
#[cfg(feature = "__test_strict")]
use crate::internal::strict::StrictValue;

Check warning

Code scanning / clippy

unused import: crate::internal::strict::StrictValue Warning

unused import: crate::internal::strict::StrictValue
use crate::media::util::request_media;
use crate::media::{Media, PosterImages};
use crate::media::{fix_empty_season_versions, Media, PosterImages};
use crate::{Crunchyroll, Locale, MusicVideo, Result, Season};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -136,7 +138,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

0 comments on commit f05a3c1

Please sign in to comment.