-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpotify.ahk
720 lines (668 loc) · 24.3 KB
/
Spotify.ahk
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#MaxThreads 2
class Spotify {
static SHOULD_CHECK_CONNECTION_BEFORE_REQUEST := false
__New() {
this.Util := new Util(this)
this.Player := new Player(this)
this.Library := new Library(this)
this.Albums := new Albums(this)
this.Artists := new Artists(this)
this.Tracks := new Tracks(this)
this.Playlists := new Playlists(this)
this.CurrentUser := new user(JSON.load(this.Util.CustomCall("GET", "me")), this, true)
this.Users := new Users(this)
}
}
class Util {
static MAX_RETRY := 3
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
this.RefreshLoc := "HKCU\Software\SpotifyAHK"
this.StartUp()
this.AuthRetries := 0 ; Count of how many times we have retried auth
}
StartUp() {
if (this.AuthRetries >= this.MAX_RETRY) {
MsgBox, % "Spotify.ahk authorization attempt cap met, aborting"
Spotify.Util := ""
this := ""
Spotify := ""
return
}
RegRead, refresh, % this.RefreshLoc, refreshToken
if (refresh) {
this.RefreshTempToken(refresh)
} else {
this.auth := ""
paths := {}
paths["/callback"] := this["authCallback"].bind(this)
paths["404"] := this["NotFound"]
server := new HttpServer()
server.SetPaths(paths)
server.Serve(8000)
Run, % "https://accounts.spotify.com/en/authorize?client_id=9fe26296bb7b4330ac59339efd2742b0&response_type=code&redirect_uri=http:%2F%2Flocalhost:8000%2Fcallback&scope=user-modify-playback-state%20user-read-currently-playing%20user-read-playback-state%20user-library-modify%20user-library-read%20user-read-email%20user-read-private%20user-read-birthdate%20user-follow-read%20user-follow-modify%20playlist-read-private%20playlist-read-collaborative%20playlist-modify-public%20playlist-modify-private%20user-read-recently-played%20user-top-read"
loop {
Sleep, -1
} until (this.WebAuthDone() = true)
this.FetchTokens()
}
}
; Timeout methods
SetTimeout(ExpiresInSeconds) {
TimeOut := A_Now
EnvAdd, TimeOut, %ExpiresInSeconds%, seconds
this.TimeOut := TimeOut
}
CheckTimeout() {
if (this.TimeLastChecked = A_Min) {
return
}
this.TimeLastChecked := A_Min
if (A_Now > this.TimeOut) {
RegRead, refresh, % this.RefreshLoc, refreshToken
this.RefreshTempToken(refresh)
}
}
; API token operations
IsInternetConnected(CheckURL := "http://api.spotify.com/v1/"){
return DllCall("Wininet.dll\InternetCheckConnection", "Str", CheckURL, "UInt", 1, "UInt", 0)
}
RefreshTempToken(refresh) {
refresh := this.DecryptToken(refresh)
arg := {1:{1:"Content-Type", 2:"application/x-www-form-urlencoded"}, 2:{1:"Authorization", 2:"Basic OWZlMjYyOTZiYjdiNDMzMGFjNTkzMzllZmQyNzQyYjA6ZWNhNjU2ZDFkNTczNDNhOTllMWJjNWVmODQ0YmY2NGM="}}
try {
response := this.CustomCall("POST", "https://accounts.spotify.com/api/token?grant_type=refresh_token&refresh_token=" . refresh, arg, true)
}
catch E {
if (InStr(E.What, "HTTP response code not 2xx")) {
this.AuthRetries++
MsgBox, % "Spotify.ahk could not get a valid refresh token from the Spotify API, retrying authorization."
RegWrite, REG_SZ, % this.RefreshLoc, refreshToken, % "" ; Wipe the stored (bad) refresh token
return this.StartUp() ; Retry auth and hope we get a valid refresh token this time
}
}
if (InStr(response, "refresh_token")) {
this.SaveRefreshToken(response)
}
try {
Response := JSON.Load(Response) ; Oh god, this old code is really bad. If anyone bothers reading this, please just
; use the rewrite
}
catch E {
MsgBox, % "Spotify.ahk could not get a valid refresh token from the Spotify API, retrying authorization."
this.AuthRetries++
return this.StartUp()
}
ForceError := false
if (ForceError) {
Response["error_description"] := "Invalid refresh token"
}
if (Response["access_token"] && !ForceError) {
; If we got an access token, we can set the flag that we're authorized
this.authState := true
this.Token := Response["access_token"] ; And store the new access token
this.SetTimeout(Response.expires_in) ; And set when the new access token will expire
}
else {
; Else if they didn't give us a new access token, something went wrong
this.authState := false ; Set that auth is *not* complete
this.AuthRetries++
if (Response["error_description"] = "Invalid refresh token") {
RegWrite, REG_SZ, % this.RefreshLoc, refreshToken, % "" ; Wipe the stored (bad) refresh token
MsgBox, % "Spotify.ahk could not get a valid refresh token from the Spotify API, retrying authorization."
return this.StartUp() ; Retry auth and hope we get a valid refresh token this time
}
Throw {"Message": Response["error_description"], "What": Response["error"], "File": A_LineFile, "Line": A_LineNumber}
;this.StartUp() ; Call startup after wiping the stored refresh token, so we can try to get a new valid one
}
}
FetchTokens() {
if (this.fail) {
ErrorLevel := 1
return
}
if (this.authState) {
return
}
AHKsock_Close(-1)
arg := {1:{1:"Content-Type", 2:"application/x-www-form-urlencoded"}, 2:{1:"Authorization", 2:"Basic OWZlMjYyOTZiYjdiNDMzMGFjNTkzMzllZmQyNzQyYjA6ZWNhNjU2ZDFkNTczNDNhOTllMWJjNWVmODQ0YmY2NGM="}}
response := this.CustomCall("POST", "https://accounts.spotify.com/api/token?grant_type=authorization_code&code=" . this.auth . "&redirect_uri=http:%2F%2Flocalhost:8000%2Fcallback", arg, true)
RegexMatch(response, "access_token"":""\K.*?(?="")", token)
this.token := token
this.SaveRefreshToken(response)
}
; Local token operations
SaveRefreshToken(response) {
RegexMatch(response, "refresh_token"":""\K.*?(?="")", response)
if !(response) {
return
}
response := this.encryptToken(response)
RegWrite, REG_SZ, % this.RefreshLoc, RefreshToken, % response
return
}
; API call method with auto-auth/timeout check/base URL
CustomCall(method, url, HeaderArray := "", noTimeOut := false, body := "", noErr := false) {
if (Spotify.SHOULD_CHECK_CONNECTION_BEFORE_REQUEST && !this.IsInternetConnected()) {
Throw Exception("No internet connection")
}
if !(noTimeOut) {
this.CheckTimeout()
}
if !((InStr(url, "https://api.spotify.com")) || (InStr(url, "https://accounts.spotify.com/api/"))) {
url := "https://api.spotify.com/v1/" . url
}
if !(HeaderArray) {
HeaderArray := {1:{1:"Authorization", 2:"Bearer " . this.token}}
}
SpotifyWinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
SpotifyWinHttp.Open(method, url, false)
for index, SubHeaderArray in HeaderArray {
SpotifyWinHttp.SetRequestHeader(SubHeaderArray[1], SubHeaderArray[2])
}
SpotifyWinHttp.Send(body)
if (SpotifyWinHttp.Status > 299 && !noErr) {
ErrorObject := JSON.Load(SpotifyWinHttp.ResponseText).Error
ErrorMessage := ""
if (ErrorObject) {
if (ErrorObject.Reason) {
ErrorMessage .= ErrorObject.Reason ": "
}
ErrorMessage .= ErrorObject.Message
}
else {
ErrorMessage := SpotifyWinHttp.Status . " not 2xx for request """ . method . ":" . url . """."
}
throw {message: ErrorMessage, what: "HTTP response code not 2xx", file: A_LineFile, line: A_LineNumber}
}
return SpotifyWinHttp.ResponseText
}
; Web auth methods
NotFound(ByRef req, ByRef res) {
res.SetBodyText("Page not found")
}
authCallback(self, ByRef req, ByRef res) {
res.SetBodyText( req.queries["error"] ? "Error, authorization not given, Spotify.ahk will not function correctly without authorization." : "Authorization complete, closing listen server.")
res.status := 200
this.auth := req.queries["code"]
this.fail := req.queries["error"]
}
WebAuthDone() {
return (this.auth ? true : false)
}
; Token encryption/decryption methods
EncryptToken(RefreshToken) {
return crypt.encrypt.strEncrypt(RefreshToken, this.GetIDs(), 5, 3)
}
DecryptToken(RefreshToken) {
try {
return crypt.encrypt.strDecrypt(RefreshToken, this.GetIDs(), 5, 3)
} catch {
this.AuthRetries++
MsgBox, % "Spotify.ahk could not decrypt local refresh token, retrying authorization"
RegDelete, % this.RefreshLoc, RefreshToken
this.StartUp()
RegRead, RefreshToken, % this.RefreshLoc, refreshToken
return crypt.encrypt.strDecrypt(RefreshToken, this.GetIDs(), 5, 3)
}
}
GetIDs() {
static infos := [["ProcessorID", "Win32_Service"], ["SKU", "Win32_BaseBoard"], ["DeviceID", "Win32_USBController"]]
wmi := ComObjGet("winmgmts:")
id := ""
for i, a in infos {
wmin := wmi.execQuery("Select " . a[1] . " from " . a[2])._newEnum
while wmin[wminf] {
id .= wminf[a[1]]
}
}
return id
}
}
class Player {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
SaveCurrentlyPlaying() {
/*
* Gets the currently playing track, then tells it to save itself (10/10 OOP, I know)
* Requires something to be playing
* returns the text response from the API, which is empty unless there is an error
*/
return this.GetCurrentPlaybackInfo().Track.Save()
}
UnSaveCurrentlyPlaying() {
/*
* Gets the currently playing track, then tells it to unsave itself
* Requires something to be playing
* returns the text response from the API, which is empty unless there is an error
*/
return this.GetCurrentPlaybackInfo().track.UnSave()
}
SetVolume(volume) {
/*
* Sets the volume of playback on the active device to the percent (0-100) passed
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/volume?volume_percent=" . volume)
}
GetCurrentPlaybackInfo() {
/*
* Calls me/player, which returns a whole bunch of different objects
* Translates JSON versions of track/device/context objects into custom objects
* Alters the original response object, so any extra info that can't be turned into an object is still returned
*/
Resp := JSON.load(this.ParentObject.Util.CustomCall("GET", "me/player"))
Resp.Track := new track(Resp["item"], this.ParentObject)
Resp.Device := new device(Resp["device"], this.ParentObject)
Resp.Context := new context(Resp["context"], this.ParentObject)
return Resp
}
ChangeContext(ContextURI) {
/*
* OLD - I should probably remove this
* Change playback to a different context (AKA playlist/album/just read whatever Spotify says is a context)
* Only to be directly called until I finish with the playlist object
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/play",, false, JSON.Dump({"context_uri": ContextURI}))
}
GetDeviceList() {
/*
* Gets an array of device objects from the API
* Translates them into our device class, and returns an array of custom device objects
*/
Resp := JSON.Load(this.ParentObject.Util.CustomCall("GET", "me/player/devices"))
RetVar := []
for k, v in Resp["devices"] {
RetVar.Push(new device(v, this.ParentObject))
}
return RetVar
}
GetRecentlyPlayed() {
/*
* Gets an array of tracks alongside other info from the API
* You might also want to read this Spotify API docs page
* https://developer.spotify.com/documentation/web-api/reference/player/get-recently-played/
* WARNING, EVERYTHING IS WRAPPED IN A PAGING OBJECT, the return object is structured as follows
* Paging Object
* |-> ["items"] (An array of the below objects)
* | |-> [1] Play History Object
* | | |-> ["track"] (A simplified track object)
* | | |-> ["context"] (A context object the track was played in)
* | | |-> ["played_at"] (A UTC timestamp formatted YYYY-MM-DDTHH:MM:SSZ) - Note, I've got no clue what T or Z mean
* | |-> [2] Another Play History Object (They are numerically indexed, you can loop through with a for loop)
* I really like how easy it is to follow what this returns with that nice graphic, I think I'll do it more
* Builds the play history objects out of functional custom objects
*/
Resp := JSON.Load(this.ParentObject.Util.CustomCall("GET", "me/player/recently-played?limit=50"))
for k, v in Resp["items"] {
v := {"track": new track(v["track"], this.ParentObject), "context": new context(v["context"], this.ParentObject), "played_at": v["played_at"]}
}
return Resp
}
SeekTime(TimeInMS) {
/*
* Tells the API to jump to the specified time in MS on the currently playing track
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/seek?position_ms=" . TimeInMS)
}
SetRepeatMode(mode) {
/*
* Tells the API to change the repeat mode
* Passing 1 for mode will have the currently playing track repeat
* Passing 2 for mode will have the currently playing context repeat
* Passing 3 or any other value that isn't 1/2 will turn off repeat
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/repeat?state=" . (mode = 1 ? "track" : (mode = 2 ? "context" : "off")))
}
SetShuffle(mode) {
/*
* Tells the API to change the shuffle mode to true/false, depending on what it it passed
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/shuffle?state=" . (mode ? "true" : "false"))
}
NextTrack() {
/*
* Figure this one out on your own
*/
return this.ParentObject.Util.CustomCall("POST", "me/player/next")
}
LastTrack() {
/*
* Figure this one out on your own
*/
return this.ParentObject.Util.CustomCall("POST", "me/player/previous")
}
PausePlayback() {
/*
* Figure this one out on your own
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/pause")
}
ResumePlayback() {
/*
* Figure this one out on your own
*/
return this.ParentObject.Util.CustomCall("PUT", "me/player/play")
}
PlayPause() {
/*
* Figure this one out on your own
*/
return ((this.GetCurrentPlaybackInfo()["is_playing"] = 0) ? (this.ResumePlayback()) : (this.PausePlayback()))
}
}
class Library {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
CheckSavedForTrack(TrackID) {
return this.ParentObject.Util.CustomCall("GET", "me/tracks/contains?ids=" . TrackID)
}
GetSavedAlbums() {
resp := JSON.load(this.ParentObject.Util.CustomCall("GET", "me/albums?limit=1"))
RetVar := []
RetVar[1] := ""
RetVar.SetCapacity(resp["total"])
loop, % Ceil(resp["total"]/50) {
for k, v in JSON.Load(this.ParentObject.Util.CustomCall("GET", "me/albums?limit=50&offset=" . ((A_Index - 1 ) * 50)))["items"] {
alb := new album(v["album"], this.ParentObject)
alb.added_at := v["added_at"]
RetVar.Push(alb)
}
}
RetVar.RemoveAt(1)
return RetVar
}
GetSavedTracks() {
resp := JSON.load(this.ParentObject.Util.CustomCall("GET", "me/tracks?limit=1"))
RetVar := []
RetVar[1] := ""
RetVar.SetCapacity(resp["total"])
loop, % Ceil(resp["total"]/50) {
for k, v in JSON.Load(this.ParentObject.Util.CustomCall("GET", "me/tracks?limit=50&offset=" . ((A_Index - 1 ) * 50)))["items"] {
trk := new track(v["track"], this.ParentObject)
trk.added_at := v["added_at"]
RetVar.Push(trk)
}
}
RetVar.RemoveAt(1)
return RetVar
}
}
class Tracks {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
GetTrack(TrackID) {
return new track(JSON.Load(this.ParentObject.Util.CustomCall("GET", "tracks/" . TrackID)), this.ParentObject)
}
}
class Albums {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
GetAlbum(AlbumID) {
return new album(JSON.Load(this.ParentObject.Util.CustomCall("GET", "albums/" . AlbumID)), this.ParentObject)
}
}
class Artists {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
GetArtist(ArtistID) {
return new artist(JSON.Load(this.ParentObject.Util.CustomCall("GET", "artists/" . ArtistID)), this.ParentObject)
}
}
class Playlists {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
GetPlaylist(PlaylistID) {
return new playlist(JSON.Load(this.ParentObject.Util.CustomCall("GET", "playlists/" . PlaylistID)), this.ParentObject)
}
GetPlaylistTracks(PlaylistID) {
; get first page (up to 100 tracks)
PlaylistObject := new playlist(JSON.Load(this.ParentObject.Util.CustomCall("GET", "playlists/" . PlaylistID)), this.ParentObject)
offset := 100
loop {
; get next 100 tracks (if any)
TrackObject := JSON.Load(this.ParentObject.Util.CustomCall("GET", "playlists/" . PlaylistID . "/tracks?offset=" . offset))
; add tracks to playlist object
PlaylistObject.AddTracksPage(TrackObject.items)
offset += 100
} until !TrackObject.items.MaxIndex()
return PlaylistObject
}
CreatePlaylist(name, description, public := true) {
headers := {1:{1:"Authorization", 2:"Bearer " . this.ParentObject.Util.token}, 2:{1:"Content-Type", 2:"application/json"}}
body := "{""name"":""" . name . """, ""description"":""" . description """, ""public"":" . public . "}"
MsgBox, % body
return new playlist(JSON.Load(this.ParentObject.Util.CustomCall("POST", "users/" . this.ParentObject.CurrentUser.id . "/playlists", headers,, body)), this.ParentObject)
}
}
class Users {
__New(ByRef ParentObject) {
this.ParentObject := ParentObject
}
GetUser(UserID) {
return new user(JSON.Load(this.ParentObject.Util.CustomCall("GET", "users/" . UserID)), this.ParentObject)
}
}
class playlist {
__New(PlaylistObj, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := PlaylistObj
this.description := (this.json["description"] = "null" ? "" : this.json["description"])
this.id := this.json["id"]
this.name := this.json["name"]
this.uri := this.json["uri"]
this.owner := new user(this.json["owner"], this.SpotifyObj)
this.public := (this.json["public"] = "null" ? true : (this.json["public"] = "true" ? true : false))
this.tracks := []
for k, v in this.json["tracks"]["items"] {
this.tracks.Push(new track(v["track"], this.SpotifyObj))
}
this.uri := this.json["uri"]
}
AddTrack(TrackIDOrTrackOBJ) {
if (IsObject(TrackIDOrTrackOBJ)) {
tid := TrackIDOrTrackOBJ.id
}
else {
tid := TrackIDOrTrackOBJ
}
this.SpotifyObj.Util.CustomCall("POST", "playlists/" . this.id . "/tracks?uris=spotify:track:" . tid)
}
RemoveTrack(TrackIDOrTrackOBJ) {
if (IsObject(TrackIDOrTrackOBJ)) {
tid := TrackIDOrTrackOBJ.id
}
else {
tid := TrackIDOrTrackOBJ
}
this.SpotifyObj.Util.CustomCall("DELETE", "playlists/" . this.id . "/tracks",, false, JSON.Dump({"tracks": [{"uri": "spotify:track:" . tid}]}))
}
Play() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/player/play",, false, JSON.Dump({"context_uri": "spotify:playlist:" . this.id}))
}
Delete() {
this.SpotifyObj.Util.CustomCall("DELETE", "https://api.spotify.com/v1/playlists/" . this.id "/followers")
return
}
GetAllTracks() {
; get first page (up to 100 tracks)
offset := 100
loop {
; get next 100 tracks (if any)
TracksObject := JSON.Load(this.SpotifyObj.Util.CustomCall("GET", "playlists/" . this.id . "/tracks?limit=100&offset=" . offset))
; add tracks to playlist object
for k, v in TracksObject.items {
this.tracks.Push(new track(v["track"], this.SpotifyObj))
}
offset += 100
} until !TracksObject.items.MaxIndex()
return this.tracks
}
; Fuck me, all these classes feel so half-baked, what the hell am I even doing?
}
class track {
__New(ResponseTrackObj, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := ResponseTrackObj
this.id := this.json["id"]
this.album := new album(this.json["album"], this.SpotifyObj) ; TODO -- Album objects
this.artists := []
for k, v in this.json["artists"] {
this.artists.Push(new artist(v, this.SpotifyObj))
}
this.duration := this.json["duration_ms"]
this.explicit := this.json["explicit"]
this.name := this.json["name"]
}
IsSaved[] {
Get {
return (this.SpotifyObj.Util.CustomCall("GET", "me/tracks/contains?ids=" . this.id) ~= "true" ? true : false)
}
}
Save() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/tracks?ids=" . this.id)
}
UnSave() {
return this.SpotifyObj.Util.CustomCall("DELETE", "me/tracks?ids=" . this.id)
}
Play() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/player/play",, false, JSON.Dump({"uris": ["spotify:track:" . this.id]}))
}
}
class album {
__New(Albumjson, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := Albumjson
this.artists := this.json["artists"]
this.genres := this.json["genres"]
this.id := this.json["id"]
this.images := this.json["images"]
this.name := this.json["name"]
this.uri := this.json["uri"]
this.tracks := []
this.context := new context({"uri": this.uri}, this.SpotifyObj)
for k, v in this.json["tracks"]["items"] {
this.tracks.Push(new track(v, this.SpotifyObj))
}
}
;__Get(this, key) {
; this.key := this.SpotifyObj.Albums.GetAlbum(this.id).key
;}
IsSaved[] {
Get {
return (this.SpotifyObj.Util.CustomCall("GET", "me/albums/contains?ids=" . this.id) ~= "true" ? true : false)
}
}
Play() {
return this.context.SwitchTo()
}
Save() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/albums?ids=" . this.id)
}
UnSave() {
return this.SpotifyObj.Util.CustomCall("DELETE", "me/albums?ids=" . this.id)
}
}
class device {
__New(Devicejson, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := Devicejson
this.id := this.json["id"]
this.IsActive := this.json["is_active"]
this.IsPrivate := this.json["is_private_session"]
this.name := this.json["name"]
this.type := this.json["type"]
this.volume := this.json["volume_percent"]
}
SwitchTo() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/player",, false, JSON.Dump({"device_ids": [this.id]}))
}
}
class context {
__New(Contextjson, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := Contextjson
this.uri := this.json["uri"]
this.type := this.json["type"]
}
SwitchTo() {
return this.SpotifyObj.Util.CustomCall("PUT", "me/player/play",, false, JSON.Dump({"context_uri": this.uri}))
}
}
class artist {
__New(Artistjson, ByRef Parent := "") {
this.SpotifyObj := Parent
this.json := Artistjson
this.genres := this.json["genres"]
this.id := this.json["id"]
this.images := this.json["images"]
this.name := this.json["name"]
this.uri := this.json["uri"]
}
GetAlbums() {
resp := JSON.load(this.SpotifyObj.Util.CustomCall("GET", "https://api.spotify.com/v1/artists/" . this.id . "/albums?limit=1"))
RetVar := []
RetVar[1] := ""
RetVar.SetCapacity(resp["total"])
loop, % Ceil(resp["total"]/50) {
for k, v in JSON.Load(this.SpotifyObj.Util.CustomCall("GET", "artists/" . this.id . "/albums?limit=50&offset=" . ((A_Index - 1 ) * 50)))["items"] {
RetVar.Push(new album(v, this.SpotifyObj))
}
}
RetVar.RemoveAt(1)
return RetVar
}
; Jesus, I have just fucking ruined the global namespace. TODO -- Nest these somewhere
; TODO -- Get a plugin that actually makes "TODO --" do something
}
class user {
__New(Userjson, ByRef Parent := "", isCur := false) {
this.SpotifyObj := Parent
this.json := Userjson
this.isCur := isCur
this.birthdate := this.json["birthdate"]
this.name := this.json["display_name"]
this.email := this.json["email"]
this.id := this.json["id"]
this.subscriptionLevel := this.json["product"]
}
GetPlaylists() {
; I don't know why, but I couldn't return the array of playlists generated by this function properly, so ignore the stuff with RetVer
; Maybe some limitation on pushing large objects onto an array
resp := JSON.load(this.SpotifyObj.Util.CustomCall("GET", "users/" . this.id . "/playlists?limit=1"))
RetVar := []
RetVar[1] := ""
RetVar.SetCapacity(resp["total"])
loop, % Ceil(resp["total"]/50) {
for k, v in JSON.load(this.SpotifyObj.Util.CustomCall("GET", "users/" . this.id . "/playlists?limit=50&offset=" . ((A_Index - 1 ) * 50)))["items"] {
RetVar.Push(new playlist(v, this.SpotifyObj))
}
}
RetVar.RemoveAt(1)
return RetVar
}
GetTop(ArtistsOrTracks := "tracks") {
if !(this.isCur) {
return ""
}
RetVar := []
for k, v in JSON.load(this.SpotifyObj.Util.CustomCall("GET", "me/top/" . ArtistsOrTracks))["items"] {
if (ArtistsOrTracks = "artists") {
RetVar.Push(new artist(v, this.SpotifyObj))
}
else {
RetVar.Push(new track(v, this.SpotifyObj))
}
}
return RetVar
}
}
#Include <AHKsock>
#Include <AHKhttp>
#Include <crypt>
#Include <json>