-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_spotify.go
416 lines (352 loc) · 10.4 KB
/
s_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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"strings"
"time"
"unsafe"
"github.com/blueforesticarus/goontunes/util"
"github.com/zmb3/spotify/v2"
)
func fuckshit(Ids []string) []spotify.ID {
return *(*[]spotify.ID)(unsafe.Pointer(&Ids))
}
func shitfuck(Ids []spotify.ID) []string {
return *(*[]string)(unsafe.Pointer(&Ids))
}
func SimplePl2Collection(pl spotify.SimplePlaylist) Collection {
return Collection{
Name: pl.Name,
ID: pl.ID.String(),
Service: "spotify",
Owner: pl.Owner.ID,
Rev: pl.SnapshotID,
Type: "Playlist",
Date: time.Now(),
Size: int(pl.Tracks.Total),
}
}
//used to find spotify links, see discord.py
func try_spotify(entry *Entry) {
u, err := url.Parse(entry.Url)
if err != nil {
println("invalid url")
return
}
if strings.Contains(u.Host, "spotify") {
entry.Service = "spotify"
entry.Valid = true
if strings.Contains(entry.Url, "album") {
entry.Type = "album"
entry.IsTrack = false
} else if strings.Contains(entry.Url, "track") {
entry.Type = "track"
entry.IsTrack = true
} else if strings.Contains(entry.Url, "user") {
entry.Type = "user"
entry.IsTrack = false
} else if strings.Contains(entry.Url, "playlist") {
entry.Type = "playlist"
entry.IsTrack = false
} else {
entry.Valid = false
return
}
re := regexp.MustCompile("/" + entry.Type + "/([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]+)")
ms := re.FindStringSubmatch(u.Path)
if len(ms) == 2 {
entry.ID = ms[1]
} else {
entry.Valid = false
}
}
}
type SpotifyInfo = spotify.FullTrack
type SpotifyExtraInfo = spotify.AudioFeatures
type SpotifyApp struct {
ClientID string
ClientSecret string
Redirect_Uri string
Playlists []*ServicePlaylist
CacheToken bool
client *spotify.Client
userid string
ready util.OutputsNeedInit
playlist_ready util.OutputsNeedInit
}
func (self *SpotifyApp) IsNil() bool {
return self == nil
}
func (self *SpotifyApp) Name() string {
return "spotify"
}
func (self *SpotifyApp) Connect() {
if self.ClientID == "" || self.ClientID == "<yours>" {
log.Fatalf("SPOTIFY: Generate a client id and secret following the instructions here: %s\n", helpurl)
}
var tokenfile string
if self.CacheToken {
//NOTE: this is the last remaining "global" in this file :(
//... not actually a bad use for a global
tokenfile = global.CachePath + "/spotify.token"
}
oauth_client := Authenticate(tokenfile, self.AuthConfig()) //see spotifyauth.go
self.client = spotify.New(
oauth_client,
spotify.WithRetry(true),
)
// use the client, test if it is working
user, err := self.client.CurrentUser(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("SPOTIFY: You are logged in as:", user.ID)
self.userid = user.ID
self.ready.Init(0)
for _, p := range self.Playlists {
p.Init(self)
}
self.playlist_ready.Init(0)
}
func (self *SpotifyApp) Get_Track_Id(track *Track) string {
i, ok := track.IDMaps["spotify"]
if ok {
id := track.IDs[i]
if id != "" && track.SpotifyInfo.V != nil {
return id
}
}
return ""
}
func (self *SpotifyApp) Playlist_InsertTracks(ID string, ins_list []Pl_Ins) int {
//insert groups of tracks
var position = 0
var total = 0
bar := func(o int, idss []string) {
_, err := self.client.AddTracksToPlaylistOpt(
context.Background(), spotify.ID(ID), position+o, fuckshit(idss)...,
)
if err != nil {
fmt.Printf("SPOTIFY: could not insert playlist tracks, %s\n", err)
} else {
fmt.Printf("SPOTIFY: inserted %d tracks\n", len(idss))
total += len(idss)
}
}
for _, v := range ins_list {
position = v.i
util.Batched(bar, v.ids, 100, false)
}
return total
}
func (self *SpotifyApp) Playlist_DeleteTracks(ID string, rm_list []Pl_Rm) int {
//delete tracks
total := 0
foo := func(o int, n int) {
spot_rml := make([]spotify.TrackToRemove, n)
for i, v := range rm_list[o : o+n] {
spot_rml[i] = spotify.NewTrackToRemove(v.id, []int{v.i})
}
_, err := self.client.RemoveTracksFromPlaylistOpt(
context.Background(), spotify.ID(ID), spot_rml, "",
)
if err != nil {
fmt.Printf("SPOTIFY: could not delete playlist tracks, %s\n", err)
} else {
fmt.Printf("SPOTIFY: deleted %d tracks, offset %d \n", n, o)
total += n
}
}
util.BatchedRange(foo, len(rm_list), 100)
return total
}
func (self *SpotifyApp) Playlist_Description(ID string, desc string) error {
err := self.client.ChangePlaylistDescription(context.Background(), spotify.ID(ID), desc)
return err
}
func (self *SpotifyApp) List_Playlists() []Collection {
return self.list_playlists(self.userid)
}
func (self *SpotifyApp) list_playlists(user string) []Collection {
var ret = make([]Collection, 0, 50)
pl, err := self.client.GetPlaylistsForUser(
context.Background(),
self.userid,
spotify.Fields("href,limit,next,previous,total,offset,items(id,name,owner(id),description)"),
spotify.Limit(50),
)
if err != nil {
fmt.Printf("SPOTIFY: Failed to get user playlists, %v\n", err)
return ret
}
for {
for _, p := range pl.Playlists {
ret = append(ret, SimplePl2Collection(p))
}
if pl.Next == "" {
break
}
err = self.client.NextPage(context.Background(), pl)
if err != nil {
fmt.Printf("SPOTIFY: err getting next page of user playlists, %v\n", err)
break
}
}
return ret
}
func (self *SpotifyApp) find_discover(name string) string {
self.ready.Wait()
for _, playlist := range self.list_playlists(name) {
if playlist.Owner == "spotify" && playlist.Name == "Discover Weekly" {
return string(playlist.ID) //probably breaks if users link have saved someone elses discover weekly
}
}
return ""
}
func (self *SpotifyApp) Create_Playlist(name string) string {
pl, err := self.client.CreatePlaylistForUser(context.Background(), self.userid, name, "GoonTunes", true, false)
if err != nil {
fmt.Printf("SPOTIFY: Failed to get create playlist %s, %v\n", name, err)
return ""
}
fmt.Printf("SPOTIFY: Created playlist %s %s\n", pl.ID.String(), pl.Name)
return pl.ID.String()
}
func (self *SpotifyApp) Fetch_Playlist(ID string) *Collection {
self.ready.Wait()
if ID == "" {
return nil
}
fields := spotify.Fields("name,snapshot_id,tracks(total),id,owner(id)")
pl, err := self.client.GetPlaylist(context.Background(), spotify.ID(ID), fields)
if err != nil {
fmt.Printf("SPOTIFY: Failed to get playlist %s metadata, %v\n", ID, err)
return nil
}
//fmt.Printf("SPOTIFY: got playlist %s metadata\n", pl.ID) its too much
var c = SimplePl2Collection(pl.SimplePlaylist)
return &c
}
func (self *SpotifyApp) Fetch_Playlist_Tracks(c Collection) *Collection {
self.ready.Wait()
//NOTE: we are repeating work here done by the Fetch_Playlist calls
fields := spotify.Fields("name,snapshot_id,tracks(href,limit,next,offset,previous,total,items(track(id))),id,owner(id)") //100 tracks per request, better than 50
pl, err := self.client.GetPlaylist(context.Background(), spotify.ID(c.ID), fields)
if err != nil {
fmt.Printf("SPOTIFY: Failed to get playlist %s metadata, %v\n", c.ID, err)
return nil
}
trackids := make([]string, 0, pl.Tracks.Total+2)
for {
for _, track := range pl.Tracks.Tracks {
//index := i + pl.Tracks.Offset
if track.Track.ID.String() == "" {
fmt.Printf("SPOTIFY: bad playlist tracklist\n")
return nil
}
trackids = append(trackids, track.Track.ID.String())
}
if pl.Tracks.Next == "" {
break
}
err := self.client.NextPage(context.Background(), &pl.Tracks)
if err != nil {
fmt.Printf("SPOTIFY: err getting next page of playlist tracks, %v\n", err)
return nil
}
}
fmt.Printf("SPOTIFY: got playlist %s with %d tracks\n", pl.ID, len(trackids))
c = SimplePl2Collection(pl.SimplePlaylist)
c.TracksIDs = trackids
return &c
}
func (self *SpotifyApp) fetch_tracks_info(Ids []string) map[string]*SpotifyInfo {
self.ready.Wait()
ret := make(map[string]*SpotifyInfo, len(Ids))
foo := func(offset int, idss []string) {
tl, err := self.client.GetTracks(context.Background(), fuckshit(idss))
if err != nil {
fmt.Printf("SPOTIFY: Failed to get tracks, %v\n", err)
return
}
for i, track := range tl {
if track == nil {
fmt.Printf("SPOTIFY: Missing track audio features %s\n", idss[i])
} else {
ret[string(track.ID)] = track
}
}
}
util.Batched(foo, Ids, 50, false)
fmt.Printf("SPOTIFY: got info for %d tracks\n", len(Ids))
return ret
}
func (self *SpotifyApp) fetch_tracks_extrainfo(Ids []string) map[string]*SpotifyExtraInfo {
self.ready.Wait()
ret := make(map[string]*SpotifyExtraInfo, len(Ids))
foo := func(_ int, idss []string) {
tl, err := self.client.GetAudioFeatures(context.Background(), fuckshit(idss)...)
if err != nil {
fmt.Printf("SPOTIFY: Failed to get track audio features, %v\n", err)
return
}
for i, audiofeatures := range tl {
if audiofeatures == nil {
fmt.Printf("SPOTIFY: Missing track audio features %s\n", idss[i])
} else {
ret[string(audiofeatures.ID)] = audiofeatures
}
}
}
util.Batched(foo, Ids, 100, false) //redundant with plumber
fmt.Printf("SPOTIFY: got extra info for %d tracks\n", len(Ids))
return ret
}
func (self *SpotifyApp) fetch_album_tracks(Ids []string) []Collection {
self.ready.Wait()
// This will return an empty list for any album which has more than 50 tracks
// this is due to track limits in spotifys api, and because at that point its more like a playlist, which is ignored.
ret := make([]Collection, len(Ids))
foo := func(offset int, idss []string) {
al, err := self.client.GetAlbums(context.Background(), fuckshit(idss))
if err != nil {
fmt.Printf("SPOTIFY: Failed to get album tracks, %v\n", err)
return
}
for i, album := range al {
size := album.Tracks.Total
trackids := make([]string, 0, size)
OUTER:
for {
for _, track := range album.Tracks.Tracks {
if track.ID.String() == "" {
fmt.Printf("SPOTIFY: bad album tracklist\n")
break OUTER
}
trackids = append(trackids, track.ID.String())
}
if album.Tracks.Next == "" {
break
}
err := self.client.NextPage(context.Background(), &album.Tracks)
if err != nil {
fmt.Printf("SPOTIFY: err getting next page of playlist tracks, %v\n", err)
break
}
}
ret[i+offset] = Collection{
ID: album.ID.String(),
TracksIDs: trackids,
Service: "spotify",
Date: time.Now(),
Type: "album",
}
}
}
util.Batched(foo, Ids, 20, false)
fmt.Printf("SPOTIFY: got tracks for %d albums\n", len(Ids))
return ret
}