feat: add ytquery and ytplay
This commit is contained in:
parent
aa2155b6ca
commit
e17d5446bf
51
command.py
51
command.py
@ -32,6 +32,8 @@ def register_all_commands(bot):
|
|||||||
bot.register_command(constants.commands('play_radio'), cmd_play_radio)
|
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_query'), cmd_rb_query)
|
||||||
bot.register_command(constants.commands('rb_play'), cmd_rb_play)
|
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_play'), cmd_yt_play)
|
||||||
bot.register_command(constants.commands('help'), cmd_help)
|
bot.register_command(constants.commands('help'), cmd_help)
|
||||||
bot.register_command(constants.commands('stop'), cmd_stop)
|
bot.register_command(constants.commands('stop'), cmd_stop)
|
||||||
bot.register_command(constants.commands('clear'), cmd_clear)
|
bot.register_command(constants.commands('clear'), cmd_clear)
|
||||||
@ -439,6 +441,55 @@ def cmd_rb_play(bot, user, text, command, parameter):
|
|||||||
msg += "No playable url found for this station, please try another station."
|
msg += "No playable url found for this station, please try another station."
|
||||||
bot.send_msg(msg, text)
|
bot.send_msg(msg, text)
|
||||||
|
|
||||||
|
yt_last_result = None
|
||||||
|
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):
|
||||||
|
global log, yt_last_result, yt_last_page
|
||||||
|
item_per_page = 5
|
||||||
|
|
||||||
|
if parameter:
|
||||||
|
# if next page
|
||||||
|
if parameter.startswith("-n"):
|
||||||
|
yt_last_page += 1
|
||||||
|
if len(yt_last_result) > yt_last_page * item_per_page:
|
||||||
|
msg = _yt_format_result(yt_last_result, yt_last_page * item_per_page, item_per_page)
|
||||||
|
bot.send_msg(constants.strings('yt_result', result_table=msg), text)
|
||||||
|
else:
|
||||||
|
bot.send_msg(constants.strings('yt_no_more'))
|
||||||
|
|
||||||
|
# if query
|
||||||
|
else:
|
||||||
|
results = util.youtube_search(parameter)
|
||||||
|
if results:
|
||||||
|
yt_last_result = results
|
||||||
|
yt_last_page = 0
|
||||||
|
msg = _yt_format_result(results, 0, item_per_page)
|
||||||
|
bot.send_msg(constants.strings('yt_result', result_table=msg), text)
|
||||||
|
else:
|
||||||
|
bot.send_msg(constants.strings('yt_query_error'))
|
||||||
|
else:
|
||||||
|
bot.send_msg(constants.strings('bad_parameter', command=command), text)
|
||||||
|
|
||||||
|
def _yt_format_result(results, start, count):
|
||||||
|
msg = '<table><tr><th width="10%">Index</th><th>Title</th><th width="20%">Uploader</th></tr>'
|
||||||
|
for index, item in enumerate(results[start:start+count]):
|
||||||
|
msg += '<tr><td>{index:d}</td><td>{title}</td><td>{uploader}</td></tr>'.format(
|
||||||
|
index=index + 1 + start, title=item[1], uploader=item[2])
|
||||||
|
msg += '</table>'
|
||||||
|
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_yt_play(bot, user, text, command, parameter):
|
||||||
|
global log, yt_last_result
|
||||||
|
|
||||||
|
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)
|
||||||
|
else:
|
||||||
|
bot.send_msg(constants.strings('bad_parameter', command=command), text)
|
||||||
|
|
||||||
|
|
||||||
def cmd_help(bot, user, text, command, parameter):
|
def cmd_help(bot, user, text, command, parameter):
|
||||||
global log
|
global log
|
||||||
|
@ -126,6 +126,9 @@ play_playlist = playlist
|
|||||||
rb_query = rbquery
|
rb_query = rbquery
|
||||||
rb_play = rbplay
|
rb_play = rbplay
|
||||||
|
|
||||||
|
yt_query = ytquery
|
||||||
|
yt_play = ytplay
|
||||||
|
|
||||||
help = help
|
help = help
|
||||||
pause = pause
|
pause = pause
|
||||||
play = p, play
|
play = p, play
|
||||||
@ -199,6 +202,10 @@ unknown_mode = Unknown playback mode '{mode}'. It should be one of <i>one-shot</
|
|||||||
current_mode = Current playback mode is <i>{mode}</i>.
|
current_mode = Current playback mode is <i>{mode}</i>.
|
||||||
change_mode = Playback mode set to <i>{mode}</i> by {user}.
|
change_mode = Playback mode set to <i>{mode}</i> by {user}.
|
||||||
repeat = Repeat {song} for {n} times.
|
repeat = Repeat {song} for {n} times.
|
||||||
|
yt_result = Youtube query result: {result_table} Use <i>!ytplay</i> {{index}} to play the item you want. <br />
|
||||||
|
<i>!ytquery -n</i> for the next page.
|
||||||
|
yt_no_more = No more results!
|
||||||
|
yt_query_error = Unable to query youtube!
|
||||||
|
|
||||||
help = <h3>Commands</h3>
|
help = <h3>Commands</h3>
|
||||||
<b>Control</b>
|
<b>Control</b>
|
||||||
|
@ -238,7 +238,7 @@ class MumbleBot:
|
|||||||
command = message[0]
|
command = message[0]
|
||||||
parameter = ''
|
parameter = ''
|
||||||
if len(message) > 1:
|
if len(message) > 1:
|
||||||
parameter = message[1]
|
parameter = message[1].rstrip()
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
19
util.py
19
util.py
@ -18,6 +18,8 @@ from importlib import reload
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from sys import platform
|
from sys import platform
|
||||||
|
import traceback
|
||||||
|
import urllib.parse, urllib.request, urllib.error
|
||||||
import base64
|
import base64
|
||||||
import media
|
import media
|
||||||
import media.radio
|
import media.radio
|
||||||
@ -467,3 +469,20 @@ def get_url_from_input(string):
|
|||||||
return res.group(1)
|
return res.group(1)
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def youtube_search(query):
|
||||||
|
query_url = "https://www.youtube.com/results?search_query=" + urllib.parse.quote(query, safe="")
|
||||||
|
|
||||||
|
try:
|
||||||
|
request = urllib.request.Request(query_url)
|
||||||
|
response = urllib.request.urlopen(request).read().decode("utf-8")
|
||||||
|
results = re.findall("watch\?v=(.*?)\".*?title=\"(.*?)\".*?"
|
||||||
|
"(?:user|channel).*?>(.*?)<", response) # (id, title, uploader)
|
||||||
|
|
||||||
|
print(results)
|
||||||
|
|
||||||
|
if len(results) > 0:
|
||||||
|
return results
|
||||||
|
except:
|
||||||
|
print(traceback.format_exc().split("During")[0])
|
||||||
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user