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 pathsearch.go
193 lines (164 loc) Β· 5.48 KB
/
search.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
package crunchyroll
import (
"encoding/json"
"fmt"
"net/http"
)
// BrowseSortType represents a sort type to sort Crunchyroll.Browse items after.
type BrowseSortType string
const (
BrowseSortPopularity BrowseSortType = "popularity"
BrowseSortNewlyAdded = "newly_added"
BrowseSortAlphabetical = "alphabetical"
)
// BrowseOptions represents options for browsing the crunchyroll catalog.
type BrowseOptions struct {
// Categories specifies the categories of the entries.
Categories []string `param:"categories"`
// IsDubbed specifies whether the entries should be dubbed.
IsDubbed bool `param:"is_dubbed"`
// IsSubbed specifies whether the entries should be subbed.
IsSubbed bool `param:"is_subbed"`
// Simulcast specifies a particular simulcast season by id in which the entries have been aired.
Simulcast string `param:"season_tag"`
// Sort specifies how the entries should be sorted.
Sort BrowseSortType `param:"sort_by"`
// Start specifies the index from which the entries should be returned.
Start uint `param:"start"`
// Type specifies the media type of the entries.
Type MediaType `param:"type"`
}
// Browse browses the crunchyroll catalog filtered by the specified options and returns all found series and movies within the given limit.
func (c *Crunchyroll) Browse(options BrowseOptions, limit uint) (s []*Series, m []*Movie, err error) {
query, err := encodeStructToQueryValues(options)
if err != nil {
return nil, nil, err
}
browseEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/browse?%s&n=%d&locale=%s",
query, limit, c.Locale)
resp, err := c.request(browseEndpoint, 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 'browse' response: %w", err)
}
for _, item := range jsonBody["items"].([]interface{}) {
switch item.(map[string]interface{})["type"] {
case MediaTypeSeries:
series := &Series{
crunchy: c,
}
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
}
s = append(s, series)
case MediaTypeMovie:
movie := &Movie{
crunchy: c,
}
if err := decodeMapToStruct(item, movie); err != nil {
return nil, nil, err
}
m = append(m, movie)
}
}
return s, m, nil
}
// FindVideoByName finds a Video (Season or Movie) by its name.
// Use this in combination with ParseVideoURL and hand over the corresponding results
// to this function.
//
// Deprecated: Use Search instead. The first result sometimes isn't the correct one
// so this function is inaccurate in some cases.
// See https://github.com/ByteDream/crunchyroll-go/issues/22 for more information.
func (c *Crunchyroll) FindVideoByName(seriesName string) (Video, error) {
s, m, err := c.Search(seriesName, 1)
if err != nil {
return nil, err
}
if len(s) > 0 {
return s[0], nil
} else if len(m) > 0 {
return m[0], nil
}
return nil, fmt.Errorf("no series or movie could be found")
}
// FindEpisodeByName finds an episode by its crunchyroll series name and episode title.
// Use this in combination with ParseEpisodeURL and hand over the corresponding results
// to this function.
func (c *Crunchyroll) FindEpisodeByName(seriesName, episodeTitle string) ([]*Episode, error) {
series, _, err := c.Search(seriesName, 5)
if err != nil {
return nil, err
}
var matchingEpisodes []*Episode
for _, s := range series {
seasons, err := s.Seasons()
if err != nil {
return nil, err
}
for _, season := range seasons {
episodes, err := season.Episodes()
if err != nil {
return nil, err
}
for _, episode := range episodes {
if episode.SlugTitle == episodeTitle {
matchingEpisodes = append(matchingEpisodes, episode)
}
}
}
}
return matchingEpisodes, nil
}
// Search searches a query and returns all found series and movies within the given limit.
func (c *Crunchyroll) Search(query string, limit uint) (s []*Series, m []*Movie, err error) {
searchEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/search?q=%s&n=%d&type=&locale=%s",
query, limit, c.Locale)
resp, err := c.request(searchEndpoint, 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 'search' response: %w", err)
}
for _, item := range jsonBody["items"].([]interface{}) {
item := item.(map[string]interface{})
if item["total"].(float64) > 0 {
switch item["type"] {
case MediaTypeSeries:
for _, series := range item["items"].([]interface{}) {
series2 := &Series{
crunchy: c,
}
if err := decodeMapToStruct(series, series2); err != nil {
return nil, nil, err
}
if err := decodeMapToStruct(series.(map[string]interface{})["series_metadata"].(map[string]interface{}), series2); err != nil {
return nil, nil, err
}
s = append(s, series2)
}
case MediaTypeMovie:
for _, movie := range item["items"].([]interface{}) {
movie2 := &Movie{
crunchy: c,
}
if err := decodeMapToStruct(movie, movie2); err != nil {
return nil, nil, err
}
m = append(m, movie2)
}
}
}
}
return s, m, nil
}