|
@@ -0,0 +1,177 @@
|
|
1
|
+package spotify
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "context"
|
|
5
|
+ "encoding/json"
|
|
6
|
+ "errors"
|
|
7
|
+ "fmt"
|
|
8
|
+ "golang.org/x/oauth2/clientcredentials"
|
|
9
|
+ spotifyAuth "golang.org/x/oauth2/spotify"
|
|
10
|
+ "io"
|
|
11
|
+ "net/http"
|
|
12
|
+ "net/url"
|
|
13
|
+ "strings"
|
|
14
|
+)
|
|
15
|
+
|
|
16
|
+type SpotifyClient struct {
|
|
17
|
+ clientID string
|
|
18
|
+ clientSecret string
|
|
19
|
+ bearerAuth string
|
|
20
|
+ baseURL *url.URL
|
|
21
|
+ httpClient *http.Client
|
|
22
|
+}
|
|
23
|
+
|
|
24
|
+type TrackInfo struct {
|
|
25
|
+ Album AlbumInfo `json:"album"`
|
|
26
|
+ Artists []ArtistInfo `json:"artists"`
|
|
27
|
+ DiskNumber int `json:"disc_number"`
|
|
28
|
+ DurationMS int `json:"duration_ms"`
|
|
29
|
+ Explicit bool `json:"explicit"`
|
|
30
|
+ ExternalIds ExternalIds `json:"external_ids"`
|
|
31
|
+ ExternalUrls ExternalUrls `json:"external_urls"`
|
|
32
|
+ Href string `json:"href"`
|
|
33
|
+ ID string `json:"id"`
|
|
34
|
+ IsPlayable bool `json:"is_playable"`
|
|
35
|
+ Name string `json:"name"`
|
|
36
|
+ Popularity int `json:"popularity"`
|
|
37
|
+ PreviewUrl string `json:"preview_url"`
|
|
38
|
+ TrackNumber int `json:"track_number"`
|
|
39
|
+ Type string `json:"type"`
|
|
40
|
+ Uri string `json:"uri"`
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+type AlbumInfo struct {
|
|
44
|
+ AlbumType string `json:"album_type"`
|
|
45
|
+ Artists []ArtistInfo `json:"artists"`
|
|
46
|
+ ExternalUrls ExternalUrls `json:"external_urls"`
|
|
47
|
+ Href string `json:"href"`
|
|
48
|
+ ID string `json:"id"`
|
|
49
|
+ Images []ImageInfo `json:"images"`
|
|
50
|
+ Name string `json:"name"`
|
|
51
|
+ Type string `json:"type"`
|
|
52
|
+ Uri string `json:"uri"`
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+type ArtistInfo struct {
|
|
56
|
+ ExternalUrls ExternalUrls `json:"external_urls"`
|
|
57
|
+ Href string `json:"href"`
|
|
58
|
+ ID string `json:"id"`
|
|
59
|
+ Name string `json:"name"`
|
|
60
|
+ Type string `json:"type"`
|
|
61
|
+ Uri string `json:"uri"`
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+type ImageInfo struct {
|
|
65
|
+ Height int `json:"height"`
|
|
66
|
+ URL string `json:url`
|
|
67
|
+ Width int `json:"width"`
|
|
68
|
+}
|
|
69
|
+
|
|
70
|
+type ExternalIds struct {
|
|
71
|
+ ISRC string `json:"isrc"`
|
|
72
|
+}
|
|
73
|
+type ExternalUrls struct {
|
|
74
|
+ Spotify string `json:"spotify"`
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+type TokenInfo struct {
|
|
78
|
+ AccessToken string `json:"access_token"`
|
|
79
|
+ TokenType string `json:"token_type"`
|
|
80
|
+ ExpiresIn int `json:"expires_in"`
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+type AuthErrorResponse struct {
|
|
84
|
+ Error string `json:"error"`
|
|
85
|
+ ErrorDescription string `json:"error_description"`
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+type ErrorResponse struct {
|
|
89
|
+ Error ErrorInfo `json:"error"`
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+type ErrorInfo struct {
|
|
93
|
+ Status int `json:"status"`
|
|
94
|
+ Message string `json:"message"`
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+func NewClient(username, secret string) *SpotifyClient {
|
|
98
|
+ baseURL, _ := url.Parse("https://api.spotify.com")
|
|
99
|
+ httpClient := &http.Client{}
|
|
100
|
+ return &SpotifyClient{
|
|
101
|
+ clientID: username,
|
|
102
|
+ clientSecret: secret,
|
|
103
|
+ baseURL: baseURL,
|
|
104
|
+ httpClient: httpClient,
|
|
105
|
+ }
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+func (s *SpotifyClient) Authenticate() error {
|
|
109
|
+ config := &clientcredentials.Config{
|
|
110
|
+ ClientID: s.clientID,
|
|
111
|
+ ClientSecret: s.clientSecret,
|
|
112
|
+ TokenURL: spotifyAuth.Endpoint.TokenURL,
|
|
113
|
+ }
|
|
114
|
+ token, err := config.Token(context.Background())
|
|
115
|
+ if err != nil {
|
|
116
|
+ fmt.Println(err)
|
|
117
|
+ return err
|
|
118
|
+ }
|
|
119
|
+ s.bearerAuth = token.AccessToken
|
|
120
|
+ return nil
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+func (s *SpotifyClient) GetTrack(trackId string) (*TrackInfo, error) {
|
|
124
|
+ var trackInfo TrackInfo
|
|
125
|
+ url := s.urlFromBase(fmt.Sprintf("/v1/tracks/%s", trackId))
|
|
126
|
+ err := s.doRequest("GET", url, &trackInfo, nil)
|
|
127
|
+ if err != nil {
|
|
128
|
+ return nil, err
|
|
129
|
+ } else {
|
|
130
|
+ return &trackInfo, nil
|
|
131
|
+ }
|
|
132
|
+}
|
|
133
|
+
|
|
134
|
+func (s *SpotifyClient) urlFromBase(path string) *url.URL {
|
|
135
|
+ endpoint := &url.URL{Path: path}
|
|
136
|
+ return s.baseURL.ResolveReference(endpoint)
|
|
137
|
+}
|
|
138
|
+
|
|
139
|
+func (s *SpotifyClient) doRequest(method string, url *url.URL, object interface{}, values *url.Values) error {
|
|
140
|
+ var bodyReader io.Reader
|
|
141
|
+ if values != nil {
|
|
142
|
+ s := values.Encode()
|
|
143
|
+ bodyReader = strings.NewReader(s)
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ req, err := http.NewRequest(method, url.String(), bodyReader)
|
|
147
|
+ if err != nil {
|
|
148
|
+ return err
|
|
149
|
+ }
|
|
150
|
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.bearerAuth))
|
|
151
|
+
|
|
152
|
+ if values != nil {
|
|
153
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ req.Header.Set("Accept", "application/json")
|
|
157
|
+ req.Header.Set("User-Agent", "github.com/lamperi/e4bot/spotify")
|
|
158
|
+
|
|
159
|
+ resp, err := s.httpClient.Do(req)
|
|
160
|
+ if err != nil {
|
|
161
|
+ return err
|
|
162
|
+ }
|
|
163
|
+ defer resp.Body.Close()
|
|
164
|
+
|
|
165
|
+ if resp.StatusCode != 200 {
|
|
166
|
+ var errorResp ErrorResponse
|
|
167
|
+ err = json.NewDecoder(resp.Body).Decode(&errorResp)
|
|
168
|
+ return errors.New(fmt.Sprintf("Spotify API returned: %d: %s", resp.StatusCode, errorResp.Error.Message))
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ err = json.NewDecoder(resp.Body).Decode(object)
|
|
172
|
+ if err != nil {
|
|
173
|
+ return err
|
|
174
|
+ }
|
|
175
|
+ return nil
|
|
176
|
+
|
|
177
|
+}
|