forked from zmb3/spotify
/
category.go
159 lines (151 loc) · 4.81 KB
/
category.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
package spotify
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// Category is used by Spotify to tag items in. For example, on the Spotify
// player's "Browse" tab.
type Category struct {
// A link to the Web API endpoint returning full details of the category
Endpoint string `json:"href"`
// The category icon, in various sizes
Icons []Image `json:"icons"`
// The Spotify category ID. This isn't a base-62 Spotify ID, its just
// a short string that describes and identifies the category (ie "party").
ID string `json:"id"`
// The name of the category
Name string `json:"name"`
}
// GetCategoryOpt is like GetCategory, but it accepts optional arguments.
// The country parameter is an ISO 3166-1 alpha-2 country code. It can be
// used to ensure that the category exists for a particular country. The
// locale argument is an ISO 639 language code and an ISO 3166-1 alpha-2
// country code, separated by an underscore. It can be used to get the
// category strings in a particular language (for example: "es_MX" means
// get categories in Mexico, returned in Spanish).
//
// This call requries authorization.
func (c *Client) GetCategoryOpt(id, country, locale string) (Category, error) {
cat := Category{}
spotifyURL := fmt.Sprintf("%sbrowse/categories/%s", baseAddress, id)
values := url.Values{}
if country != "" {
values.Set("country", country)
}
if locale != "" {
values.Set("locale", locale)
}
if query := values.Encode(); query != "" {
spotifyURL += "?" + query
}
resp, err := c.http.Get(spotifyURL)
if err != nil {
return cat, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return cat, decodeError(resp.Body)
}
err = json.NewDecoder(resp.Body).Decode(&cat)
return cat, err
}
// GetCategory gets a single category used to tag items in Spotify
// (on, for example, the Spotify player's Browse tab).
// This call requires authorization.
func (c *Client) GetCategory(id string) (Category, error) {
return c.GetCategoryOpt(id, "", "")
}
// GetCategoryPlaylists gets a list of Spotify playlists tagged with a paricular category.
// This call requires authorization.
func (c *Client) GetCategoryPlaylists(catID string) (*SimplePlaylistPage, error) {
return c.GetCategoryPlaylistsOpt(catID, nil)
}
// GetCategoryPlaylistsOpt is like GetCategoryPlaylists, but it accepts optional
// arguments. This call requires authorization.
func (c *Client) GetCategoryPlaylistsOpt(catID string, opt *Options) (*SimplePlaylistPage, error) {
spotifyURL := fmt.Sprintf("%sbrowse/categories/%s/playlists", baseAddress, catID)
if opt != nil {
values := url.Values{}
if opt.Country != nil {
values.Set("country", *opt.Country)
}
if opt.Limit != nil {
values.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Offset != nil {
values.Set("offset", strconv.Itoa(*opt.Offset))
}
if query := values.Encode(); query != "" {
spotifyURL += "?" + query
}
}
resp, err := c.http.Get(spotifyURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, decodeError(resp.Body)
}
wrapper := struct {
Playlists SimplePlaylistPage `json:"playlists"`
}{}
err = json.NewDecoder(resp.Body).Decode(&wrapper)
if err != nil {
return nil, err
}
return &wrapper.Playlists, nil
}
// GetCategories gets a list of categories used to tag items in Spotify
// (on, for example, the Spotify player's "Browse" tab).
// This call requires authorization.
func (c *Client) GetCategories() (*CategoryPage, error) {
return c.GetCategoriesOpt(nil, "")
}
// GetCategoriesOpt is like GetCategories, but it accepts optional parameters.
// This call requires authorization.
//
// The locale option can be used to get the results in a particular language.
// It consists of an ISO 639 language code and an ISO 3166-1 alpha-2 country
// code, separated by an underscore. Specify the empty string to have results
// returned in the Spotify default language (American English).
func (c *Client) GetCategoriesOpt(opt *Options, locale string) (*CategoryPage, error) {
spotifyURL := baseAddress + "browse/categories"
values := url.Values{}
if locale != "" {
values.Set("locale", locale)
}
if opt != nil {
if opt.Country != nil {
values.Set("country", *opt.Country)
}
if opt.Limit != nil {
values.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Offset != nil {
values.Set("offset", strconv.Itoa(*opt.Offset))
}
}
if query := values.Encode(); query != "" {
spotifyURL += "?" + query
}
resp, err := c.http.Get(spotifyURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, decodeError(resp.Body)
}
wrapper := struct {
Categories CategoryPage `json:"categories"`
}{}
err = json.NewDecoder(resp.Body).Decode(&wrapper)
if err != nil {
return nil, err
}
return &wrapper.Categories, nil
}