diff --git a/plugins/spotify.py b/plugins/spotify.py index 45c8a8ff0..1a2ba7bca 100644 --- a/plugins/spotify.py +++ b/plugins/spotify.py @@ -1,13 +1,9 @@ import re - import requests from cloudbot import hook -from cloudbot.util import web - -gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address -spuri = 'spotify:{}:{}' +api_url = "https://api.spotify.com/v1/search?" spotify_re = re.compile(r'(spotify:(track|album|artist|user):([a-zA-Z0-9]+))', re.I) http_re = re.compile(r'(open\.spotify\.com/(track|album|artist|user)/' @@ -17,63 +13,84 @@ @hook.command('spotify', 'sptrack') def spotify(text): """spotify -- Search Spotify for """ - params = {'q': text.strip()} - - request = requests.get('http://ws.spotify.com/search/1/track.json', params=params) + params = { + "q": text.strip(), + "offset": 0, + "limit": 1, + "type": "track" + } + + request = requests.get(api_url, params=params) if request.status_code != requests.codes.ok: return "Could not get track information: {}".format(request.status_code) - data = request.json() + data = request.json()["tracks"]["items"][0] try: - _type, _id = data["tracks"][0]["href"].split(":")[1:] + artist, url, song, uri = (data["artists"][0]["name"], + data["external_urls"]["spotify"], + data["name"], + data["uri"]) except IndexError: - return "Could not find track." - url = web.try_shorten(gateway.format(_type, _id)) + return "Unable to find any tracks!" - return "\x02{}\x02 by \x02{}\x02 - {}".format(data["tracks"][0]["name"], - data["tracks"][0]["artists"][0]["name"], url) + return "\x02{}\x02 by \x02{}\x02 - {} / {}".format(song, artist, + url, uri) -@hook.command +@hook.command("spalbum") def spalbum(text): """spalbum -- Search Spotify for """ - params = {'q': text.strip()} - - request = requests.get('http://ws.spotify.com/search/1/album.json', params=params) + params = { + "q": text.strip(), + "offset": 0, + "limit": 1, + "type": "album" + } + + request = requests.get(api_url, params=params) if request.status_code != requests.codes.ok: return "Could not get album information: {}".format(request.status_code) - data = request.json() + data = request.json()["albums"]["items"][0] try: - _type, _id = data["albums"][0]["href"].split(":")[1:] + artist, name, url, uri = (data["artists"][0]["name"], + data["name"], + data["external_urls"]["spotify"], + data["uri"]) + except IndexError: - return "Could not find album." - url = web.try_shorten(gateway.format(_type, _id)) + return "Unable to find any albums!" - return "\x02{}\x02 by \x02{}\x02 - {}".format(data["albums"][0]["name"], - data["albums"][0]["artists"][0]["name"], url) + return "\x02{}\x02 by \x02{}\x02 - {} / {}".format(name, artist, + url, uri) -@hook.command +@hook.command("spartist", "artist") def spartist(text): """spartist -- Search Spotify for """ - params = {'q': text.strip()} - - request = requests.get('http://ws.spotify.com/search/1/artist.json', params=params) + params = { + "q": text.strip(), + "offset": 0, + "limit": 1, + "type": "artist" + } + + request = requests.get(api_url, params=params) if request.status_code != requests.codes.ok: return "Could not get artist information: {}".format(request.status_code) - data = request.json() + data = request.json()["artists"]["items"][0] try: - _type, _id = data["artists"][0]["href"].split(":")[1:] + artist, url, uri = (data["name"], + data["external_urls"]["spotify"], + data["uri"]) except IndexError: - return "Could not find artist." - url = web.try_shorten(gateway.format(_type, _id)) + return "Unable to find any artists!" - return "\x02{}\x02 - {}".format(data["artists"][0]["name"], url) + return "\x02{}\x02 - {} / {}".format(artist, url, uri) @hook.regex(http_re) @@ -81,20 +98,21 @@ def spartist(text): 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 - params = {'uri': url} - request = requests.get('http://ws.spotify.com/search/1/artist.json', params=params) - if request.status_code != requests.codes.ok: - return - data = request.json() + if _type == "track": - name = data["track"]["name"] - artist = data["track"]["artists"][0]["name"] - album = data["track"]["album"]["name"] + request = requests.get("https://api.spotify.com/v1/tracks/{}".format(spotify_id)) + data = request.json() + + return "Spotify Track: \x02{}\x02 by \x02{}\x02 from the album \x02{}\x02".format(data["name"], data["album"]["artists"][0]["name"], data["album"]["name"]) - return "Spotify Track: \x02{}\x02 by \x02{}\x02 from the album \x02{}\x02".format(name, artist, album) elif _type == "artist": - return "Spotify Artist: \x02{}\x02".format(data["artist"]["name"]) + request = requests.get("https://api.spotify.com/v1/artists/{}".format(spotify_id)) + data = request.json() + + return "Spotify Artist: \x02{}\x02, followers: \x02{}\x02, genres: \x02{}\x02".format(data["name"], data["followers"]["total"], ', '.join(data["genres"])) + elif _type == "album": - return "Spotify Album: \x02{}\x02 - \x02{}\x02".format(data["album"]["artist"], data["album"]["name"]) + request = requests.get("https://api.spotify.com/v1/albums/{}".format(spotify_id)) + data = request.json() + + return "Spotify Album: \x02{}\x02 by \x02{}\x02".format(data["name"], data["artists"][0]["name"])