Skip to content

Instantly share code, notes, and snippets.

@anonymous1184
Last active November 11, 2022 13:39
Show Gist options
  • Save anonymous1184/43f6acbbf4d3a39a3b793ee7be50f7ed to your computer and use it in GitHub Desktop.
Save anonymous1184/43f6acbbf4d3a39a3b793ee7be50f7ed to your computer and use it in GitHub Desktop.
Spotify Keys (API)

; Version: 2022.11.11.1
; About: https://redd.it/orzend
#Include %A_LineFile%\..\Spotify_Api.ahk
class Spotify_Keys extends Spotify_Actions {
_baseEndpoint := "https://api.spotify.com/v1/me/player/"
__New(Arguments*) {
this._api := new Spotify_Api(Arguments*)
}
}
class Spotify_Actions {
__Call(Action, Adjustment:=0) {
static methods := {next:"POST", pause:"PUT", play:"PUT", previous:"POST", repeat:"PUT", shuffle:"PUT", volume:"PUT"}
Action := Format("{:L}", Action)
if (Action ~= "^(repeat|shuffle|toggleplay|volume)$")
player := this._api.Rest("GET", this._baseEndpoint)
if (Action = "toggleplay")
Action := player.is_playing ? "pause" : "play"
if !(method := methods[Action])
throw Exception("Action not available.", -1, Action)
endpoint := this._baseEndpoint Action
if (Action = "shuffle") {
opts := {false:"true", true:"false"}
endpoint .= "?state=" opts[player.shuffle_state]
} else if (Action = "repeat") {
opts := {context:"track", off:"context", track:"off"}
endpoint .= "?state=" opts[player.repeat_state]
} else if (Action = "volume") {
volume := player.device.volume_percent + Adjustment
volume := Max(0, Min(100, volume))
endpoint .= "?volume_percent=" volume
}
this._api.Rest(method, endpoint)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment