diff --git a/plugins/spotify.py b/plugins/spotify.py index 3db2758..68543c8 100644 --- a/plugins/spotify.py +++ b/plugins/spotify.py @@ -3,20 +3,24 @@ import re from util import hook, http from urllib import urlencode -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 def sptfy(inp): - """sptfy url - Shorten spotify url. There's NO REASON to use this command, just say the URL and the bot will return all info for it, AND the short url.""" -# login = http.get("http://sptfy.com/login.php", post_data=urlencode({'loginUsername': 'xDCloudBot', 'loginPassword': 'rjgw67kf', 'submit': '', 'submitme': '1'}), cookies=True) +# try: +# login = http.get("http://sptfy.com/login.php", post_data=urlencode({'loginUsername': 'xDCloudBot', 'loginPassword': 'rjgw67kf', 'submit': '', 'submitme': '1'}), cookies=True) +# except Exception as e: +# return inp shortenurl = "http://sptfy.com/index.php" - data = urlencode({'longUrl': inp, 'shortUrlDomain': 1, 'submitted': 1, "customUrl": "", "shortUrlPassword": "", "shortUrlExpiryDate": "", "shortUrlUses": 0, "shortUrlType": 0}) - soup = http.get_soup(shortenurl, post_data=data, cookies=True) + data = urlencode({'longUrl': inp, 'shortUrlDomain': 1, 'submitted': 1, "shortUrlFolder": 6, "customUrl": "", "shortUrlPassword": "", "shortUrlExpiryDate": "", "shortUrlUses": 0, "shortUrlType": 0}) + try: + soup = http.get_soup(shortenurl, post_data=data, cookies=True) + except: + return inp try: link = soup.find('div', {'class': 'resultLink'}).text.strip() return link @@ -28,7 +32,11 @@ def sptfy(inp): @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: @@ -39,7 +47,11 @@ def spotify(inp): @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: @@ -50,7 +62,11 @@ def spalbum(inp): @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: @@ -64,6 +80,7 @@ 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"]