Last active
January 18, 2021 11:39
-
-
Save srishanbhattarai/dacf8058699aa3b96d5a7135e61c3817 to your computer and use it in GitHub Desktop.
Get currently playing track on Spotify desktop - MacOS and Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on escape_quotes(string_to_escape) | |
set AppleScript's text item delimiters to the "\"" | |
set the item_list to every text item of string_to_escape | |
set AppleScript's text item delimiters to the "\\\"" | |
set string_to_escape to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return string_to_escape | |
end escape_quotes | |
tell application "Spotify" | |
set ctrack to "{" | |
set ctrack to ctrack & "\"artist\": \"" & my escape_quotes(current track's artist) & "\"" | |
set ctrack to ctrack & ",\"album\": \"" & my escape_quotes(current track's album) & "\"" | |
set ctrack to ctrack & ",\"disc_number\": " & current track's disc number | |
set ctrack to ctrack & ",\"duration\": " & current track's duration | |
set ctrack to ctrack & ",\"played_count\": " & current track's played count | |
set ctrack to ctrack & ",\"track_number\": " & current track's track number | |
set ctrack to ctrack & ",\"popularity\": " & current track's popularity | |
set ctrack to ctrack & ",\"id\": \"" & current track's id & "\"" | |
set ctrack to ctrack & ",\"name\": \"" & my escape_quotes(current track's name) & "\"" | |
set ctrack to ctrack & ",\"album_artist\": \"" & my escape_quotes(current track's album artist) & "\"" | |
set ctrack to ctrack & ",\"artwork_url\": \"" & current track's artwork url & "\"" | |
set ctrack to ctrack & ",\"spotify_url\": \"" & current track's spotify url & "\"" | |
set ctrack to ctrack & "}" | |
return ctrack | |
end tell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"fmt" | |
"runtime" | |
"os/exec" | |
dbus "github.com/godbus/dbus" | |
) | |
// Track is a struct to represent what's mostly synonymous with a Song | |
type Track struct { | |
Genre string | |
Meta Meta | |
Title string `json:"name"` | |
Artist string `json:"artist"` | |
Album string `json:"album"` | |
AlbumArtist string `json:"album_artist"` | |
ArtworkUrl string `json:"artwork_url"` | |
} | |
func main() { | |
var trackIdentifier func() (*Track, error) | |
if runtime.GOOS == "linux" { | |
trackIdentifier = trackIdentifierLinux | |
} else if runtime.GOOS == "darwin" { | |
trackIdentifier = trackIdentifierDarwin | |
} | |
track, _ := trackIdentifier() | |
fmt.Printf("Track: %v\n", track) | |
} | |
// trackIdentifierLinux is the linux variant of a function to identify tracks. | |
func trackIdentifierLinux() (*Track, error) { | |
conn, err := dbus.SessionBus() | |
if err != nil { | |
log.Panicf("Could not open a session bus. Error: %s", err.Error()) | |
} | |
spotify := conn.Object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2") | |
metadata, _ := spotify.GetProperty("org.mpris.MediaPlayer2.Player.Metadata") | |
songData := metadata.Value().(map[string]dbus.Variant) | |
track := Track{ | |
Title: songData["xesam:title"].Value().(string), | |
Artist: songData["xesam:artist"].Value().([]string)[0], | |
Album: songData["xesam:album"].Value().(string), | |
AlbumArtist: songData["xesam:albumArtist"].Value().([]string)[0], | |
ArtworkUrl: songData["mpris:artUrl"].Value().(string), | |
} | |
return &track, nil | |
} | |
// trackIdentifierDarwin is the MacOS variant. | |
func trackIdentifierDarwin() (*Track, error) { | |
cmd := exec.Command("osascript", "spotify.applescript") | |
out, _ := cmd.CombinedOutput() | |
track := &Track{} | |
err := json.Unmarshal(out, track) | |
return track, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment