2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
2016-04-06 12:56:12 +00:00
|
|
|
#--require-config soundcloud-api-key
|
|
|
|
|
2018-07-02 13:33:38 +00:00
|
|
|
import json, re, time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-06 12:56:12 +00:00
|
|
|
|
2018-07-02 13:33:38 +00:00
|
|
|
URL_SOUNDCLOUD_TRACK = "http://api.soundcloud.com/tracks"
|
|
|
|
URL_SOUNDCLOUD_RESOLVE = "http://api.soundcloud.com/resolve"
|
2018-07-02 13:34:46 +00:00
|
|
|
REGEX_SOUNDCLOUD = "https?://soundcloud.com/([^/]+)/([^/]+)"
|
2016-04-06 12:56:12 +00:00
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2016-04-06 12:56:12 +00:00
|
|
|
_name = "SoundCloud"
|
|
|
|
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.sc", alias_of="soundcloud")
|
|
|
|
@utils.hook("received.command.soundcloud")
|
2016-04-06 12:56:12 +00:00
|
|
|
def soundcloud(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Search SoundCloud
|
|
|
|
:usage: <term>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-07-02 13:33:38 +00:00
|
|
|
query = None
|
|
|
|
url = None
|
|
|
|
|
|
|
|
if event["args"]:
|
|
|
|
match = re.search(REGEX_SOUNDCLOUD, event["args"])
|
|
|
|
if match:
|
|
|
|
url = match.string
|
2016-04-06 12:56:12 +00:00
|
|
|
else:
|
2018-07-02 13:33:38 +00:00
|
|
|
query = event["args"]
|
|
|
|
else:
|
2018-09-30 10:43:34 +00:00
|
|
|
last_soundcloud = event["target"].buffer.find(REGEX_SOUNDCLOUD)
|
2018-07-02 13:33:38 +00:00
|
|
|
if last_soundcloud:
|
2019-08-13 12:48:03 +00:00
|
|
|
url = last_soundcloud.match
|
2018-07-02 13:33:38 +00:00
|
|
|
|
|
|
|
if not query and not url:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("no search phrase provided")
|
2018-07-02 13:33:38 +00:00
|
|
|
has_query = not query == None
|
|
|
|
get_params = {"limit": 1,
|
|
|
|
"client_id": self.bot.config["soundcloud-api-key"]}
|
|
|
|
|
|
|
|
if query:
|
|
|
|
get_params["q"] = query
|
|
|
|
else:
|
|
|
|
get_params["url"] = url
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(
|
2018-07-02 13:33:38 +00:00
|
|
|
URL_SOUNDCLOUD_TRACK if has_query else URL_SOUNDCLOUD_RESOLVE,
|
|
|
|
get_params=get_params, json=True)
|
|
|
|
|
|
|
|
if page:
|
2019-01-08 10:14:03 +00:00
|
|
|
if len(page.data):
|
|
|
|
page = page.data[0] if has_query else page
|
|
|
|
title = page["title"]
|
|
|
|
user = page["user"]["username"]
|
|
|
|
duration = time.strftime("%H:%M:%S", time.gmtime(page[
|
|
|
|
"duration"]/1000))
|
|
|
|
if duration.startswith("00:"):
|
|
|
|
duration = duration[3:]
|
|
|
|
link = page["permalink_url"]
|
|
|
|
event["stdout"].write("%s [%s] (posted by %s) %s" % (title,
|
|
|
|
duration, user, link))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No results found")
|
2016-04-06 12:56:12 +00:00
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|