How to get Spotify current playing song in Python (Windows)?

Question:

I want to show the current playing song in Spotify on a 16×2 LCD.
I was thinking of connecting the LCD with my Arduino and then making a Python script that sends the current playing song of Spotify to the Arduino.

To get to the point, I’m looking for a way to get Spotify’s current playing song in Python. (I’m using Windows 8.) I found some ways like dbus, but they were either for Linux or for Mac.

Thanks in advance! (And sorry for bad English grammar.)

Asked By: Kevin

||

Answers:

Is there more than one pytify ?
This worked for me until today:
https://code.google.com/p/pytify/

Spotify has been updated, now song is not shown in windows title anymore.

They will bring the “feature” back:
https://community.spotify.com/t5/ideas/v2/ideapage/blog-id/ideaexchange/article-id/73238/page/2#comments

Answered By: Candas

The easiest way would probably be to scrobble the currently playing tracks from the Spotify client to a last.fm account and then use python to get it from there.

Last.fm allows you to get scrobbled tracks via their api with user.getRecentTracks which provides the nowplaying="true" attribute if a song is playing. It also provides some other useful things you may want for an external display like a link to the album art and last.fm page for the song.

Here’s a quick example that takes a username and api key as cmd line arguments and fetches what is currently playing for that user using the requests library.

from time import sleep
import requests
import json
from pprint import pprint
import sys    

def get_playing():
    base_url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='
    user = sys.argv[1]
    key = sys.argv[2]
    r = requests.get(base_url+user+'&api_key='+key+'&format=json')
    data = json.loads(r.text)
    latest_track = data['recenttracks']['track'][0]
    try:
        if latest_track['@attr']['nowplaying'] == 'true':
            artist = latest_track['artist']['#text']
            song = latest_track['name']
            album = latest_track['album']['#text']
            print "nNow Playing: {0} - {1}, from the album {2}.n".format(artist, song, album)
    except KeyError:
        print 'nNothing playing...n'    

def main():
    if len(sys.argv)<3:
        print "nError: Please provide a username and api key in the format of: 'scrobble.py username api_keyn"
    else:
        get_playing()    

if __name__ == '__main__':
    main()

In a quick test it does seem to take a minute or so to realize the track is no longer playing after pausing or exiting the Spotify client however.

Answered By: Tony W.

I encountered the same issue, so I wrote a library to solve this issue. The library can be found at github: https://github.com/XanderMJ/spotilib. Keep in mind that this is still work in progress.

Just copy the file and place it in your Python/Lib directory.

import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song

spotilib.artist() returns only the first artist. I started working on an other library spotimeta.py to solve this issue. However, this is not working at 100% yet.

import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track

If an error occurs, spotimeta.artists() will return only the first artist (found with spotilib.artist())

Hope this will help you (if still needed)!

Answered By: XanderMJ

while True:
print("Welcome to the project, " + user_name[‘display_name’])

print("0 - Exit the console")

print("1 - Search for a Song")

user_input = int(input("Enter Your Choice: "))

if user_input == 1:
    search_song = input("Enter the song name: ")

    results = spotifyObject.search(search_song, 1, 0, "track")
    songs_dict = results['tracks']
    song_items = songs_dict['items']
    song = song_items[0]['external_urls']['spotify']
    webbrowser.open(song)
    print('Song has opened in your browser.')
elif user_input == 0:

    print("Good Bye, Have a great day!")
    break
else:
    print("Please enter valid user-input.")
Answered By: Asmit Raj
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.