forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
140 lines (124 loc) · 4.07 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
package spotify
import (
"fmt"
"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", c.baseURL, id)
values := url.Values{}
if country != "" {
values.Set("country", country)
}
if locale != "" {
values.Set("locale", locale)
}
if query := values.Encode(); query != "" {
spotifyURL += "?" + query
}
err := c.get(spotifyURL, &cat)
if err != nil {
return cat, err
}
return cat, err
}
// GetCategory gets a single category used to tag items in Spotify
// (on, for example, the Spotify player's Browse tab).
func (c *Client) GetCategory(id string) (Category, error) {
return c.GetCategoryOpt(id, "", "")
}
// GetCategoryPlaylists gets a list of Spotify playlists tagged with a paricular category.
func (c *Client) GetCategoryPlaylists(catID string) (*SimplePlaylistPage, error) {
return c.GetCategoryPlaylistsOpt(catID, nil)
}
// GetCategoryPlaylistsOpt is like GetCategoryPlaylists, but it accepts optional
// arguments.
func (c *Client) GetCategoryPlaylistsOpt(catID string, opt *Options) (*SimplePlaylistPage, error) {
spotifyURL := fmt.Sprintf("%sbrowse/categories/%s/playlists", c.baseURL, 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
}
}
wrapper := struct {
Playlists SimplePlaylistPage `json:"playlists"`
}{}
err := c.get(spotifyURL, &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).
func (c *Client) GetCategories() (*CategoryPage, error) {
return c.GetCategoriesOpt(nil, "")
}
// GetCategoriesOpt is like GetCategories, but it accepts optional parameters.
//
// 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 := c.baseURL + "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
}
wrapper := struct {
Categories CategoryPage `json:"categories"`
}{}
err := c.get(spotifyURL, &wrapper)
if err != nil {
return nil, err
}
return &wrapper.Categories, nil
}