Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed spotify.py to use the new v1 API #252

Merged
merged 2 commits into from Oct 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 64 additions & 46 deletions plugins/spotify.py
Original file line number Diff line number Diff line change
@@ -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)/'
Expand All @@ -17,84 +13,106 @@
@hook.command('spotify', 'sptrack')
def spotify(text):
"""spotify <song> -- Search Spotify for <song>"""
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 <album> -- Search Spotify for <album>"""
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 <artist> -- Search Spotify for <artist>"""
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)
@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
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"])