From 61497b1ecb9f7a1dc6e3474d6d98b4ff6dbeea26 Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Tue, 3 Mar 2020 09:26:11 +0800 Subject: [PATCH] feat: 'yplay' play the first result. add 'last' command. #87 --- command.py | 34 ++++++++++++++++++++++++++++------ configuration.default.ini | 13 ++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/command.py b/command.py index 70b0154..2fd317b 100644 --- a/command.py +++ b/command.py @@ -32,7 +32,7 @@ def register_all_commands(bot): bot.register_command(constants.commands('play_radio'), cmd_play_radio) bot.register_command(constants.commands('rb_query'), cmd_rb_query) bot.register_command(constants.commands('rb_play'), cmd_rb_play) - bot.register_command(constants.commands('yt_query'), cmd_yt_query) + bot.register_command(constants.commands('yt_search'), cmd_yt_search) bot.register_command(constants.commands('yt_play'), cmd_yt_play) bot.register_command(constants.commands('help'), cmd_help) bot.register_command(constants.commands('stop'), cmd_stop) @@ -46,6 +46,7 @@ def register_all_commands(bot): bot.register_command(constants.commands('ducking_volume'), cmd_ducking_volume) bot.register_command(constants.commands('current_music'), cmd_current_music) bot.register_command(constants.commands('skip'), cmd_skip) + bot.register_command(constants.commands('last'), cmd_last) bot.register_command(constants.commands('remove'), cmd_remove) bot.register_command(constants.commands('list_file'), cmd_list_file) bot.register_command(constants.commands('queue'), cmd_queue) @@ -451,7 +452,7 @@ def cmd_rb_play(bot, user, text, command, parameter): yt_last_result = [] yt_last_page = 0 # TODO: if we keep adding global variables, we need to consider sealing all commands up into classes. -def cmd_yt_query(bot, user, text, command, parameter): +def cmd_yt_search(bot, user, text, command, parameter): global log, yt_last_result, yt_last_page item_per_page = 5 @@ -489,11 +490,21 @@ def _yt_format_result(results, start, count): def cmd_yt_play(bot, user, text, command, parameter): - global log, yt_last_result + global log, yt_last_result, yt_last_page - if parameter and parameter.isdigit() and 0 <= int(parameter) - 1 < len(yt_last_result): - url = "https://www.youtube.com/watch?v=" + yt_last_result[int(parameter) - 1][0] - cmd_play_url(bot, user, text, command, url) + if parameter: + if parameter.isdigit() and 0 <= int(parameter) - 1 < len(yt_last_result): + url = "https://www.youtube.com/watch?v=" + yt_last_result[int(parameter) - 1][0] + cmd_play_url(bot, user, text, command, url) + else: + results = util.youtube_search(parameter) + if results: + yt_last_result = results + yt_last_page = 0 + url = "https://www.youtube.com/watch?v=" + yt_last_result[0][0] + cmd_play_url(bot, user, text, command, url) + else: + bot.send_msg(constants.strings('yt_query_error')) else: bot.send_msg(constants.strings('bad_parameter', command=command), text) @@ -639,6 +650,17 @@ def cmd_skip(bot, user, text, command, parameter): bot.send_msg(constants.strings('queue_empty'), text) +def cmd_last(bot, user, text, command, parameter): + global log + + if len(var.playlist) > 0: + bot.interrupt_playing() + bot.launch_music(len(var.playlist) - 1) + bot.async_download_next() + else: + bot.send_msg(constants.strings('queue_empty'), text) + + def cmd_remove(bot, user, text, command, parameter): global log diff --git a/configuration.default.ini b/configuration.default.ini index 8e4d1d4..5db47fd 100644 --- a/configuration.default.ini +++ b/configuration.default.ini @@ -126,8 +126,8 @@ play_playlist = playlist rb_query = rbquery rb_play = rbplay -yt_query = ytquery -yt_play = ytplay +yt_search = ysearch +yt_play = yplay help = help pause = pause @@ -136,6 +136,7 @@ stop = stop remove = rm clear = clear skip = skip +last = last current_music = np, now volume = volume kill = kill @@ -217,6 +218,7 @@ help =

Commands

  • !pause - pause
  • !stop - stop playing
  • !skip - jump to the next song
  • +
  • !last - jump to the last song
  • !volume {volume} - get or change the volume (from 0 to 100)
  • !mode [{mode}] - get or set the playback mode, {mode} should be one of one-shot (remove item once played), repeat (looping through the playlist), random (randomize the playlist)
  • @@ -237,11 +239,12 @@ help =

    Commands

  • !repeat [{num}] - repeat current song {num} (1 by default) times.
  • !random - randomize the playlist.
  • !radio {url} - append a radio {url} to the playlist
  • -
  • !listfile [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given)
  • +
  • !listfile [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given)
  • !rbquery {keyword} - query http://www.radio-browser.info for a radio station
  • !rbplay {id} - play a radio station with {id} (eg. !rbplay 96746)
  • -
  • !ytquery {keyword} - query youtube. Use !ytquery -n to turn the page.
  • -
  • !ytplay {index} - play an item from the list returned by !ytquery.
  • +
  • !ysearch {keyword} - query youtube. Use !ytquery -n to turn the page.
  • +
  • !yplay {index/keywords} - play an item from the list returned by !ytquery, or add the + first search result of {keywords} into the playlist.
  • Other