diff --git a/src/account.rs b/src/account.rs index edbb5e7..9632036 100644 --- a/src/account.rs +++ b/src/account.rs @@ -25,6 +25,7 @@ pub struct Account { pub created: DateTime, pub avatar: String, + #[serde(deserialize_with = "crate::internal::serde::deserialize_wallpaper_from_id")] pub wallpaper: Wallpaper, pub account_id: String, @@ -206,7 +207,7 @@ impl Account { let endpoint = "https://www.crunchyroll.com/accounts/v1/me/profile"; self.executor .patch(endpoint) - .json(&json!({"wallpaper": &wallpaper.name})) + .json(&json!({"wallpaper": &wallpaper.id})) .request_raw(true) .await?; self.wallpaper = wallpaper; @@ -268,12 +269,20 @@ mod wallpaper { use crate::{Crunchyroll, Request, Result}; use serde::{Deserialize, Serialize}; + /// A collection of wallpapers under a specific title/topic. + #[derive(Clone, Debug, Default, Deserialize, Serialize, Request)] + #[cfg_attr(not(feature = "__test_strict"), serde(default))] + pub struct WallpaperCollection { + pub title: String, + pub assets: Vec, + } + /// Wallpaper which are shown at the top of your Crunchyroll profile. #[derive(Clone, Debug, Default, Deserialize, Serialize, Request)] - #[serde(from = "String")] #[cfg_attr(not(feature = "__test_strict"), serde(default))] pub struct Wallpaper { - pub name: String, + pub id: String, + pub title: String, } #[derive(Clone, Debug, Deserialize, Serialize, smart_default::SmartDefault, Request)] @@ -281,19 +290,16 @@ mod wallpaper { #[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))] #[cfg_attr(not(feature = "__test_strict"), serde(default))] struct WallpaperResult { - items: Vec, - } - - impl From for Wallpaper { - fn from(s: String) -> Self { - Self { name: s } - } + items: Vec, } impl Wallpaper { /// Returns all available wallpapers - pub async fn all_wallpapers(crunchyroll: &Crunchyroll) -> Result> { - let endpoint = "https://www.crunchyroll.com/assets/v1/wallpaper"; + pub async fn all_wallpapers(crunchyroll: &Crunchyroll) -> Result> { + let endpoint = format!( + "https://www.crunchyroll.com/assets/v2/{}/wallpaper", + crunchyroll.executor.details.locale + ); Ok(crunchyroll .executor .get(endpoint) @@ -306,7 +312,14 @@ mod wallpaper { pub fn tiny_url(&self) -> String { format!( "https://static.crunchyroll.com/assets/wallpaper/360x115/{}", - self.name + self.id + ) + } + + pub fn medium_url(&self) -> String { + format!( + "https://static.crunchyroll.com/assets/wallpaper/720x180/{}", + self.id ) } @@ -314,7 +327,7 @@ mod wallpaper { pub fn big_url(&self) -> String { format!( "https://static.crunchyroll.com/assets/wallpaper/1920x400/{}", - self.name + self.id ) } } diff --git a/src/internal/serde.rs b/src/internal/serde.rs index 7540a6e..535caa9 100644 --- a/src/internal/serde.rs +++ b/src/internal/serde.rs @@ -1,3 +1,4 @@ +use crate::account::Wallpaper; use crate::common::Image; use crate::error::Error; use crate::{Request, Result}; @@ -198,3 +199,14 @@ where ) .map_err(|e| SerdeError::custom(e.to_string())) } + +pub(crate) fn deserialize_wallpaper_from_id<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let id = String::deserialize(deserializer)?; + Ok(Wallpaper { + id: id.clone(), + title: id, + }) +}