]> git.0d.be Git - botaradio.git/commitdiff
feat: 'yplay' play the first result. add 'last' command. #87
authorTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 01:26:11 +0000 (09:26 +0800)
committerTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 01:27:35 +0000 (09:27 +0800)
command.py
configuration.default.ini

index 70b0154ae37043323d0a3bff65fdf173bdee4313..2fd317b8612db5ef1e4a66a770aa5d7582772f49 100644 (file)
@@ -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
 
index 8e4d1d46448576a8189281d1660a216d9b352786..5db47fd7f0d516c63f8ab305df1dfe10c4b0eb3c 100644 (file)
@@ -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 = <h3>Commands</h3>
        <li> <b>!<u>pa</u>use </b> - pause </li>
        <li> <b>!<u>st</u>op </b> - stop playing </li>
        <li> <b>!<u>sk</u>ip </b> - jump to the next song </li>
+       <li> <b>!<u>la</u>st </b> - jump to the last song </li>
        <li> <b>!<u>v</u>olume </b> {volume} - get or change the volume (from 0 to 100) </li>
        <li> <b>!<u>m</u>ode </b> [{mode}] - get or set the playback mode, {mode} should be one of <i>one-shot</i> (remove
        item once played), <i>repeat</i> (looping through the playlist), <i>random</i> (randomize the playlist)</li>
@@ -237,11 +239,12 @@ help = <h3>Commands</h3>
        <li> <b>!<u>rep</u>eat </b> [{num}] - repeat current song {num} (1 by default) times.</li>
        <li> <b>!<u>ran</u>dom </b> - randomize the playlist.</li>
        <li> <b>!<u>rad</u>io </b> {url} - append a radio {url} to the playlist </li>
-       <li> <b>!<u>l</u>istfile </b> [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given) </li>
+       <li> <b>!<u>li</u>stfile </b> [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given) </li>
        <li> <b>!<u>rbq</u>uery </b> {keyword} - query http://www.radio-browser.info for a radio station </li>
        <li> <b>!<u>rbp</u>lay </b> {id} - play a radio station with {id} (eg. !rbplay 96746) </li>
-       <li> <b>!<u>ytq</u>uery </b> {keyword} - query youtube. Use <i>!ytquery -n</i> to turn the page. </li>
-       <li> <b>!<u>ytp</u>lay </b> {index} - play an item from the list returned by <i>!ytquery</i>. </li>
+       <li> <b>!<u>ys</u>earch </b> {keyword} - query youtube. Use <i>!ytquery -n</i> to turn the page. </li>
+       <li> <b>!<u>yp</u>lay </b> {index/keywords} - play an item from the list returned by <i>!ytquery</i>, or add the
+       first search result of {keywords} into the playlist.</li>
        </ul>
        <b>Other</b>
        <ul>