Merge pull request #77 from blha303/patch-7

soundc.py: Remove dependency on soundcloud plugin, add description truncating
This commit is contained in:
Luke Rogers 2013-07-22 08:06:22 -07:00
commit fd4a8ac6fa
2 changed files with 28 additions and 8 deletions

View file

@ -1,18 +1,39 @@
from util import hook, http, web from util import hook, http, web
import soundcloud from urllib import urlencode
import re import re
sc_re = (r'(.*:)//(www.)?(soundcloud.com)(.*)', re.I) sc_re = (r'(.*:)//(www.)?(soundcloud.com)(.*)', re.I)
def truncate(msg):
nmsg = msg.split(" ")
out = None
x = 0
for i in nmsg:
if x <= 7:
if out:
out = out + " " + nmsg[x]
else:
out = nmsg[x]
x = x + 1
if x <= 7:
return out
else:
return out + "..."
@hook.regex(*sc_re) @hook.regex(*sc_re)
def soundcloud_url(match, bot=None): def soundcloud_url(match, bot=None):
api_key = bot.config.get("api_keys", {}).get("soundcloud") api_key = bot.config.get("api_keys", {}).get("soundcloud")
if not api_key: if not api_key:
return "Error: no api key set" 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] url = match.group(1).split(' ')[-1] + "//" + (match.group(2) if match.group(2) else "") + match.group(3) + match.group(4).split(' ')[0]
client = soundcloud.Client(client_id=api_key) apiurl = "http://api.soundcloud.com"
track = client.get('/resolve', url=url) track = http.get_json(apiurl + '/resolve.json?' + urlencode({'url': url, 'client_id': api_key}))
try: if track['description']:
return "SoundCloud track: \x02%s\x02 by \x02%s\x02 - \x02%s\x02" % (track.title, track.user['username'], web.isgd(track.permalink_url)) desc = ": %s " % truncate(track['description'])
except: else:
return "SoundCloud user: \x02%s\x02 - \x02%s\x02" % (track.username, web.isgd(track.permalink_url)) desc = ""
if track['genre'] != "":
genre = "- Genre: \x02%s\x02 " % track['genre']
else:
genre = ""
return "SoundCloud track: \x02%s\x02 by \x02%s\x02 %s%s- %s plays, %s downloads, %s comments - \x02%s\x02" % (track['title'], track['user']['username'], desc, genre, track['playback_count'], track['download_count'], track['comment_count'], web.isgd(track['permalink_url']))

View file

@ -3,4 +3,3 @@ lxml==3.1beta1
pyenchant==1.6.5 pyenchant==1.6.5
pydns>=2.3.6 pydns>=2.3.6
BeautifulSoup4 BeautifulSoup4
soundcloud==0.3.6