Merge pull request #62 from blha303/patch-1
spotify.py - Removed spotimeta dependency, using json api natively
This commit is contained in:
commit
16ad0cfbf6
|
@ -1,9 +1,9 @@
|
|||
import re
|
||||
import spotimeta
|
||||
|
||||
from util import hook, http
|
||||
|
||||
gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address
|
||||
spuri = 'spotify:{}:{}'
|
||||
|
||||
spotify_re = (r'(spotify:(track|album|artist|user):([a-zA-Z0-9]+))', re.I)
|
||||
http_re = (r'(open\.spotify\.com\/(track|album|artist|user)\/'
|
||||
|
@ -12,13 +12,13 @@ http_re = (r'(open\.spotify\.com\/(track|album|artist|user)\/'
|
|||
@hook.command
|
||||
def spotify(inp):
|
||||
"spotify <song> -- Search Spotify for <song>"
|
||||
data = spotimeta.search_track(inp.strip())
|
||||
data = http.get_json("http://ws.spotify.com/search/1/track.json", q=inp.strip())
|
||||
try:
|
||||
type, id = data["result"][0]["href"].split(":")[1:]
|
||||
type, id = data["tracks"][0]["href"].split(":")[1:]
|
||||
except IndexError:
|
||||
return "Could not find track."
|
||||
url = gateway.format(type, id)
|
||||
return u"{} by {} - {}".format(data["result"][0]["name"], data["result"][0]["artist"]["name"], url)
|
||||
return u"{} by {} - {}".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url)
|
||||
|
||||
|
||||
@hook.regex(*http_re)
|
||||
|
@ -26,11 +26,14 @@ def spotify(inp):
|
|||
def spotify_url(match):
|
||||
type = match.group(2)
|
||||
spotify_id = match.group(3)
|
||||
url = gateway.format(type, spotify_id)
|
||||
data = spotimeta.lookup(url)
|
||||
url = spuri.format(type, spotify_id)
|
||||
data = http.get_json("http://ws.spotify.com/lookup/1/.json", uri=url)
|
||||
if type == "track":
|
||||
return u"Spotify Track: {} by {} from the album {}".format(data["result"]["name"], data["result"]["artist"]["name"], data["result"]["album"]["name"])
|
||||
name = data["track"]["name"]
|
||||
artist = data["track"]["artists"][0]["name"]
|
||||
album = data["track"]["album"]["name"]
|
||||
return u"Spotify Track: {} by {} from the album {}".format(name, artist, album) // shortened
|
||||
elif type == "artist":
|
||||
return u"Spotify Artist: {}".format(data["result"]["name"])
|
||||
return u"Spotify Artist: {}".format(data["artist"]["name"])
|
||||
elif type == "album":
|
||||
return u"Spotify Album: {} - {}".format(data["result"]["artist"]["name"], data["result"]["name"])
|
||||
return u"Spotify Album: {} - {}".format(data["album"]["artist"], data["album"]["name"])
|
||||
|
|
Reference in a new issue