Created
May 30, 2015 18:47
-
-
Save jpallari/954d4be7c30c92b8d1e2 to your computer and use it in GitHub Desktop.
Spotify lookup in command line
This file contains hidden or 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
#!/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