Skip to content

Commit

Permalink
Update wallpaper endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Oct 6, 2024
1 parent dd3326a commit 932843d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Account {
pub created: DateTime<Utc>,

pub avatar: String,
#[serde(deserialize_with = "crate::internal::serde::deserialize_wallpaper_from_id")]
pub wallpaper: Wallpaper,

pub account_id: String,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -268,32 +269,37 @@ 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>,
}

/// 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)]
#[request(executor(items))]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
struct WallpaperResult {
items: Vec<Wallpaper>,
}

impl From<String> for Wallpaper {
fn from(s: String) -> Self {
Self { name: s }
}
items: Vec<WallpaperCollection>,
}

impl Wallpaper {
/// Returns all available wallpapers
pub async fn all_wallpapers(crunchyroll: &Crunchyroll) -> Result<Vec<Wallpaper>> {
let endpoint = "https://www.crunchyroll.com/assets/v1/wallpaper";
pub async fn all_wallpapers(crunchyroll: &Crunchyroll) -> Result<Vec<WallpaperCollection>> {
let endpoint = format!(
"https://www.crunchyroll.com/assets/v2/{}/wallpaper",
crunchyroll.executor.details.locale
);
Ok(crunchyroll
.executor
.get(endpoint)
Expand All @@ -306,15 +312,22 @@ 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
)
}

/// Link to a high resolution image of the wallpaper.
pub fn big_url(&self) -> String {
format!(
"https://static.crunchyroll.com/assets/wallpaper/1920x400/{}",
self.name
self.id
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/internal/serde.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::account::Wallpaper;
use crate::common::Image;
use crate::error::Error;
use crate::{Request, Result};
Expand Down Expand Up @@ -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<Wallpaper, D::Error>
where
D: Deserializer<'de>,
{
let id = String::deserialize(deserializer)?;
Ok(Wallpaper {
id: id.clone(),
title: id,
})
}

0 comments on commit 932843d

Please sign in to comment.