Merge pull request #99 from blha303/patch-1
Add snd.sc url catcher, change return contents for invalid urls, move soundcloud lookup into another function
This commit is contained in:
commit
e5fb97f128
1 changed files with 24 additions and 12 deletions
|
@ -4,15 +4,10 @@ import re
|
|||
|
||||
sc_re = (r'(.*:)//(www.)?(soundcloud.com)(.*)', re.I)
|
||||
api_url = "http://api.soundcloud.com"
|
||||
sndsc_re = (r'(.*:)//(www.)?(snd.sc)(.*)', re.I)
|
||||
|
||||
|
||||
@hook.regex(*sc_re)
|
||||
def soundcloud_url(match, bot=None):
|
||||
api_key = bot.config.get("api_keys", {}).get("soundcloud")
|
||||
if not api_key:
|
||||
return "error: no api key set"
|
||||
|
||||
url = match.group(1).split(' ')[-1] + "//" + (match.group(2) if match.group(2) else "") + match.group(3) + match.group(4).split(' ')[0]
|
||||
def soundcloud(url, api_key):
|
||||
data = http.get_json(api_url + '/resolve.json?' + urlencode({'url': url, 'client_id': api_key}))
|
||||
|
||||
if data['description']:
|
||||
|
@ -24,9 +19,26 @@ def soundcloud_url(match, bot=None):
|
|||
else:
|
||||
genre = ""
|
||||
|
||||
try:
|
||||
url = web.isgd(data['permalink_url'])
|
||||
except (web.ShortenError, http.HTTPError):
|
||||
url = data['permalink_url']
|
||||
url = web.try_isgd(data['permalink_url'])
|
||||
|
||||
return u"SoundCloud track: \x02{}\x02 by \x02{}user\x02 {}{}- {} plays, {} downloads, {} comments - \x02{}\x02".format(data['title'], data['user']['username'], desc, genre, data['playback_count'], data['download_count'], data['comment_count'], url)
|
||||
|
||||
|
||||
@hook.regex(*sc_re)
|
||||
def soundcloud_url(match, bot=None):
|
||||
api_key = bot.config.get("api_keys", {}).get("soundcloud")
|
||||
if not api_key:
|
||||
print "Error: no api key set"
|
||||
return None
|
||||
url = match.group(1).split(' ')[-1] + "//" + (match.group(2) if match.group(2) else "") + match.group(3) + match.group(4).split(' ')[0]
|
||||
return soundcloud(url, api_key)
|
||||
|
||||
|
||||
@hook.regex(*sndsc_re)
|
||||
def sndsc_url(match, bot=None):
|
||||
api_key = bot.config.get("api_keys", {}).get("soundcloud")
|
||||
if not api_key:
|
||||
print "Error: no api key set"
|
||||
return None
|
||||
url = match.group(1).split(' ')[-1] + "//" + (match.group(2) if match.group(2) else "") + match.group(3) + match.group(4).split(' ')[0]
|
||||
return soundcloud(http.open(url).url, api_key)
|
||||
|
|
Reference in a new issue