forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
402 lines (353 loc) · 12.1 KB
/
user.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
package spotify
import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// User contains the basic, publicly available information about a Spotify user.
type User struct {
// The name displayed on the user's profile.
// Note: Spotify currently fails to populate
// this field when querying for a playlist.
DisplayName string `json:"display_name"`
// Known public external URLs for the user.
ExternalURLs map[string]string `json:"external_urls"`
// Information about followers of the user.
Followers Followers `json:"followers"`
// A link to the Web API endpoint for this user.
Endpoint string `json:"href"`
// The Spotify user ID for the user.
ID string `json:"id"`
// The user's profile image.
Images []Image `json:"images"`
// The Spotify URI for the user.
URI URI `json:"uri"`
}
// PrivateUser contains additional information about a user.
// This data is private and requires user authentication.
type PrivateUser struct {
User
// The country of the user, as set in the user's account profile.
// An ISO 3166-1 alpha-2 country code. This field is only available when the
// current user has granted acess to the ScopeUserReadPrivate scope.
Country string `json:"country"`
// The user's email address, as entered by the user when creating their account.
// Note: this email is UNVERIFIED - there is no proof that it actually
// belongs to the user. This field is only available when the current user
// has granted access to the ScopeUserReadEmail scope.
Email string `json:"email"`
// The user's Spotify subscription level: "premium", "free", etc.
// The subscription level "open" can be considered the same as "free".
// This field is only available when the current user has granted access to
// the ScopeUserReadPrivate scope.
Product string `json:"product"`
// The user's date of birth, in the format 'YYYY-MM-DD'. You can use
// the DateLayout constant to convert this to a time.Time value.
// This field is only available when the current user has granted
// access to the ScopeUserReadBirthdate scope.
Birthdate string `json:"birthdate"`
}
// GetUsersPublicProfile gets public profile information about a
// Spotify User. It does not require authentication.
func (c *Client) GetUsersPublicProfile(userID ID) (*User, error) {
spotifyURL := c.baseURL + "users/" + string(userID)
var user User
err := c.get(spotifyURL, &user)
if err != nil {
return nil, err
}
return &user, nil
}
// CurrentUser gets detailed profile information about the
// current user.
//
// Reading the user's email address requires that the application
// has the ScopeUserReadEmail scope. Reading the country, display
// name, profile images, and product subscription level requires
// that the application has the ScopeUserReadPrivate scope.
//
// Warning: The email address in the response will be the address
// that was entered when the user created their spotify account.
// This email address is unverified - do not assume that Spotify has
// checked that the email address actually belongs to the user.
func (c *Client) CurrentUser() (*PrivateUser, error) {
var result PrivateUser
err := c.get(c.baseURL+"me", &result)
if err != nil {
return nil, err
}
return &result, nil
}
// CurrentUsersTracks gets a list of songs saved in the current
// Spotify user's "Your Music" library.
func (c *Client) CurrentUsersTracks() (*SavedTrackPage, error) {
return c.CurrentUsersTracksOpt(nil)
}
// CurrentUsersTracksOpt is like CurrentUsersTracks, but it accepts additional
// options for sorting and filtering the results.
func (c *Client) CurrentUsersTracksOpt(opt *Options) (*SavedTrackPage, error) {
spotifyURL := c.baseURL + "me/tracks"
if opt != nil {
v := url.Values{}
if opt.Country != nil {
v.Set("country", *opt.Country)
}
if opt.Limit != nil {
v.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Offset != nil {
v.Set("offset", strconv.Itoa(*opt.Offset))
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
}
var result SavedTrackPage
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// FollowUser adds the current user as a follower of one or more
// spotify users, identified by their Spotify IDs.
//
// Modifying the lists of artists or users the current user follows
// requires that the application has the ScopeUserFollowModify scope.
func (c *Client) FollowUser(ids ...ID) error {
return c.modifyFollowers("user", true, ids...)
}
// FollowArtist adds the current user as a follower of one or more
// spotify artists, identified by their Spotify IDs.
//
// Modifying the lists of artists or users the current user follows
// requires that the application has the ScopeUserFollowModify scope.
func (c *Client) FollowArtist(ids ...ID) error {
return c.modifyFollowers("artist", true, ids...)
}
// UnfollowUser removes the current user as a follower of one or more
// Spotify users.
//
// Modifying the lists of artists or users the current user follows
// requires that the application has the ScopeUserFollowModify scope.
func (c *Client) UnfollowUser(ids ...ID) error {
return c.modifyFollowers("user", false, ids...)
}
// UnfollowArtist removes the current user as a follower of one or more
// Spotify artists.
//
// Modifying the lists of artists or users the current user follows
// requires that the application has the ScopeUserFollowModify scope.
func (c *Client) UnfollowArtist(ids ...ID) error {
return c.modifyFollowers("artist", false, ids...)
}
// CurrentUserFollows checks to see if the current user is following
// one or more artists or other Spotify Users. This call requires
// ScopeUserFollowRead.
//
// The t argument indicates the type of the IDs, and must be either
// "user" or "artist".
//
// The result is returned as a slice of bool values in the same order
// in which the IDs were specified.
func (c *Client) CurrentUserFollows(t string, ids ...ID) ([]bool, error) {
if l := len(ids); l == 0 || l > 50 {
return nil, errors.New("spotify: UserFollows supports 1 to 50 IDs")
}
if t != "artist" && t != "user" {
return nil, errors.New("spotify: t must be 'artist' or 'user'")
}
spotifyURL := fmt.Sprintf("%sme/following/contains?type=%s&ids=%s",
c.baseURL, t, strings.Join(toStringSlice(ids), ","))
var result []bool
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return result, nil
}
func (c *Client) modifyFollowers(usertype string, follow bool, ids ...ID) error {
if l := len(ids); l == 0 || l > 50 {
return errors.New("spotify: Follow/Unfollow supports 1 to 50 IDs")
}
v := url.Values{}
v.Add("type", usertype)
v.Add("ids", strings.Join(toStringSlice(ids), ","))
spotifyURL := c.baseURL + "me/following?" + v.Encode()
method := "PUT"
if !follow {
method = "DELETE"
}
req, err := http.NewRequest(method, spotifyURL, nil)
if err != nil {
return err
}
err = c.execute(req, nil, http.StatusNoContent)
if err != nil {
return err
}
return nil
}
// CurrentUsersFollowedArtists gets the current user's followed artists.
// This call requires that the user has granted the ScopeUserFollowRead scope.
func (c *Client) CurrentUsersFollowedArtists() (*FullArtistCursorPage, error) {
return c.CurrentUsersFollowedArtistsOpt(-1, "")
}
// CurrentUsersFollowedArtistsOpt is like CurrentUsersFollowedArtists,
// but it accept the optional arguments limit and after. Limit is the
// maximum number of items to return (1 <= limit <= 50), and after is
// the last artist ID retrieved from the previous request. If you don't
// wish to specify either of the parameters, use -1 for limit and the empty
// string for after.
func (c *Client) CurrentUsersFollowedArtistsOpt(limit int, after string) (*FullArtistCursorPage, error) {
spotifyURL := c.baseURL + "me/following"
v := url.Values{}
v.Set("type", "artist")
if limit != -1 {
v.Set("limit", strconv.Itoa(limit))
}
if after != "" {
v.Set("after", after)
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
var result struct {
A FullArtistCursorPage `json:"artists"`
}
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result.A, nil
}
// CurrentUsersAlbums gets a list of albums saved in the current
// Spotify user's "Your Music" library.
func (c *Client) CurrentUsersAlbums() (*SavedAlbumPage, error) {
return c.CurrentUsersAlbumsOpt(nil)
}
// CurrentUsersAlbumsOpt is like CurrentUsersAlbums, but it accepts additional
// options for sorting and filtering the results.
func (c *Client) CurrentUsersAlbumsOpt(opt *Options) (*SavedAlbumPage, error) {
spotifyURL := c.baseURL + "me/albums"
if opt != nil {
v := url.Values{}
if opt.Country != nil {
v.Set("market", *opt.Country)
}
if opt.Limit != nil {
v.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Offset != nil {
v.Set("offset", strconv.Itoa(*opt.Offset))
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
}
var result SavedAlbumPage
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// CurrentUsersPlaylists gets a list of the playlists owned or followed by
// the current spotify user.
//
// Private playlists require the ScopePlaylistReadPrivate scope. Note that
// this scope alone will not return collaborative playlists, even though
// they are always private. In order to retrieve collaborative playlists
// the user must authorize the ScopePlaylistReadCollaborative scope.
func (c *Client) CurrentUsersPlaylists() (*SimplePlaylistPage, error) {
return c.CurrentUsersPlaylistsOpt(nil)
}
// CurrentUsersPlaylistsOpt is like CurrentUsersPlaylists, but it accepts
// additional options for sorting and filtering the results.
func (c *Client) CurrentUsersPlaylistsOpt(opt *Options) (*SimplePlaylistPage, error) {
spotifyURL := c.baseURL + "me/playlists"
if opt != nil {
v := url.Values{}
if opt.Limit != nil {
v.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Offset != nil {
v.Set("offset", strconv.Itoa(*opt.Offset))
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
}
var result SimplePlaylistPage
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// CurrentUsersTopArtistsOpt gets a list of the top played artists in a given time
// range of the current Spotify user. It supports up to 50 artists in a single
// call. This call requires ScopeUserTopRead.
func (c *Client) CurrentUsersTopArtistsOpt(opt *Options) (*FullArtistPage, error) {
spotifyURL := c.baseURL + "me/top/artists"
if opt != nil {
v := url.Values{}
if opt.Limit != nil {
v.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Timerange != nil {
v.Set("time_range", *opt.Timerange+"_term")
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
}
var result FullArtistPage
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// CurrentUsersTopArtists is like CurrentUsersTopArtistsOpt but with
// sensible defaults. The default limit is 20 and the default timerange
// is medium_term.
func (c *Client) CurrentUsersTopArtists() (*FullArtistPage, error) {
return c.CurrentUsersTopArtistsOpt(nil)
}
// CurrentUsersTopTracksOpt gets a list of the top played tracks in a given time
// range of the current Spotify user. It supports up to 50 tracks in a single
// call. This call requires ScopeUserTopRead.
func (c *Client) CurrentUsersTopTracksOpt(opt *Options) (*FullTrackPage, error) {
spotifyURL := c.baseURL + "me/top/tracks"
if opt != nil {
v := url.Values{}
if opt.Limit != nil {
v.Set("limit", strconv.Itoa(*opt.Limit))
}
if opt.Timerange != nil {
v.Set("time_range", *opt.Timerange+"_term")
}
if opt.Offset != nil {
v.Set("offset", strconv.Itoa(*opt.Offset))
}
if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}
}
var result FullTrackPage
err := c.get(spotifyURL, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// CurrentUsersTopTracks is like CurrentUsersTopTracksOpt but with
// sensible defaults. The default limit is 20 and the default timerange
// is medium_term.
func (c *Client) CurrentUsersTopTracks() (*FullTrackPage, error) {
return c.CurrentUsersTopTracksOpt(nil)
}