1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::common::{BulkResult, CrappyBulkResult};
use crate::error::CrunchyrollError;
use crate::media::MediaType;
use crate::search::{BrowseOptions, BrowseSortType};
use crate::{options, Crunchyroll, Executor, MediaCollection, Request, Result};
use chrono::{DateTime, Utc};
use serde::Deserialize;
use std::sync::Arc;
#[allow(dead_code)]
#[derive(Clone, Debug, Default, Deserialize, Request)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct CuratedFeed {
pub id: String,
pub channel_id: String,
pub title: String,
pub description: String,
pub items: Vec<MediaCollection>,
#[cfg(feature = "__test_strict")]
feed_type: crate::StrictValue,
#[cfg(feature = "__test_strict")]
version: crate::StrictValue,
}
impl CuratedFeed {
pub async fn from_id(crunchy: &Crunchyroll, id: String) -> Result<Self> {
let endpoint = format!(
"https://www.crunchyroll.com/content/v1/curated_feeds/{}",
id
);
crunchy
.executor
.get(endpoint)
.apply_locale_query()
.request()
.await
}
}
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct CarouselFeedImages {
pub landscape_poster: String,
pub portrait_poster: String,
}
#[allow(dead_code)]
#[derive(Clone, Debug, Default, Deserialize, Request)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct CarouselFeed {
pub id: u32,
pub link: String,
pub slug: String,
pub title: String,
pub description: String,
pub button_text: String,
pub images: CarouselFeedImages,
#[cfg(feature = "__test_strict")]
third_party_impression_tracker: crate::StrictValue,
}
impl CarouselFeed {
pub async fn collection_from_id(
crunchy: &Crunchyroll,
id: String,
) -> Result<Vec<CarouselFeed>> {
let endpoint = format!("https://www.crunchyroll.com/content/v1/carousel/{}", id);
Ok(crunchy
.executor
.get(endpoint)
.apply_locale_query()
.request::<BulkResult<CarouselFeed>>()
.await?
.items)
}
}
#[derive(Clone, Debug)]
pub enum HomeFeedType {
CarouselFeed(String),
Series(String),
SimilarTo(String),
CuratedFeed(String),
Recommendation,
UpNext,
Watchlist,
NewsFeed,
Browse(BrowseOptions),
Banner(String, HomeFeedBannerImages),
}
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct HomeFeedBannerImages {
pub mobile_small: String,
pub mobile_large: String,
pub desktop_small: String,
pub desktop_large: String,
}
#[allow(dead_code)]
#[derive(Clone, Debug, Deserialize, smart_default::SmartDefault, Request)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct HomeFeed {
#[serde(skip)]
executor: Arc<Executor>,
pub id: String,
#[serde(deserialize_with = "crate::internal::serde::deserialize_maybe_none_to_option")]
pub source_media_id: Option<String>,
pub title: String,
#[serde(deserialize_with = "crate::internal::serde::deserialize_maybe_none_to_option")]
pub source_media_title: Option<String>,
pub description: String,
pub display_type: String,
pub resource_type: String,
#[serde(rename = "__links__")]
#[serde(default)]
#[serde(deserialize_with = "crate::internal::serde::deserialize_resource")]
resource: String,
link: Option<String>,
banner_images: Option<HomeFeedBannerImages>,
#[cfg(feature = "__test_strict")]
new_window: Option<crate::StrictValue>,
#[cfg(feature = "__test_strict")]
promo_image: Option<crate::StrictValue>,
}
impl HomeFeed {
pub async fn home_feed_type(&self) -> Result<HomeFeedType> {
match self.resource_type.as_str() {
"hero_carousel" => Ok(HomeFeedType::CarouselFeed(
self.id.clone().split('-').last().unwrap().to_string(),
)),
"panel" => Ok(HomeFeedType::Series(self.id.clone())),
"dynamic_collection" => {
if self.resource.contains("recommendations") {
Ok(HomeFeedType::Recommendation)
} else if self.resource.contains("similar_to") {
Ok(HomeFeedType::SimilarTo(self.id.clone()))
} else {
Err(CrunchyrollError::Internal(
format!("invalid dynamic collection type `{}`", self.resource).into(),
))
}
}
"continue_watching" => Ok(HomeFeedType::UpNext),
"dynamic_watchlist" => Ok(HomeFeedType::Watchlist),
"news_feed" => Ok(HomeFeedType::NewsFeed),
"recent_episodes" => Ok(HomeFeedType::Browse(
BrowseOptions::default()
.sort(BrowseSortType::NewlyAdded)
.media_type(MediaType::Custom("episode".to_string())),
)),
"in_feed_banner" => Ok(HomeFeedType::Banner(
self.link.clone().unwrap(),
self.banner_images.clone().unwrap(),
)),
"curated_collection" => Ok(HomeFeedType::CuratedFeed(self.id.clone())),
_ => Err(CrunchyrollError::Internal(
format!("invalid resource type `{}`", self.resource_type).into(),
)),
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Request)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct NewsFeedResult {
pub top_news: BulkResult<NewsFeed>,
pub latest_news: BulkResult<NewsFeed>,
}
#[derive(Clone, Debug, Deserialize, smart_default::SmartDefault, Request)]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct NewsFeed {
pub title: String,
pub description: String,
pub creator: String,
#[default(DateTime::<Utc>::from(std::time::SystemTime::UNIX_EPOCH))]
pub publish_date: DateTime<Utc>,
#[serde(rename = "image")]
pub image_link: String,
#[serde(rename = "link")]
pub news_link: String,
}
#[derive(Clone, Debug, Deserialize, smart_default::SmartDefault, Request)]
#[request(executor(panel))]
#[cfg_attr(feature = "__test_strict", serde(deny_unknown_fields))]
#[cfg_attr(not(feature = "__test_strict"), serde(default))]
pub struct UpNextEntry {
pub new: bool,
pub new_content: bool,
pub playhead: u32,
pub fully_watched: bool,
pub panel: MediaCollection,
}
options! {
HomeFeedOptions;
limit(u32, "n") = Some(20),
start(u32, "start") = None
}
options! {
NewsFeedOptions;
top_limit(u32, "top_news_n") = Some(20),
top_start(u32, "top_news_start") = None,
latest_limit(u32, "latest_news_n") = Some(20),
latest_start(u32, "latest_news_start") = None
}
options! {
RecommendationOptions;
limit(u32, "n") = Some(20),
start(u32, "start") = None
}
options! {
UpNextOptions;
limit(u32, "n") = Some(20),
start(u32, "start") = None
}
impl Crunchyroll {
pub async fn home_feed(&self, options: HomeFeedOptions) -> Result<Vec<HomeFeed>> {
let endpoint = format!(
"https://www.crunchyroll.com/content/v1/{}/home_feed",
self.executor.details.account_id.clone()?
);
Ok(self
.executor
.get(endpoint)
.query(&options.into_query())
.apply_locale_query()
.request::<CrappyBulkResult<HomeFeed>>()
.await?
.items)
}
pub async fn news_feed(&self, options: NewsFeedOptions) -> Result<NewsFeedResult> {
let endpoint = "https://www.crunchyroll.com/content/v1/news_feed";
self.executor
.get(endpoint)
.query(&options.into_query())
.apply_locale_query()
.request()
.await
}
pub async fn recommendations(
&self,
options: RecommendationOptions,
) -> Result<BulkResult<MediaCollection>> {
let endpoint = format!(
"https://www.crunchyroll.com/content/v1/{}/recommendations",
self.executor.details.account_id.clone()?
);
self.executor
.get(endpoint)
.query(&options.into_query())
.apply_locale_query()
.request()
.await
}
pub async fn up_next(&self, options: UpNextOptions) -> Result<BulkResult<UpNextEntry>> {
let endpoint = format!(
"https://www.crunchyroll.com/content/v1/{}/up_next_account",
self.executor.details.account_id.clone()?
);
self.executor
.get(endpoint)
.query(&options.into_query())
.apply_locale_query()
.request()
.await
}
}