Skip to content

Instantly share code, notes, and snippets.

@jkpl
Created May 30, 2015 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkpl/954d4be7c30c92b8d1e2 to your computer and use it in GitHub Desktop.
Save jkpl/954d4be7c30c92b8d1e2 to your computer and use it in GitHub Desktop.
Spotify lookup in command line
#!/usr/bin/python
import urllib
import sys
import json
import re
SP_REGEX = re.compile('^http://[\S]+/track/([\S]+)')
SP_LOOKUP_BASEURL = 'http://ws.spotify.com/lookup/1/.json?uri=spotify:track:%s'
def get_track_details(url):
fragment = SP_REGEX.match(url)
if len(fragment.groups()) > 0:
fragment = fragment.group(1)
else:
fragment = url
res = json.loads(urllib.urlopen(SP_LOOKUP_BASEURL % fragment).read())
track = res.get('track')
if track:
return track
else:
return {}
def pp_track(d):
print 'Title : ', d.get('name')
artists = [x.get('name') for x in d.get('artists')]
for a in artists:
print 'Artist : ', a
album = d.get('album')
if album:
print 'Album : ', album.get('name')
if __name__ == '__main__':
if len(sys.argv) > 1:
track = get_track_details(sys.argv[1])
pp_track(track)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment