catch more errors, pep8

This commit is contained in:
Luke Rogers 2013-06-28 21:25:06 +12:00
parent b98b6ad933
commit e091a48744

View file

@ -2,18 +2,23 @@ import re
from util import hook, http from util import hook, http
gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address
spuri = 'spotify:{}:{}' spuri = 'spotify:{}:{}'
spotify_re = (r'(spotify:(track|album|artist|user):([a-zA-Z0-9]+))', re.I) spotify_re = (r'(spotify:(track|album|artist|user):([a-zA-Z0-9]+))', re.I)
http_re = (r'(open\.spotify\.com\/(track|album|artist|user)\/' 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('sptrack')
@hook.command @hook.command
def spotify(inp): def spotify(inp):
"spotify <song> -- Search Spotify for <song>" "spotify <song> -- Search Spotify for <song>"
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: try:
type, id = data["tracks"][0]["href"].split(":")[1:] type, id = data["tracks"][0]["href"].split(":")[1:]
except IndexError: except IndexError:
@ -21,10 +26,15 @@ def spotify(inp):
url = gateway.format(type, id) url = gateway.format(type, id)
return u"{} by {} - {}".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url) return u"{} by {} - {}".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url)
@hook.command @hook.command
def spalbum(inp): def spalbum(inp):
"spalbum <album> -- Search Spotify for <album>" "spalbum <album> -- Search Spotify for <album>"
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: try:
type, id = data["albums"][0]["href"].split(":")[1:] type, id = data["albums"][0]["href"].split(":")[1:]
except IndexError: except IndexError:
@ -32,10 +42,15 @@ def spalbum(inp):
url = gateway.format(type, id) url = gateway.format(type, id)
return u"{} by {} - {}".format(data["albums"][0]["name"], data["albums"][0]["artists"][0]["name"], url) return u"{} by {} - {}".format(data["albums"][0]["name"], data["albums"][0]["artists"][0]["name"], url)
@hook.command @hook.command
def spartist(inp): def spartist(inp):
"spartist <artist> -- Search Spotify for <artist>" "spartist <artist> -- Search Spotify for <artist>"
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: try:
type, id = data["artists"][0]["href"].split(":")[1:] type, id = data["artists"][0]["href"].split(":")[1:]
except IndexError: except IndexError:
@ -43,12 +58,14 @@ def spartist(inp):
url = gateway.format(type, id) url = gateway.format(type, id)
return u"{} - {}".format(data["artists"][0]["name"], url) return u"{} - {}".format(data["artists"][0]["name"], url)
@hook.regex(*http_re) @hook.regex(*http_re)
@hook.regex(*spotify_re) @hook.regex(*spotify_re)
def spotify_url(match): def spotify_url(match):
type = match.group(2) type = match.group(2)
spotify_id = match.group(3) spotify_id = match.group(3)
url = spuri.format(type, spotify_id) 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) data = http.get_json("http://ws.spotify.com/lookup/1/.json", uri=url)
if type == "track": if type == "track":
name = data["track"]["name"] name = data["track"]["name"]