diff --git a/plugins/spotify.py b/plugins/spotify.py index 7da7d4f..8b35586 100644 --- a/plugins/spotify.py +++ b/plugins/spotify.py @@ -2,18 +2,23 @@ import re from util import hook, http -gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address -spuri = 'spotify:{}:{}' +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)\/' - '([a-zA-Z0-9]+))', re.I) + '([a-zA-Z0-9]+))', re.I) + @hook.command('sptrack') @hook.command def spotify(inp): "spotify -- Search Spotify for " - data = http.get_json("http://ws.spotify.com/search/1/track.json", q=inp.strip()) + try: + data = http.get_json("http://ws.spotify.com/search/1/track.json", q=inp.strip()) + except Exception as e: + return "Could not get track information: {}".format(e) + try: type, id = data["tracks"][0]["href"].split(":")[1:] except IndexError: @@ -21,10 +26,15 @@ def spotify(inp): url = gateway.format(type, id) return u"{} by {} - {}".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url) + @hook.command def spalbum(inp): "spalbum -- Search Spotify for " - data = http.get_json("http://ws.spotify.com/search/1/album.json", q=inp.strip()) + try: + data = http.get_json("http://ws.spotify.com/search/1/album.json", q=inp.strip()) + except Exception as e: + return "Could not get album information: {}".format(e) + try: type, id = data["albums"][0]["href"].split(":")[1:] except IndexError: @@ -32,10 +42,15 @@ def spalbum(inp): url = gateway.format(type, id) return u"{} by {} - {}".format(data["albums"][0]["name"], data["albums"][0]["artists"][0]["name"], url) + @hook.command def spartist(inp): "spartist -- Search Spotify for " - data = http.get_json("http://ws.spotify.com/search/1/artist.json", q=inp.strip()) + try: + data = http.get_json("http://ws.spotify.com/search/1/artist.json", q=inp.strip()) + except Exception as e: + return "Could not get artist information: {}".format(e) + try: type, id = data["artists"][0]["href"].split(":")[1:] except IndexError: @@ -43,12 +58,14 @@ def spartist(inp): url = gateway.format(type, id) return u"{} - {}".format(data["artists"][0]["name"], url) + @hook.regex(*http_re) @hook.regex(*spotify_re) def spotify_url(match): type = match.group(2) spotify_id = match.group(3) url = spuri.format(type, spotify_id) + # no error catching here, if the API is down fail silently data = http.get_json("http://ws.spotify.com/lookup/1/.json", uri=url) if type == "track": name = data["track"]["name"]