feat: beautified current song string, fix ytplay index problem
This commit is contained in:
parent
13753afa83
commit
dbe317bf31
@ -330,6 +330,10 @@ def cmd_play_radio(bot, user, text, command, parameter):
|
|||||||
music = {'type': 'radio',
|
music = {'type': 'radio',
|
||||||
'url': url,
|
'url': url,
|
||||||
'user': user}
|
'user': user}
|
||||||
|
|
||||||
|
log.info("bot: fetching radio server description")
|
||||||
|
music["name"] = media.radio.get_radio_server_description(url)
|
||||||
|
|
||||||
var.playlist.append(music)
|
var.playlist.append(music)
|
||||||
log.info("cmd: add to playlist: " + util.format_debug_song_string(music))
|
log.info("cmd: add to playlist: " + util.format_debug_song_string(music))
|
||||||
bot.async_download_next()
|
bot.async_download_next()
|
||||||
@ -429,7 +433,7 @@ def cmd_rb_play(bot, user, text, command, parameter):
|
|||||||
if url != "-1":
|
if url != "-1":
|
||||||
log.info('cmd: Found url: ' + url)
|
log.info('cmd: Found url: ' + url)
|
||||||
music = {'type': 'radio',
|
music = {'type': 'radio',
|
||||||
'title': stationname,
|
'name': stationname,
|
||||||
'artist': homepage,
|
'artist': homepage,
|
||||||
'url': url,
|
'url': url,
|
||||||
'user': user}
|
'user': user}
|
||||||
@ -484,7 +488,7 @@ def _yt_format_result(results, start, count):
|
|||||||
def cmd_yt_play(bot, user, text, command, parameter):
|
def cmd_yt_play(bot, user, text, command, parameter):
|
||||||
global log, yt_last_result
|
global log, yt_last_result
|
||||||
|
|
||||||
if parameter and parameter.isdigit() and 0 < int(parameter) - 1 < len(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]
|
url = "https://www.youtube.com/watch?v=" + yt_last_result[int(parameter) - 1][0]
|
||||||
cmd_play_url(bot, user, text, command, url)
|
cmd_play_url(bot, user, text, command, url)
|
||||||
else:
|
else:
|
||||||
|
@ -181,7 +181,10 @@ multiple_matches = Track not found! Possible candidates:
|
|||||||
queue_contents = Items on the playlist:
|
queue_contents = Items on the playlist:
|
||||||
queue_empty = Playlist is empty!
|
queue_empty = Playlist is empty!
|
||||||
invalid_index = Invalid index <i>{index}</i>. Use '!queue' to see your playlist.
|
invalid_index = Invalid index <i>{index}</i>. Use '!queue' to see your playlist.
|
||||||
now_playing = Now playing {item}<br />{thumb}
|
now_playing_radio = Now Playing Radio: <br /> <a href="{url}">{title}</a> <i>from</i> {name} <i>added by</i> {user}
|
||||||
|
now_playing_file = Now Playing File:<br /> {artist} - {title} <i>added by</i> {user}
|
||||||
|
now_playing_from_playlist = Now Playing URL:<br /> {title} <i>from playlist</i> <a href="{url}">{playlist}</a> <i>added by</i> {user}
|
||||||
|
now_playing_url = Now Playing URL: <br /> <a href="{url}">{title}</a> <i>added by</i> {user}
|
||||||
not_in_my_channel = You're not in my channel, command refused!
|
not_in_my_channel = You're not in my channel, command refused!
|
||||||
pm_not_allowed = Private message aren't allowed.
|
pm_not_allowed = Private message aren't allowed.
|
||||||
too_long = This music is too long, skip!
|
too_long = This music is too long, skip!
|
||||||
|
@ -12,6 +12,7 @@ import random
|
|||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
import errno
|
import errno
|
||||||
import media
|
import media
|
||||||
|
import media.radio
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
import constants
|
import constants
|
||||||
@ -226,9 +227,12 @@ def post():
|
|||||||
var.botamusique.async_download_next()
|
var.botamusique.async_download_next()
|
||||||
|
|
||||||
elif 'add_radio' in request.form:
|
elif 'add_radio' in request.form:
|
||||||
|
url = request.form['add_radio']
|
||||||
music = var.playlist.append({'type': 'radio',
|
music = var.playlist.append({'type': 'radio',
|
||||||
'path': request.form['add_radio'],
|
'url': url,
|
||||||
'user': "Remote Control"})
|
'user': "Remote Control"})
|
||||||
|
log.info("web: fetching radio server description")
|
||||||
|
music["name"] = media.radio.get_radio_server_description(url)
|
||||||
log.info("web: add to playlist: " + util.format_debug_song_string(music))
|
log.info("web: add to playlist: " + util.format_debug_song_string(music))
|
||||||
|
|
||||||
elif 'delete_music' in request.form:
|
elif 'delete_music' in request.form:
|
||||||
|
@ -10,7 +10,6 @@ def get_url_info(music):
|
|||||||
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
||||||
for i in range(2):
|
for i in range(2):
|
||||||
try:
|
try:
|
||||||
print(var.playlist)
|
|
||||||
info = ydl.extract_info(music['url'], download=False)
|
info = ydl.extract_info(music['url'], download=False)
|
||||||
music['duration'] = info['duration'] / 60
|
music['duration'] = info['duration'] / 60
|
||||||
music['title'] = info['title']
|
music['title'] = info['title']
|
||||||
|
@ -285,7 +285,7 @@ class MumbleBot:
|
|||||||
if len(matches) == 1:
|
if len(matches) == 1:
|
||||||
self.log.info("bot: {:s} matches {:s}".format(command, matches[0]))
|
self.log.info("bot: {:s} matches {:s}".format(command, matches[0]))
|
||||||
command_exc = matches[0]
|
command_exc = matches[0]
|
||||||
self.cmd_handle[matches[0]](self, user, text, command, parameter)
|
self.cmd_handle[command_exc](self, user, text, command_exc, parameter)
|
||||||
elif len(matches) > 1:
|
elif len(matches) > 1:
|
||||||
self.mumble.users[text.actor].send_text_message(
|
self.mumble.users[text.actor].send_text_message(
|
||||||
constants.strings('which_command', commands="<br>".join(matches)))
|
constants.strings('which_command', commands="<br>".join(matches)))
|
||||||
@ -354,10 +354,10 @@ class MumbleBot:
|
|||||||
|
|
||||||
elif music["type"] == "radio":
|
elif music["type"] == "radio":
|
||||||
uri = music["url"]
|
uri = music["url"]
|
||||||
if 'title' not in music:
|
if 'name' not in music:
|
||||||
self.log.info("bot: fetching radio server description")
|
self.log.info("bot: fetching radio server description")
|
||||||
title = media.radio.get_radio_server_description(uri)
|
title = media.radio.get_radio_server_description(uri)
|
||||||
music["title"] = title
|
music["name"] = title
|
||||||
|
|
||||||
if var.config.getboolean('bot', 'announce_current_music'):
|
if var.config.getboolean('bot', 'announce_current_music'):
|
||||||
self.send_msg(util.format_current_playing())
|
self.send_msg(util.format_current_playing())
|
||||||
@ -469,7 +469,8 @@ class MumbleBot:
|
|||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
error_traceback = traceback.format_exc().split("During")[0]
|
error_traceback = traceback.format_exc().split("During")[0]
|
||||||
self.log.error("bot: download failed with error:\n %s" % error_traceback)
|
error = error_traceback.rstrip().split("\n")[-1]
|
||||||
|
self.log.error("bot: download failed with error:\n %s" % error)
|
||||||
|
|
||||||
if not download_succeed:
|
if not download_succeed:
|
||||||
for f in [mp3, path.replace(".%(ext)s", ".jpg"), path.replace(".%(ext)s", ".m4a")]:
|
for f in [mp3, path.replace(".%(ext)s", ".jpg"), path.replace(".%(ext)s", ".m4a")]:
|
||||||
|
50
util.py
50
util.py
@ -8,7 +8,7 @@ import sys
|
|||||||
import variables as var
|
import variables as var
|
||||||
import constants
|
import constants
|
||||||
import zipfile
|
import zipfile
|
||||||
import urllib.request
|
import requests
|
||||||
import mutagen
|
import mutagen
|
||||||
import re
|
import re
|
||||||
import subprocess as sp
|
import subprocess as sp
|
||||||
@ -149,29 +149,30 @@ def format_song_string(music):
|
|||||||
artist = music["artist"] if "artist" in music else "Unknown artist"
|
artist = music["artist"] if "artist" in music else "Unknown artist"
|
||||||
|
|
||||||
if source == "radio":
|
if source == "radio":
|
||||||
display = "[radio] {title} from {url} by {user}".format(
|
display = constants.strings("now_playing_radio",
|
||||||
title=media.radio.get_radio_title(music["url"]),
|
|
||||||
url=music["url"],
|
url=music["url"],
|
||||||
|
title=media.radio.get_radio_title(music["url"]),
|
||||||
|
name=music["name"],
|
||||||
user=music["user"]
|
user=music["user"]
|
||||||
)
|
)
|
||||||
elif source == "url" and 'from_playlist' in music:
|
elif source == "url" and 'from_playlist' in music:
|
||||||
display = "[url] {title} (from playlist <a href=\"{url}\">{playlist}</a>) by {user}".format(
|
display = constants.strings("now_playing_from_playlist",
|
||||||
title=title,
|
title=title,
|
||||||
url=music["playlist_url"],
|
url=music["playlist_url"],
|
||||||
playlist=music["playlist_title"],
|
playlist=music["playlist_title"],
|
||||||
user=music["user"]
|
user=music["user"]
|
||||||
)
|
)
|
||||||
elif source == "url":
|
elif source == "url":
|
||||||
display = "[url] <a href=\"{url}\">{title}</a> by {user}".format(
|
display = constants.strings("now_playing_url",
|
||||||
title=title,
|
title=title,
|
||||||
url=music["url"],
|
url=music["url"],
|
||||||
user=music["user"]
|
user=music["user"]
|
||||||
)
|
)
|
||||||
elif source == "file":
|
elif source == "file":
|
||||||
display = "[file] {artist} - {title} by {user}".format(
|
display = constants.strings("now_playing_file",
|
||||||
title=title,
|
title=title,
|
||||||
artist=artist,
|
artist=artist,
|
||||||
user=music["user"]
|
user=music["user"]
|
||||||
)
|
)
|
||||||
|
|
||||||
return display
|
return display
|
||||||
@ -184,7 +185,8 @@ def format_debug_song_string(music):
|
|||||||
artist = music["artist"] if "artist" in music else "??"
|
artist = music["artist"] if "artist" in music else "??"
|
||||||
|
|
||||||
if source == "radio":
|
if source == "radio":
|
||||||
display = "[radio] {url} by {user}".format(
|
display = "[radio] {name} ({url}) by {user}".format(
|
||||||
|
name=music["name"],
|
||||||
url=music["url"],
|
url=music["url"],
|
||||||
user=music["user"]
|
user=music["user"]
|
||||||
)
|
)
|
||||||
@ -216,12 +218,10 @@ def format_current_playing():
|
|||||||
music = var.playlist.current_item()
|
music = var.playlist.current_item()
|
||||||
display = format_song_string(music)
|
display = format_song_string(music)
|
||||||
|
|
||||||
thumbnail_html = ''
|
|
||||||
if 'thumbnail' in music:
|
if 'thumbnail' in music:
|
||||||
thumbnail_html = '<img width="80" src="data:image/jpge;base64,' + \
|
thumbnail_html = '<img width="80" src="data:image/jpge;base64,' + \
|
||||||
music['thumbnail'] + '"/>'
|
music['thumbnail'] + '"/>'
|
||||||
|
return display + "<br />" + thumbnail_html
|
||||||
display = (constants.strings('now_playing', item=display, thumb=thumbnail_html))
|
|
||||||
|
|
||||||
return display
|
return display
|
||||||
|
|
||||||
@ -473,17 +473,15 @@ def get_url_from_input(string):
|
|||||||
def youtube_search(query):
|
def youtube_search(query):
|
||||||
global log
|
global log
|
||||||
|
|
||||||
query_url = "https://www.youtube.com/results?search_query=" + urllib.parse.quote(query, safe="")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request = urllib.request.Request(query_url)
|
r = requests.get("https://www.youtube.com/results", params={'search_query': query}, timeout=5)
|
||||||
response = urllib.request.urlopen(request).read().decode("utf-8")
|
|
||||||
results = re.findall("watch\?v=(.*?)\".*?title=\"(.*?)\".*?"
|
results = re.findall("watch\?v=(.*?)\".*?title=\"(.*?)\".*?"
|
||||||
"(?:user|channel).*?>(.*?)<", response) # (id, title, uploader)
|
"(?:user|channel).*?>(.*?)<", r.text) # (id, title, uploader)
|
||||||
|
|
||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
return results
|
return results
|
||||||
except:
|
|
||||||
|
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
|
||||||
error_traceback = traceback.format_exc().split("During")[0]
|
error_traceback = traceback.format_exc().split("During")[0]
|
||||||
log.error("util: youtube query failed with error:\n %s" % error_traceback)
|
log.error("util: youtube query failed with error:\n %s" % error_traceback)
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user