forked from avatar282/Spotify-APKV7.3.2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartist.go
More file actions
212 lines (195 loc) · 6.69 KB
/
Copy pathartist.go
File metadata and controls
212 lines (195 loc) · 6.69 KB
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
package spotify
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// SimpleArtist contains basic info about an artist.
type SimpleArtist struct {
Name string `json:"name"`
ID ID `json:"id"`
// The Spotify URI for the artist.
URI URI `json:"uri"`
// A link to the Web API enpoint providing full details of the artist.
Endpoint string `json:"href"`
ExternalURLs map[string]string `json:"external_urls"`
}
// FullArtist provides extra artist data in addition to what is provided by SimpleArtist.
type FullArtist struct {
SimpleArtist
// The popularity of the artist, expressed as an integer between 0 and 100.
// The artist's popularity is calculated from the popularity of the artist's tracks.
Popularity int `json:"popularity"`
// A list of genres the artist is associated with. For example, "Prog Rock"
// or "Post-Grunge". If not yet classified, the slice is empty.
Genres []string `json:"genres"`
Followers Followers
// Images of the artist in various sizes, widest first.
Images []Image `json:"images"`
}
// GetArtist is a wrapper around DefaultClient.GetArtist.
func GetArtist(id ID) (*FullArtist, error) {
return DefaultClient.GetArtist(id)
}
// GetArtist gets Spotify catalog information for a single artist, given its Spotify ID.
func (c *Client) GetArtist(id ID) (*FullArtist, error) {
spotifyURL := fmt.Sprintf("%sartists/%s", baseAddress, id)
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)
}
var a FullArtist
err = json.NewDecoder(resp.Body).Decode(&a)
if err != nil {
return nil, err
}
return &a, nil
}
// GetArtists is a wrapper around DefaultClient.GetArtists.
func GetArtists(ids ...ID) ([]*FullArtist, error) {
return DefaultClient.GetArtists(ids...)
}
// GetArtists gets spotify catalog information for several artists based on their
// Spotify IDs. It supports up to 50 artists in a single call. Artists are
// returned in the order requested. If an artist is not found, that position
// in the result will be nil. Duplicate IDs will result in duplicate artists
// in the result.
func (c *Client) GetArtists(ids ...ID) ([]*FullArtist, error) {
spotifyURL := fmt.Sprintf("%sartists?ids=%s", baseAddress, strings.Join(toStringSlice(ids), ","))
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)
}
var a struct {
Artists []*FullArtist
}
err = json.NewDecoder(resp.Body).Decode(&a)
if err != nil {
return nil, err
}
return a.Artists, nil
}
// GetArtistsTopTracks is a wrapper around DefaultClient.GetArtistsTopTracks.
func GetArtistsTopTracks(artistID ID, country string) ([]FullTrack, error) {
return DefaultClient.GetArtistsTopTracks(artistID, country)
}
// GetArtistsTopTracks gets Spotify catalog information about an artist's top
// tracks in a particular country. It returns a maximum of 10 tracks. The
// country is specified as an ISO 3166-1 alpha-2 country code.
func (c *Client) GetArtistsTopTracks(artistID ID, country string) ([]FullTrack, error) {
spotifyURL := fmt.Sprintf("%sartists/%s/top-tracks?country=%s", baseAddress, artistID, country)
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)
}
var t struct {
Tracks []FullTrack `json:"tracks"`
}
err = json.NewDecoder(resp.Body).Decode(&t)
if err != nil {
return nil, err
}
return t.Tracks, nil
}
// GetRelatedArtists is a wrapper around DefaultClient.GetRelatedArtists.
func GetRelatedArtists(id ID) ([]FullArtist, error) {
return DefaultClient.GetRelatedArtists(id)
}
// GetRelatedArtists gets Spotify catalog information about artists similar to a
// given artist. Similarity is based on analysis of the Spotify community's
// listening history. This function returns up to 20 artists that are considered
// related to the specified artist.
func (c *Client) GetRelatedArtists(id ID) ([]FullArtist, error) {
spotifyURL := fmt.Sprintf("%sartists/%s/related-artists", baseAddress, id)
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)
}
var a struct {
Artists []FullArtist `json:"artists"`
}
err = json.NewDecoder(resp.Body).Decode(&a)
if err != nil {
return nil, err
}
return a.Artists, nil
}
// GetArtistAlbums is a wrapper around DefaultClient.GetArtistAlbums.
func GetArtistAlbums(artistID ID) (*SimpleAlbumPage, error) {
return DefaultClient.GetArtistAlbums(artistID)
}
// GetArtistAlbums gets Spotify catalog information about an artist's albums.
// It is equivalent to GetArtistAlbumsOpt(artistID, nil).
func (c *Client) GetArtistAlbums(artistID ID) (*SimpleAlbumPage, error) {
return c.GetArtistAlbumsOpt(artistID, nil, nil)
}
// GetArtistAlbumsOpt is a wrapper around DefaultClient.GetArtistAlbumsOpt
func GetArtistAlbumsOpt(artistID ID, options *Options, t *AlbumType) (*SimpleAlbumPage, error) {
return DefaultClient.GetArtistAlbumsOpt(artistID, options, t)
}
// GetArtistAlbumsOpt is just like GetArtistAlbums, but it accepts optional
// parameters used to filter and sort the result.
//
// The AlbumType argument can be used to find a particular type of album. Search
// for multiple types by OR-ing the types together.
func (c *Client) GetArtistAlbumsOpt(artistID ID, options *Options, t *AlbumType) (*SimpleAlbumPage, error) {
spotifyURL := fmt.Sprintf("%sartists/%s/albums", baseAddress, artistID)
// add optional query string if options were specified
values := url.Values{}
if t != nil {
values.Set("album_type", t.encode())
}
if options != nil {
if options.Country != nil {
values.Set("market", *options.Country)
} else {
// if the market is not specified, Spotify will likely return a lot
// of duplicates (one for each market in which the album is available)
// - prevent this behavior by falling back to the US by default
// TODO: would this ever be the desired behavior?
values.Set("market", CountryUSA)
}
if options.Limit != nil {
values.Set("limit", strconv.Itoa(*options.Limit))
}
if options.Offset != nil {
values.Set("offset", strconv.Itoa(*options.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)
}
var p SimpleAlbumPage
err = json.NewDecoder(resp.Body).Decode(&p)
if err != nil {
return nil, err
}
return &p, nil
}