Added !list queue command. Added !audioset soundlcoud command to go with

this
This commit is contained in:
James Seden Smith 2016-02-15 16:07:54 +00:00
parent e54b80b3c9
commit af373901df
2 changed files with 82 additions and 11 deletions

View File

@ -245,7 +245,11 @@ class Audio:
if await self.check_voice(ctx.message.author, ctx.message):
if not self.playlist:
self.queue.append(link)
await self.bot.say("Link added to queue.")
msg = ctx.message
if result["title"] != []:
await self.bot.say("{} has been put into the queue by {}.".format(result["title"], msg.author))
else:
await self.bot.say("The song has been put into the queue by {}, however it may error.".format(msg.author))
else:
await self.bot.say("I'm already playing a playlist.")
@ -318,6 +322,29 @@ class Audio:
else:
await self.bot.say("There are no local playlists.")
@_list.command(name="queue", pass_context=True)
async def list_queue(self, ctx):
message = ctx.message
cmdmsg = message
song_names = []
song_names.append(self.downloader["TITLE"])
if len(self.queue) > 0:
for song_url in self.queue:
try:
result = await self.get_song_metadata(song_url)
if result["title"] != []:
song_names.append(result["title"])
else:
song_names.append("Could not get song title")
except:
await self.bot.say("Error.")
song_list = '\n'.join('{}: {}'.format(*k) for k in enumerate(song_names))
elif self.music_player.is_playing():
song_list = "0: {}".format(song_names)
else:
song_list = "None"
await self.bot.say("Videos in queue: \n" + song_list)
@commands.group(pass_context=True)
@checks.mod_or_permissions()
async def audioset(self, ctx):
@ -374,6 +401,17 @@ class Audio:
else:
await self.bot.say("Maximum audio cache size has been set to " + str(size) + "MB.")
@audioset.command()
@checks.is_owner()
async def soundcloud(self, ID : str):
"""Sets the SoundCloud Client ID
"""
self.settings["SOUNDCLOUD_CLIENT_ID"] = ID
fileIO("data/audio/settings.json", "save", self.settings)
if not ID:
await self.bot.say("SoundCloud API intergration has been disabled")
else:
await self.bot.say("SoundCloud Client ID has been set")
@commands.group(pass_context=True)
@checks.is_owner()
@ -608,6 +646,39 @@ class Audio:
except:
return False
async def get_json(self, url):
"""
Returns the JSON from an URL.
Expects the url to be valid and return a JSON object.
"""
async with aiohttp.get(url) as r:
result = await r.json()
return result
async def get_song_metadata(self, song_url):
"""
Returns JSON object containing metadata about the song.
Expects song_url to be valid url and in acepted_list
"""
youtube_regex = (
r'(https?://)?(www\.)?'
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
soundcloud_regex = "^(https:\\/\\/soundcloud\\.com\\/.*)"
is_youtube_link = re.match(youtube_regex, song_url)
is_soundcloud_link = re.match(soundcloud_regex, song_url)
if is_youtube_link:
url = "http://www.youtube.com/oembed?url={0}&format=json".format(song_url)
result = await self.get_json(url)
elif is_soundcloud_link and (self.settings["SOUNDCLOUD_CLIENT_ID"] is not None):
url = "http://api.soundcloud.com/resolve.json?url={0}&client_id={1}".format(song_url, self.settings["SOUNDCLOUD_CLIENT_ID"])
result = await self.get_json(url)
else:
result = {"title": "Couldn't get title"}
return result
class EmptyPlayer(): #dummy player
def __init__(self):
pass
@ -633,7 +704,7 @@ def check_folders():
def check_files():
default = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True, "MAX_CACHE" : 0}
default = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True, "MAX_CACHE" : 0, "SOUNDCLOUD_CLIENT_ID": None}
settings_path = "data/audio/settings.json"
if not os.path.isfile(settings_path):