This commit is contained in:
Steven Smith 2013-02-01 06:47:25 +08:00
parent 30febf051c
commit c357bfed82
3 changed files with 481 additions and 22 deletions

View file

@ -1,33 +1,26 @@
import re
import time
import spotimeta
from util import hook, http
spotify_re = (r'(open\.spotify\.com/(track|album|artist|user)'
'([a-zA-Z0-9]+{22})', re.I)
spotify_re = (r'open\.spotify\.com\/track\/'
'([a-z\d]{22})', re.I)
gateway = 'http://ws.spotify.com/lookup/1/' # http spotify gw address
base_url = 'http://ws.spotify.com/'
api_url = base_url + 'lookup/1/.json?uri=spotify:track:{}'
track_url = "spotify://track:"
def get_video_description(spotify_id):
request = http.get_json(api_url.format(spotify_id))
if request.get('error'):
return spotify_id
data = request['track']
out = '\x02%s\x02' % data['name']
out += ', by %s' % data['artists']['name']
out += ' from the album %s released in ' % data['album']['name']
out += '%s' % data['album']['released']
return out
spotify_track_res = ( re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
re.compile(r'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})') )
def get_spotify_ids(s):
for r in spotify_track_res:
for type, track in r.findall(s):
yield type, track
@hook.regex(*spotify_re)
def spotify_url(match):
return get_video_description(match.group(1))
for type, spotify_id in get_spotify_ids(match):
url = '%s?uri=spotify:%s:%s' %(gateway, type, spotify_id)
track = spotimeta.lookup(url)
out = track["result"]["artist"]["name"], "-", track["result"]["name"]
return out