Skip to content

Commit cba34f6

Browse files
author
Kamran Mackey
committed
Added a SoundCloud group command & also added regex for it.
1 parent e06c585 commit cba34f6

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

plugins/soundcloud.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from cloudbot.util import web, formatting, timeformat
66

77
SC_RE = re.compile(r'(.*:)//(www.)?(soundcloud.com|snd.sc)(.*)', re.I)
8-
API_BASE = "http://api.soundcloud.com/{}/"
8+
SCGROUP_RE = re.compile(r'(.*:)//(www.)?(soundcloud.com/group)(.*)', re.I)
9+
API_BASE = 'http://api.soundcloud.com/{}/'
910

1011

1112
class APIError(Exception):
@@ -84,10 +85,13 @@ def format_user(user, show_url=True):
8485
out = "\x02{}\x02".format(user['username'])
8586

8687
if user['description']:
87-
out += ': "{}"'.format(formatting.truncate(user['description']))
88+
out += ': "{}"'.format(user['description'])
89+
90+
if user['city']:
91+
out += ': {}'.format(user['city'])
8892

8993
if user['country']:
90-
out += " - {}".format(formatting.truncate(user['country']))
94+
out += ", {}".format(formatting.truncate(user['country']))
9195

9296
out += " - \x02{track_count:,}\x02 tracks, \x02{playlist_count:,}\x02 playlists, \x02{followers_count:,}\x02 " \
9397
"followers, \x02{followings_count:,}\x02 followed".format(**user)
@@ -123,6 +127,21 @@ def format_playlist(playlist, show_url=True):
123127
out += " - {}".format(web.try_shorten(playlist['permalink_url']))
124128
return out
125129

130+
def format_group(group, show_url=True):
131+
"""
132+
Takes a SoundCloud group and returns a formatting string.
133+
"""
134+
out = "\x02{}\x02".format(group['name'])
135+
136+
if group['description']:
137+
out += ': "{}"'.format(formatting.truncate(group['description']))
138+
139+
out += " - Owned by \x02{}\x02.".format(group['creator']['username'])
140+
141+
if show_url:
142+
out += " - {}".format(web.try_shorten(group['permalink_url']))
143+
return out
144+
126145

127146
# CLOUDBOT HOOKS
128147
@hook.on_start()
@@ -168,6 +187,24 @@ def soundcloud_user(text):
168187
except APIError as ae:
169188
return ae
170189

190+
@hook.command("scgroup")
191+
def soundcloud_group(text):
192+
"""<query> -- Searches for groups on SoundCloud."""
193+
if not api_key:
194+
return "This command requires a SoundCloud API key."
195+
try:
196+
group = get_with_search('groups', text)
197+
except APIError as ae:
198+
return ae
199+
200+
if not group:
201+
return "No results found."
202+
203+
try:
204+
return format_group(group)
205+
except APIError as ae:
206+
return ae
207+
171208

172209
@hook.regex(SC_RE)
173210
def soundcloud_url(match):
@@ -187,3 +224,5 @@ def soundcloud_url(match):
187224
return format_user(item, show_url=False)
188225
elif item['kind'] == 'playlist':
189226
return format_playlist(item, show_url=False)
227+
elif item['kind'] == 'group':
228+
return format_group(item, show_url=False)

0 commit comments

Comments
 (0)