This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvideo.go
403 lines (347 loc) Β· 12.2 KB
/
video.go
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package crunchyroll
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type video struct {
ID string `json:"id"`
ExternalID string `json:"external_id"`
Description string `json:"description"`
Title string `json:"title"`
Slug string `json:"slug"`
SlugTitle string `json:"slug_title"`
Images struct {
PosterTall [][]Image `json:"poster_tall"`
PosterWide [][]Image `json:"poster_wide"`
} `json:"images"`
}
// Video is the base for Movie and Season.
type Video interface{}
// Movie contains information about a movie.
type Movie struct {
video
Video
crunchy *Crunchyroll
children []*MovieListing
// not generated when calling MovieFromID.
MovieListingMetadata struct {
AvailabilityNotes string `json:"availability_notes"`
AvailableOffline bool `json:"available_offline"`
DurationMS int `json:"duration_ms"`
ExtendedDescription string `json:"extended_description"`
FirstMovieID string `json:"first_movie_id"`
IsDubbed bool `json:"is_dubbed"`
IsMature bool `json:"is_mature"`
IsPremiumOnly bool `json:"is_premium_only"`
IsSubbed bool `json:"is_subbed"`
MatureRatings []string `json:"mature_ratings"`
MovieReleaseYear int `json:"movie_release_year"`
SubtitleLocales []LOCALE `json:"subtitle_locales"`
} `json:"movie_listing_metadata"`
Playback string `json:"playback"`
PromoDescription string `json:"promo_description"`
PromoTitle string `json:"promo_title"`
SearchMetadata struct {
Score float64 `json:"score"`
}
}
// MovieFromID returns a movie by its api id.
func MovieFromID(crunchy *Crunchyroll, id string) (*Movie, error) {
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/movies/%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
crunchy.Config.Bucket,
id,
crunchy.Locale,
crunchy.Config.Signature,
crunchy.Config.Policy,
crunchy.Config.KeyPairID), http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
movieListing := &Movie{
crunchy: crunchy,
}
movieListing.ID = id
if err = decodeMapToStruct(jsonBody, movieListing); err != nil {
return nil, err
}
return movieListing, nil
}
// MovieListing returns all videos corresponding with the movie.
func (m *Movie) MovieListing() (movieListings []*MovieListing, err error) {
if m.children != nil {
return m.children, nil
}
resp, err := m.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/movies?movie_listing_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
m.crunchy.Config.Bucket,
m.ID,
m.crunchy.Locale,
m.crunchy.Config.Signature,
m.crunchy.Config.Policy,
m.crunchy.Config.KeyPairID), http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
for _, item := range jsonBody["items"].([]interface{}) {
movieListing := &MovieListing{
crunchy: m.crunchy,
}
if err = decodeMapToStruct(item, movieListing); err != nil {
return nil, err
}
movieListings = append(movieListings, movieListing)
}
if m.crunchy.cache {
m.children = movieListings
}
return movieListings, nil
}
// Series contains information about an anime series.
type Series struct {
video
Video
crunchy *Crunchyroll
children []*Season
PromoDescription string `json:"promo_description"`
PromoTitle string `json:"promo_title"`
AvailabilityNotes string `json:"availability_notes"`
EpisodeCount int `json:"episode_count"`
ExtendedDescription string `json:"extended_description"`
IsDubbed bool `json:"is_dubbed"`
IsMature bool `json:"is_mature"`
IsSimulcast bool `json:"is_simulcast"`
IsSubbed bool `json:"is_subbed"`
MatureBlocked bool `json:"mature_blocked"`
MatureRatings []string `json:"mature_ratings"`
SeasonCount int `json:"season_count"`
// not generated when calling SeriesFromID.
SearchMetadata struct {
Score float64 `json:"score"`
}
}
// SeriesFromID returns a series by its api id.
func SeriesFromID(crunchy *Crunchyroll, id string) (*Series, error) {
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/movies?movie_listing_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
crunchy.Config.Bucket,
id,
crunchy.Locale,
crunchy.Config.Signature,
crunchy.Config.Policy,
crunchy.Config.KeyPairID), http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
series := &Series{
crunchy: crunchy,
}
series.ID = id
if err = decodeMapToStruct(jsonBody, series); err != nil {
return nil, err
}
return series, nil
}
// AddToWatchlist adds the current episode to the watchlist.
// Will return an RequestError with the response status code of 409 if the series was already on the watchlist before.
func (s *Series) AddToWatchlist() error {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/watchlist/%s?locale=%s", s.crunchy.Config.AccountID, s.crunchy.Locale)
body, _ := json.Marshal(map[string]string{"content_id": s.ID})
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
_, err = s.crunchy.requestFull(req)
return err
}
// RemoveFromWatchlist removes the current episode from the watchlist.
// Will return an RequestError with the response status code of 404 if the series was not on the watchlist before.
func (s *Series) RemoveFromWatchlist() error {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/watchlist/%s/%s?locale=%s", s.crunchy.Config.AccountID, s.ID, s.crunchy.Locale)
_, err := s.crunchy.request(endpoint, http.MethodDelete)
return err
}
// Similar returns similar series and movies to the current series within the given limit.
func (s *Series) Similar(limit uint) (ss []*Series, m []*Movie, err error) {
similarToEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/%s/similar_to?guid=%s&n=%d&locale=%s",
s.crunchy.Config.AccountID, s.ID, limit, s.crunchy.Locale)
resp, err := s.crunchy.request(similarToEndpoint, http.MethodGet)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, nil, fmt.Errorf("failed to parse 'similar_to' response: %w", err)
}
for _, item := range jsonBody["items"].([]interface{}) {
switch item.(map[string]interface{})["type"] {
case MediaTypeSeries:
series := &Series{
crunchy: s.crunchy,
}
if err := decodeMapToStruct(item, series); err != nil {
return nil, nil, err
}
if err := decodeMapToStruct(item.(map[string]interface{})["series_metadata"].(map[string]interface{}), series); err != nil {
return nil, nil, err
}
ss = append(ss, series)
case MediaTypeMovie:
movie := &Movie{
crunchy: s.crunchy,
}
if err := decodeMapToStruct(item, movie); err != nil {
return nil, nil, err
}
m = append(m, movie)
}
}
return
}
// Seasons returns all seasons of a series.
func (s *Series) Seasons() (seasons []*Season, err error) {
if s.children != nil {
return s.children, nil
}
resp, err := s.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/seasons?series_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
s.crunchy.Config.Bucket,
s.ID,
s.crunchy.Locale,
s.crunchy.Config.Signature,
s.crunchy.Config.Policy,
s.crunchy.Config.KeyPairID), http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
for _, item := range jsonBody["items"].([]interface{}) {
season := &Season{
crunchy: s.crunchy,
}
if err = decodeMapToStruct(item, season); err != nil {
return nil, err
}
seasons = append(seasons, season)
}
if s.crunchy.cache {
s.children = seasons
}
return
}
// Rating returns the series rating.
func (s *Series) Rating() (*Rating, error) {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content-reviews/v2/user/%s/rating/series/%s", s.crunchy.Config.AccountID, s.ID)
resp, err := s.crunchy.request(endpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rating := &Rating{}
json.NewDecoder(resp.Body).Decode(rating)
return rating, nil
}
// ReviewSortType represents a sort type to sort Series.Reviews items after.
type ReviewSortType string
const (
ReviewSortNewest ReviewSortType = "newest"
ReviewSortOldest = "oldest"
ReviewSortHelpful = "helpful"
)
// ReviewOptions represents options for fetching series reviews.
type ReviewOptions struct {
// Sort specifies how the items should be sorted.
Sort ReviewSortType `json:"sort"`
// Filter specified after which the returning items should be filtered.
Filter ReviewRating `json:"filter"`
}
// Reviews returns user reviews for the series.
func (s *Series) Reviews(options ReviewOptions, page uint, size uint) (BulkResult[*UserReview], error) {
options, err := structDefaults(ReviewOptions{Sort: ReviewSortNewest}, options)
if err != nil {
return BulkResult[*UserReview]{}, err
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content-reviews/v2/%s/user/%s/review/series/%s/list?page=%d&page_size=%d&sort=%s&filter=%s", s.crunchy.Locale, s.crunchy.Config.AccountID, s.ID, page, size, options.Sort, options.Filter)
resp, err := s.crunchy.request(endpoint, http.MethodGet)
if err != nil {
return BulkResult[*UserReview]{}, err
}
defer resp.Body.Close()
var result BulkResult[*UserReview]
json.NewDecoder(resp.Body).Decode(&result)
for _, review := range result.Items {
review.crunchy = s.crunchy
review.SeriesID = s.ID
}
return result, nil
}
// Rate rates the current series.
func (s *Series) Rate(rating ReviewRating) error {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content-reviews/v2/en-US/user/%s/review/series/%s", s.crunchy.Config.AccountID, s.ID)
body, _ := json.Marshal(map[string]string{"rating": string(rating)})
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
_, err = s.crunchy.requestFull(req)
return err
}
// CreateReview creates a review for the current series with the logged-in account.
// Will fail if a review is already present. Check Series.HasOwnerReview if the account
// has already written a review. If this is the case, use Series.GetOwnerReview and user
// OwnerReview.Edit to edit the review.
func (s *Series) CreateReview(title, content string, spoiler bool) (*OwnerReview, error) {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content-reviews/v2/en-US/user/%s/review/series/%s", s.crunchy.Config.AccountID, s.ID)
body, _ := json.Marshal(map[string]any{
"title": title,
"body": content,
"spoiler": spoiler,
})
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := s.crunchy.requestFull(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
review := &OwnerReview{}
json.NewDecoder(resp.Body).Decode(review)
review.crunchy = s.crunchy
review.SeriesID = s.ID
return review, nil
}
// GetOwnerReview returns the series review, written by the current logged-in account.
// Returns an error if no review was written yet.
func (s *Series) GetOwnerReview() (*OwnerReview, error) {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content-reviews/v2/en-US/user/%s/review/series/%s", s.crunchy.Config.AccountID, s.ID)
resp, err := s.crunchy.request(endpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
review := &OwnerReview{}
json.NewDecoder(resp.Body).Decode(review)
review.crunchy = s.crunchy
review.SeriesID = s.ID
return review, nil
}
// HasOwnerReview returns if the logged-in account has written a review for the series.
func (s *Series) HasOwnerReview() bool {
_, err := s.GetOwnerReview()
return err == nil
}