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
/
season.go
135 lines (115 loc) Β· 3.57 KB
/
season.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
package crunchyroll
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
)
// Season contains information about an anime season.
type Season struct {
crunchy *Crunchyroll
children []*Episode
ID string `json:"id"`
ChannelID string `json:"channel_id"`
Title string `json:"title"`
SlugTitle string `json:"slug_title"`
SeriesID string `json:"series_id"`
SeasonNumber int `json:"season_number"`
IsComplete bool `json:"is_complete"`
Description string `json:"description"`
Keywords []string `json:"keywords"`
SeasonTags []string `json:"season_tags"`
IsMature bool `json:"is_mature"`
MatureBlocked bool `json:"mature_blocked"`
IsSubbed bool `json:"is_subbed"`
IsDubbed bool `json:"is_dubbed"`
IsSimulcast bool `json:"is_simulcast"`
SeoTitle string `json:"seo_title"`
SeoDescription string `json:"seo_description"`
AvailabilityNotes string `json:"availability_notes"`
// the locales are always empty, idk why, this may change in the future
AudioLocales []LOCALE
SubtitleLocales []LOCALE
}
// SeasonFromID returns a season by its api id.
func SeasonFromID(crunchy *Crunchyroll, id string) (*Season, error) {
resp, err := crunchy.Client.Get(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/seasons?series_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))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
season := &Season{
crunchy: crunchy,
ID: id,
}
if err := decodeMapToStruct(jsonBody, season); err != nil {
return nil, err
}
return season, nil
}
// AudioLocale returns the audio locale of the season.
// Will fail if no streams are available, thus use Season.Available
// to prevent any misleading errors.
func (s *Season) AudioLocale() (LOCALE, error) {
episodes, err := s.Episodes()
if err != nil {
return "", err
}
return episodes[0].AudioLocale()
}
// Available returns if downloadable streams for this season are available.
func (s *Season) Available() (bool, error) {
episodes, err := s.Episodes()
if err != nil {
return false, err
}
return episodes[0].Available(), nil
}
// Episodes returns all episodes which are available for the season.
func (s *Season) Episodes() (episodes []*Episode, 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/episodes?season_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{}) {
episode := &Episode{
crunchy: s.crunchy,
}
if err = decodeMapToStruct(item, episode); err != nil {
return nil, err
}
if episode.Playback != "" {
streamHref := item.(map[string]interface{})["__links__"].(map[string]interface{})["streams"].(map[string]interface{})["href"].(string)
if match := regexp.MustCompile(`(?m)(\w+)/streams$`).FindAllStringSubmatch(streamHref, -1); len(match) > 0 {
episode.StreamID = match[0][1]
} else {
fmt.Println()
}
}
episodes = append(episodes, episode)
}
if s.crunchy.cache {
s.children = episodes
}
return
}