This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/spotify.py

107 lines
4.2 KiB
Python
Raw Normal View History

2013-01-31 17:16:22 +01:00
import re
2014-02-14 04:36:57 +01:00
from urllib import urlencode
2013-01-31 17:16:22 +01:00
2013-08-11 09:57:16 +02:00
from util import hook, http, web
2013-01-31 17:16:22 +01:00
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)
2013-06-28 11:25:06 +02:00
2013-09-04 12:30:04 +02:00
2013-08-03 11:27:02 +02:00
def sptfy(inp, sptfy=False):
if sptfy:
shortenurl = "http://sptfy.com/index.php"
2013-09-04 12:30:04 +02:00
data = urlencode({'longUrl': inp, 'shortUrlDomain': 1, 'submitted': 1, "shortUrlFolder": 6, "customUrl": "",
"shortUrlPassword": "", "shortUrlExpiryDate": "", "shortUrlUses": 0, "shortUrlType": 0})
2013-08-03 11:27:02 +02:00
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
except:
2013-09-04 12:30:04 +02:00
message = "Unable to shorten URL: %s" % \
soup.find('div', {'class': 'messagebox_text'}).find('p').text.split("<br/>")[0]
2013-08-03 11:27:02 +02:00
return message
else:
return web.try_isgd(inp)
2013-01-31 17:16:22 +01:00
@hook.command('sptrack')
2013-02-06 15:24:30 +01:00
@hook.command
def spotify(inp):
2013-09-04 12:30:04 +02:00
"""spotify <song> -- Search Spotify for <song>"""
try:
2013-09-04 12:30:04 +02:00
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)
2013-02-06 23:54:11 +01:00
try:
type, id = data["tracks"][0]["href"].split(":")[1:]
2013-02-06 23:54:11 +01:00
except IndexError:
return "Could not find track."
2013-07-15 16:46:41 +02:00
url = sptfy(gateway.format(type, id))
2014-03-31 09:23:45 +02:00
return u"\x02{}\x02 by \x02{}\x02 - {}".format(data["tracks"][0]["name"],
2013-09-04 12:30:04 +02:00
data["tracks"][0]["artists"][0]["name"], url)
2013-06-28 11:25:06 +02:00
@hook.command
def spalbum(inp):
2013-09-04 12:30:04 +02:00
"""spalbum <album> -- Search Spotify for <album>"""
try:
2013-09-04 12:30:04 +02:00
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:
return "Could not find album."
2013-07-15 16:46:41 +02:00
url = sptfy(gateway.format(type, id))
2014-03-31 09:23:45 +02:00
return u"\x02{}\x02 by \x02{}\x02 - {}".format(data["albums"][0]["name"],
2013-09-04 12:30:04 +02:00
data["albums"][0]["artists"][0]["name"], url)
2013-06-28 11:25:06 +02:00
@hook.command
def spartist(inp):
2013-09-04 12:30:04 +02:00
"""spartist <artist> -- Search Spotify for <artist>"""
try:
2013-09-04 12:30:04 +02:00
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:
return "Could not find artist."
2013-07-15 16:46:41 +02:00
url = sptfy(gateway.format(type, id))
2014-03-31 09:23:45 +02:00
return u"\x02{}\x02 - {}".format(data["artists"][0]["name"], url)
2013-06-28 11:25:06 +02:00
2013-09-04 12:30:04 +02:00
@hook.regex(*http_re)
2013-01-31 17:16:22 +01:00
@hook.regex(*spotify_re)
def spotify_url(match):
type = match.group(2)
2013-02-06 07:16:13 +01:00
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"]
artist = data["track"]["artists"][0]["name"]
album = data["track"]["album"]["name"]
2014-03-31 09:23:45 +02:00
return u"Spotify Track: \x02{}\x02 by \x02{}\x02 from the album \x02{}\x02 - {}".format(name, artist,
2013-09-04 12:30:04 +02:00
album, sptfy(
gateway.format(type, spotify_id)))
elif type == "artist":
2014-03-31 09:23:45 +02:00
return u"Spotify Artist: \x02{}\x02 - {}".format(data["artist"]["name"],
2013-09-04 12:30:04 +02:00
sptfy(gateway.format(type, spotify_id)))
elif type == "album":
2014-03-31 09:23:45 +02:00
return u"Spotify Album: \x02{}\x02 - \x02{}\x02 - {}".format(data["album"]["artist"],
2013-09-04 12:30:04 +02:00
data["album"]["name"],
sptfy(gateway.format(type, spotify_id)))