Add logic in soundcloud.py to accept urls and search back through log for urls
This commit is contained in:
parent
b27610e23d
commit
50bf76d795
1 changed files with 46 additions and 19 deletions
|
@ -1,24 +1,53 @@
|
|||
#--require-config soundcloud-api-key
|
||||
|
||||
import json, time
|
||||
import json, re, time
|
||||
import Utils
|
||||
|
||||
URL_SOUNDCLOUD = "http://api.soundcloud.com/tracks"
|
||||
URL_SOUNDCLOUD_TRACK = "http://api.soundcloud.com/tracks"
|
||||
URL_SOUNDCLOUD_RESOLVE = "http://api.soundcloud.com/resolve"
|
||||
REGEX_SOUNDCLOUD = "https://soundcloud.com/([^/]+)/([^/]+)"
|
||||
|
||||
class Module(object):
|
||||
_name = "SoundCloud"
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
bot.events.on("received").on("command").on("soundcloud", "sc"
|
||||
).hook(self.soundcloud, min_args=1, help="Search SoundCloud")
|
||||
).hook(self.soundcloud, help="Search SoundCloud")
|
||||
|
||||
def soundcloud(self, event):
|
||||
page = Utils.get_url(URL_SOUNDCLOUD, get_params={
|
||||
"client_id": self.bot.config["soundcloud-api-key"],
|
||||
"limit": 1, "q": event["args"]}, json=True)
|
||||
query = None
|
||||
url = None
|
||||
|
||||
if event["args"]:
|
||||
match = re.search(REGEX_SOUNDCLOUD, event["args"])
|
||||
if match:
|
||||
url = match.string
|
||||
else:
|
||||
query = event["args"]
|
||||
else:
|
||||
last_soundcloud = event["log"].find(REGEX_SOUNDCLOUD)
|
||||
if last_soundcloud:
|
||||
url = re.match(REGEX_SOUNDCLOUD,
|
||||
last_soundcloud.message).string
|
||||
|
||||
if not query and not url:
|
||||
event["stderr"].write("no search phrase provided")
|
||||
return
|
||||
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
|
||||
|
||||
page = Utils.get_url(
|
||||
URL_SOUNDCLOUD_TRACK if has_query else URL_SOUNDCLOUD_RESOLVE,
|
||||
get_params=get_params, json=True)
|
||||
|
||||
if page:
|
||||
if page:
|
||||
page = page[0]
|
||||
page = page[0] if has_query else page
|
||||
title = page["title"]
|
||||
user = page["user"]["username"]
|
||||
duration = time.strftime("%H:%M:%S", time.gmtime(page[
|
||||
|
@ -28,7 +57,5 @@ class Module(object):
|
|||
link = page["permalink_url"]
|
||||
event["stdout"].write("%s [%s] (posted by %s) %s" % (title,
|
||||
duration, user, link))
|
||||
else:
|
||||
event["stderr"].write("No results found")
|
||||
else:
|
||||
event["stderr"].write("Failed to load results")
|
||||
|
|
Loading…
Reference in a new issue