55from cloudbot .util import web , formatting , timeformat
66
77SC_RE = re .compile (r'(.*:)//(www.)?(soundcloud.com|snd.sc)(.*)' , re .I )
8- API_BASE = " http://api.soundcloud.com/{}/"
8+ API_BASE = ' http://api.soundcloud.com/{}/'
99
1010
1111class APIError (Exception ):
@@ -86,8 +86,11 @@ def format_user(user, show_url=True):
8686 if user ['description' ]:
8787 out += ': "{}"' .format (formatting .truncate (user ['description' ]))
8888
89+ if user ['city' ]:
90+ out += ': {}' .format (user ['city' ])
91+
8992 if user ['country' ]:
90- out += " - {}" .format (formatting .truncate (user ['country' ]))
93+ out += ", {}" .format (formatting .truncate (user ['country' ]))
9194
9295 out += " - \x02 {track_count:,}\x02 tracks, \x02 {playlist_count:,}\x02 playlists, \x02 {followers_count:,}\x02 " \
9396 "followers, \x02 {followings_count:,}\x02 followed" .format (** user )
@@ -123,6 +126,21 @@ def format_playlist(playlist, show_url=True):
123126 out += " - {}" .format (web .try_shorten (playlist ['permalink_url' ]))
124127 return out
125128
129+ def format_group (group , show_url = True ):
130+ """
131+ Takes a SoundCloud group and returns a formatting string.
132+ """
133+ out = "\x02 {}\x02 " .format (group ['name' ])
134+
135+ if group ['description' ]:
136+ out += ': "{}"' .format (formatting .truncate (group ['description' ]))
137+
138+ out += " - Owned by \x02 {}\x02 ." .format (group ['creator' ]['username' ])
139+
140+ if show_url :
141+ out += " - {}" .format (web .try_shorten (group ['permalink_url' ]))
142+ return out
143+
126144
127145# CLOUDBOT HOOKS
128146@hook .on_start ()
@@ -168,6 +186,24 @@ def soundcloud_user(text):
168186 except APIError as ae :
169187 return ae
170188
189+ @hook .command ("scgroup" )
190+ def soundcloud_group (text ):
191+ """<query> -- Searches for groups on SoundCloud."""
192+ if not api_key :
193+ return "This command requires a SoundCloud API key."
194+ try :
195+ group = get_with_search ('groups' , text )
196+ except APIError as ae :
197+ return ae
198+
199+ if not group :
200+ return "No results found."
201+
202+ try :
203+ return format_group (group )
204+ except APIError as ae :
205+ return ae
206+
171207
172208@hook .regex (SC_RE )
173209def soundcloud_url (match ):
@@ -187,3 +223,5 @@ def soundcloud_url(match):
187223 return format_user (item , show_url = False )
188224 elif item ['kind' ] == 'playlist' :
189225 return format_playlist (item , show_url = False )
226+ elif item ['kind' ] == 'group' :
227+ return format_group (item , show_url = False )
0 commit comments