Merge pull request #75 from blha303/patch-6
Add sptfy URL shortener to spotify.py
This commit is contained in:
commit
8873d61c0a
1 changed files with 30 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
from urllib import urlencode
|
||||||
|
|
||||||
gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address
|
gateway = 'http://open.spotify.com/{}/{}' # http spotify gw address
|
||||||
spuri = 'spotify:{}:{}'
|
spuri = 'spotify:{}:{}'
|
||||||
|
@ -9,6 +10,23 @@ 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)
|
||||||
|
|
||||||
|
def sptfy(inp):
|
||||||
|
# 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, "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
|
||||||
|
except:
|
||||||
|
message = "Unable to shorten URL: %s" % soup.find('div', {'class': 'messagebox_text'}).find('p').text.split("<br/>")[0]
|
||||||
|
return message
|
||||||
|
|
||||||
@hook.command('sptrack')
|
@hook.command('sptrack')
|
||||||
@hook.command
|
@hook.command
|
||||||
|
@ -23,9 +41,8 @@ def spotify(inp):
|
||||||
type, id = data["tracks"][0]["href"].split(":")[1:]
|
type, id = data["tracks"][0]["href"].split(":")[1:]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return "Could not find track."
|
return "Could not find track."
|
||||||
url = gateway.format(type, id)
|
url = sptfy(gateway.format(type, id))
|
||||||
return u"{} by {} - {}".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url)
|
return u"\x02{}\x02 by \x02{}\x02 - \x02{}\x02".format(data["tracks"][0]["name"], data["tracks"][0]["artists"][0]["name"], url)
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def spalbum(inp):
|
def spalbum(inp):
|
||||||
|
@ -39,9 +56,8 @@ def spalbum(inp):
|
||||||
type, id = data["albums"][0]["href"].split(":")[1:]
|
type, id = data["albums"][0]["href"].split(":")[1:]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return "Could not find album."
|
return "Could not find album."
|
||||||
url = gateway.format(type, id)
|
url = sptfy(gateway.format(type, id))
|
||||||
return u"{} by {} - {}".format(data["albums"][0]["name"], data["albums"][0]["artists"][0]["name"], url)
|
return u"\x02{}\x02 by \x02{}\x02 - \x02{}\x02".format(data["albums"][0]["name"], data["albums"][0]["artists"][0]["name"], url)
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def spartist(inp):
|
def spartist(inp):
|
||||||
|
@ -55,9 +71,8 @@ def spartist(inp):
|
||||||
type, id = data["artists"][0]["href"].split(":")[1:]
|
type, id = data["artists"][0]["href"].split(":")[1:]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return "Could not find artist."
|
return "Could not find artist."
|
||||||
url = gateway.format(type, id)
|
url = sptfy(gateway.format(type, id))
|
||||||
return u"{} - {}".format(data["artists"][0]["name"], url)
|
return u"\x02{}\x02 - \x02{}\x02".format(data["artists"][0]["name"], url)
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(*http_re)
|
@hook.regex(*http_re)
|
||||||
@hook.regex(*spotify_re)
|
@hook.regex(*spotify_re)
|
||||||
|
@ -71,8 +86,8 @@ def spotify_url(match):
|
||||||
name = data["track"]["name"]
|
name = data["track"]["name"]
|
||||||
artist = data["track"]["artists"][0]["name"]
|
artist = data["track"]["artists"][0]["name"]
|
||||||
album = data["track"]["album"]["name"]
|
album = data["track"]["album"]["name"]
|
||||||
return u"Spotify Track: {} by {} from the album {}".format(name, artist, album)
|
return u"Spotify Track: \x02{}\x02 by \x02{}\x02 from the album \x02{}\x02 - \x02{}\x02".format(name, artist, album, sptfy(gateway.format(type, spotify_id)))
|
||||||
elif type == "artist":
|
elif type == "artist":
|
||||||
return u"Spotify Artist: {}".format(data["artist"]["name"])
|
return u"Spotify Artist: \x02{}\x02 - \x02{}\x02".format(data["artist"]["name"], sptfy(gateway.format(type, spotify_id)))
|
||||||
elif type == "album":
|
elif type == "album":
|
||||||
return u"Spotify Album: {} - {}".format(data["album"]["artist"], data["album"]["name"])
|
return u"Spotify Album: \x02{}\x02 - \x02{}\x02 - \x02{}\x02".format(data["album"]["artist"], data["album"]["name"], sptfy(gateway.format(type, spotify_id)))
|
||||||
|
|
Reference in a new issue