-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotify.go
198 lines (179 loc) · 5.26 KB
/
spotify.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
package music
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type Response struct {
Href string `json:"href"`
Items []struct {
AddedAt time.Time `json:"added_at"`
AddedBy struct {
ExternalUrls struct {
Spotify string `json:"spotify"`
} `json:"external_urls"`
Href string `json:"href"`
ID string `json:"id"`
Type string `json:"type"`
URI string `json:"uri"`
} `json:"added_by"`
IsLocal bool `json:"is_local"`
PrimaryColor interface{} `json:"primary_color"`
SharingInfo struct {
ShareID string `json:"share_id"`
ShareURL string `json:"share_url"`
URI string `json:"uri"`
} `json:"sharing_info"`
Track struct {
Album struct {
AlbumType string `json:"album_type"`
Artists []struct {
ExternalUrls struct {
Spotify string `json:"spotify"`
} `json:"external_urls"`
Href string `json:"href"`
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
URI string `json:"uri"`
} `json:"artists"`
ExternalUrls struct {
Spotify string `json:"spotify"`
} `json:"external_urls"`
Href string `json:"href"`
ID string `json:"id"`
Images []struct {
Height int `json:"height"`
URL string `json:"url"`
Width int `json:"width"`
} `json:"images"`
Name string `json:"name"`
ReleaseDate string `json:"release_date"`
ReleaseDatePrecision string `json:"release_date_precision"`
TotalTracks int `json:"total_tracks"`
Type string `json:"type"`
URI string `json:"uri"`
} `json:"album"`
Artists []struct {
ExternalUrls struct {
Spotify string `json:"spotify"`
} `json:"external_urls"`
Href string `json:"href"`
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
URI string `json:"uri"`
} `json:"artists"`
DiscNumber int `json:"disc_number"`
DurationMs int `json:"duration_ms"`
Episode bool `json:"episode"`
Explicit bool `json:"explicit"`
ExternalIds struct {
Isrc string `json:"isrc"`
} `json:"external_ids"`
ExternalUrls struct {
Spotify string `json:"spotify"`
} `json:"external_urls"`
Href string `json:"href"`
ID string `json:"id"`
IsLocal bool `json:"is_local"`
IsPlayable bool `json:"is_playable"`
Name string `json:"name"`
Popularity int `json:"popularity"`
PreviewURL string `json:"preview_url"`
Track bool `json:"track"`
TrackNumber int `json:"track_number"`
Type string `json:"type"`
URI string `json:"uri"`
} `json:"track"`
VideoThumbnail struct {
URL interface{} `json:"url"`
} `json:"video_thumbnail"`
} `json:"items"`
}
//https://api.spotify.com/v1/playlists/2E0Yp9EH27aq68HRhvDXWk/tracks?offset=0&limit=100&additional_types=track%2Cepisode&market=BE
func (m *MusicCommand) SpotifyToSearch(id string) []string {
tracks := []string{}
hasMore := true
offset := 0
for hasMore {
// call spotify
m.fetchSpotifyToken()
m.SpotifyTokenMutex.Lock()
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.spotify.com/v1/playlists/%s/tracks?offset=%d&limit=100&additional_types=track&market=BE", id, offset), nil)
if err != nil {
log.Printf("error: %s", err)
return tracks
}
req.Header.Set("Authorization", "Bearer "+m.SpotifyToken)
resp, err := http.DefaultClient.Do(req)
m.SpotifyTokenMutex.Unlock()
if err != nil {
log.Printf("error: %s", err)
return tracks
}
defer resp.Body.Close()
if err != nil {
log.Println("Error getting spotify playlist", "err", err)
return tracks
}
body, _ := ioutil.ReadAll(resp.Body)
data := Response{}
err = json.Unmarshal(body, &data)
if err != nil {
log.Println("Error parsing spotify playlist", "err", err, string(body))
return tracks
}
if len(data.Items) == 0 {
log.Println(string(body))
}
for _, item := range data.Items {
if len(item.Track.Artists) == 0 {
tracks = append(tracks, item.Track.Name)
} else {
tracks = append(tracks, fmt.Sprintf("%s %s", item.Track.Name, item.Track.Artists[0].Name))
}
}
if len(data.Items) < 100 {
hasMore = false
} else {
offset += 100
}
if len(tracks) >= 300 {
hasMore = false // look bro it stops here with your 50000 tracks
}
}
return tracks
}
func (m *MusicCommand) fetchSpotifyToken() {
m.SpotifyTokenMutex.Lock()
defer m.SpotifyTokenMutex.Unlock()
if m.SpotifyToken != "" {
return
}
resp, err := http.Get("https://open.spotify.com/get_access_token?reason=transport&productType=embed")
if err != nil {
log.Println("Error getting spotify token", "err", err)
return
}
data := struct {
AccessToken string `json:"accessToken"`
AccessTokenExpirationTimestampMs int64 `json:"accessTokenExpirationTimestampMs"`
}{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
log.Println("Error parsing spotify token", "err", err)
return
}
m.SpotifyToken = data.AccessToken
go func(s int64) {
s -= 100
time.Sleep(time.Duration(s) * time.Millisecond)
m.SpotifyTokenMutex.Lock()
m.SpotifyToken = ""
m.SpotifyTokenMutex.Unlock()
}(data.AccessTokenExpirationTimestampMs - time.Now().Unix()*1000)
}