fix: some small issue
This commit is contained in:
24
command.py
24
command.py
@ -12,7 +12,7 @@ from librb import radiobrowser
|
|||||||
from database import Database
|
from database import Database
|
||||||
from media.playlist import PlaylistItemWrapper
|
from media.playlist import PlaylistItemWrapper
|
||||||
from media.file import FileItem
|
from media.file import FileItem
|
||||||
from media.url_from_playlist import URLFromPlaylistItem, get_playlist_info
|
from media.url_from_playlist import PlaylistURLItem, get_playlist_info
|
||||||
from media.url import URLItem
|
from media.url import URLItem
|
||||||
from media.radio import RadioItem
|
from media.radio import RadioItem
|
||||||
|
|
||||||
@ -139,8 +139,10 @@ def cmd_play(bot, user, text, command, parameter):
|
|||||||
|
|
||||||
if var.playlist.length() > 0:
|
if var.playlist.length() > 0:
|
||||||
if parameter:
|
if parameter:
|
||||||
if parameter.isdigit() and 0 <= int(parameter) <= len(var.playlist):
|
if parameter.isdigit() and 1 <= int(parameter) <= len(var.playlist):
|
||||||
var.playlist.point_to(int(parameter) - 1)
|
var.playlist.point_to(int(parameter) - 1 - 1) # First "-1" transfer 12345 to 01234, second "-1"
|
||||||
|
# point to the previous item. the loop will next to
|
||||||
|
# the one you want
|
||||||
bot.interrupt_playing()
|
bot.interrupt_playing()
|
||||||
else:
|
else:
|
||||||
bot.send_msg(constants.strings('invalid_index', index=parameter), text)
|
bot.send_msg(constants.strings('invalid_index', index=parameter), text)
|
||||||
@ -299,7 +301,7 @@ def cmd_play_playlist(bot, user, text, command, parameter):
|
|||||||
log.debug("cmd: fetching media info from playlist url %s" % url)
|
log.debug("cmd: fetching media info from playlist url %s" % url)
|
||||||
items = get_playlist_info(bot, url=url, start_index=offset, user=user)
|
items = get_playlist_info(bot, url=url, start_index=offset, user=user)
|
||||||
if len(items) > 0:
|
if len(items) > 0:
|
||||||
var.playlist.extend(items)
|
var.playlist.extend(list(map(lambda item: PlaylistItemWrapper(item, user), items)))
|
||||||
for music in items:
|
for music in items:
|
||||||
log.info("cmd: add to playlist: " + music.format_debug_string())
|
log.info("cmd: add to playlist: " + music.format_debug_string())
|
||||||
else:
|
else:
|
||||||
@ -636,8 +638,7 @@ def cmd_last(bot, user, text, command, parameter):
|
|||||||
|
|
||||||
if len(var.playlist) > 0:
|
if len(var.playlist) > 0:
|
||||||
bot.interrupt_playing()
|
bot.interrupt_playing()
|
||||||
bot.launch_music(len(var.playlist) - 1)
|
var.playlist.point_to(len(var.playlist) - 1)
|
||||||
bot.async_download_next()
|
|
||||||
else:
|
else:
|
||||||
bot.send_msg(constants.strings('queue_empty'), text)
|
bot.send_msg(constants.strings('queue_empty'), text)
|
||||||
|
|
||||||
@ -669,7 +670,7 @@ def cmd_remove(bot, user, text, command, parameter):
|
|||||||
removed = var.playlist.remove(index)
|
removed = var.playlist.remove(index)
|
||||||
|
|
||||||
bot.send_msg(constants.strings('removing_item',
|
bot.send_msg(constants.strings('removing_item',
|
||||||
item=removed.format_song_string()), text)
|
item=removed.format_short_string()), text)
|
||||||
|
|
||||||
log.info("cmd: delete from playlist: " + removed.format_debug_string())
|
log.info("cmd: delete from playlist: " + removed.format_debug_string())
|
||||||
else:
|
else:
|
||||||
@ -714,12 +715,13 @@ def cmd_queue(bot, user, text, command, parameter):
|
|||||||
msgs = [ constants.strings('queue_contents')]
|
msgs = [ constants.strings('queue_contents')]
|
||||||
for i, value in enumerate(var.playlist):
|
for i, value in enumerate(var.playlist):
|
||||||
newline = ''
|
newline = ''
|
||||||
|
music = value.item
|
||||||
if i == var.playlist.current_index:
|
if i == var.playlist.current_index:
|
||||||
newline = '<b>{} ▶ ({}) {} ◀</b>'.format(i + 1, value['type'],
|
newline = '<b>{} ▶ ({}) {} ◀</b>'.format(i + 1, music.display_type(),
|
||||||
value['title'] if 'title' in value else value['url'])
|
music.format_short_string())
|
||||||
else:
|
else:
|
||||||
newline = '<b>{}</b> ({}) {}'.format(i + 1, value['type'],
|
newline = '<b>{}</b> ({}) {}'.format(i + 1, music.display_type(),
|
||||||
value['title'] if 'title' in value else value['url'])
|
music.format_short_string())
|
||||||
|
|
||||||
msgs.append(newline)
|
msgs.append(newline)
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import errno
|
|||||||
import media
|
import media
|
||||||
from media.playlist import PlaylistItemWrapper
|
from media.playlist import PlaylistItemWrapper
|
||||||
from media.file import FileItem
|
from media.file import FileItem
|
||||||
from media.url_from_playlist import URLFromPlaylistItem, get_playlist_info
|
from media.url_from_playlist import PlaylistURLItem, get_playlist_info
|
||||||
from media.url import URLItem
|
from media.url import URLItem
|
||||||
from media.radio import RadioItem
|
from media.radio import RadioItem
|
||||||
import logging
|
import logging
|
||||||
|
@ -30,7 +30,7 @@ class FileItem(BaseItem):
|
|||||||
super().__init__(bot)
|
super().__init__(bot)
|
||||||
self.path = path
|
self.path = path
|
||||||
self.title = ""
|
self.title = ""
|
||||||
self.artist = "??"
|
self.artist = ""
|
||||||
self.thumbnail = None
|
self.thumbnail = None
|
||||||
if self.path:
|
if self.path:
|
||||||
self.id = hashlib.md5(path.encode()).hexdigest()
|
self.id = hashlib.md5(path.encode()).hexdigest()
|
||||||
@ -132,9 +132,8 @@ class FileItem(BaseItem):
|
|||||||
return dict
|
return dict
|
||||||
|
|
||||||
def format_debug_string(self):
|
def format_debug_string(self):
|
||||||
return "[file] {artist} - {title} ({path})".format(
|
return "[file] {descrip} ({path})".format(
|
||||||
title=self.title,
|
descrip=self.format_short_string(),
|
||||||
artist=self.artist,
|
|
||||||
path=self.path
|
path=self.path
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -154,5 +153,12 @@ class FileItem(BaseItem):
|
|||||||
|
|
||||||
return display
|
return display
|
||||||
|
|
||||||
|
def format_short_string(self):
|
||||||
|
title = self.title if self.title else self.path
|
||||||
|
if self.artist:
|
||||||
|
return self.artist + " - " + title
|
||||||
|
else:
|
||||||
|
return title
|
||||||
|
|
||||||
def display_type(self):
|
def display_type(self):
|
||||||
return constants.strings("file")
|
return constants.strings("file")
|
||||||
|
@ -41,6 +41,7 @@ class BaseItem:
|
|||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.log = logging.getLogger("bot")
|
self.log = logging.getLogger("bot")
|
||||||
self.type = "base"
|
self.type = "base"
|
||||||
|
self.title = ""
|
||||||
|
|
||||||
if from_dict is None:
|
if from_dict is None:
|
||||||
self.id = ""
|
self.id = ""
|
||||||
@ -83,6 +84,9 @@ class BaseItem:
|
|||||||
def format_current_playing(self, user):
|
def format_current_playing(self, user):
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
|
def format_short_string(self):
|
||||||
|
return self.title
|
||||||
|
|
||||||
def format_debug_string(self):
|
def format_debug_string(self):
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import hashlib
|
|
||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import util
|
|
||||||
import variables as var
|
import variables as var
|
||||||
from media.item import BaseItem
|
|
||||||
from media.file import FileItem
|
from media.file import FileItem
|
||||||
from media.url import URLItem
|
from media.url import URLItem
|
||||||
|
from media.url_from_playlist import PlaylistURLItem
|
||||||
|
from media.radio import RadioItem
|
||||||
|
|
||||||
|
|
||||||
class PlaylistItemWrapper:
|
class PlaylistItemWrapper:
|
||||||
@ -27,6 +26,9 @@ class PlaylistItemWrapper:
|
|||||||
def format_song_string(self):
|
def format_song_string(self):
|
||||||
return self.item.format_song_string(self.user)
|
return self.item.format_song_string(self.user)
|
||||||
|
|
||||||
|
def format_short_string(self):
|
||||||
|
return self.item.format_short_string()
|
||||||
|
|
||||||
def format_debug_string(self):
|
def format_debug_string(self):
|
||||||
return self.item.format_debug_string()
|
return self.item.format_debug_string()
|
||||||
|
|
||||||
@ -36,6 +38,10 @@ def dict_to_item(dict):
|
|||||||
return PlaylistItemWrapper(FileItem(var.bot, "", dict), dict['user'])
|
return PlaylistItemWrapper(FileItem(var.bot, "", dict), dict['user'])
|
||||||
elif dict['type'] == 'url':
|
elif dict['type'] == 'url':
|
||||||
return PlaylistItemWrapper(URLItem(var.bot, "", dict), dict['user'])
|
return PlaylistItemWrapper(URLItem(var.bot, "", dict), dict['user'])
|
||||||
|
elif dict['type'] == 'url_from_playlist':
|
||||||
|
return PlaylistItemWrapper(PlaylistURLItem(var.bot, "", "", "", "", dict), dict['user'])
|
||||||
|
elif dict['type'] == 'radio':
|
||||||
|
return PlaylistItemWrapper(RadioItem(var.bot, "", "", dict), dict['user'])
|
||||||
|
|
||||||
|
|
||||||
class PlayList(list):
|
class PlayList(list):
|
||||||
@ -79,7 +85,6 @@ class PlayList(list):
|
|||||||
if index == -1:
|
if index == -1:
|
||||||
index = self.current_index
|
index = self.current_index
|
||||||
|
|
||||||
item = util.attach_music_tag_info(item)
|
|
||||||
super().insert(index, item)
|
super().insert(index, item)
|
||||||
|
|
||||||
if index <= self.current_index:
|
if index <= self.current_index:
|
||||||
@ -95,9 +100,6 @@ class PlayList(list):
|
|||||||
|
|
||||||
def extend(self, items):
|
def extend(self, items):
|
||||||
self.version += 1
|
self.version += 1
|
||||||
items = list(map(
|
|
||||||
lambda item: item,
|
|
||||||
items))
|
|
||||||
super().extend(items)
|
super().extend(items)
|
||||||
self.pending_items.extend(items)
|
self.pending_items.extend(items)
|
||||||
self.start_async_validating()
|
self.start_async_validating()
|
||||||
|
@ -143,8 +143,6 @@ class URLItem(FileItem):
|
|||||||
'preferredquality': '192'},
|
'preferredquality': '192'},
|
||||||
{'key': 'FFmpegMetadata'}]
|
{'key': 'FFmpegMetadata'}]
|
||||||
}
|
}
|
||||||
# TODO
|
|
||||||
self.send_client_message(constants.strings('download_in_progress', item=self.url))
|
|
||||||
|
|
||||||
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
||||||
attempts = var.config.getint('bot', 'download_attempts', fallback=2)
|
attempts = var.config.getint('bot', 'download_attempts', fallback=2)
|
||||||
@ -166,6 +164,7 @@ class URLItem(FileItem):
|
|||||||
self.log.info(
|
self.log.info(
|
||||||
"bot: finished downloading url (%s) %s, saved to %s." % (self.title, self.url, self.path))
|
"bot: finished downloading url (%s) %s, saved to %s." % (self.title, self.url, self.path))
|
||||||
self.downloading = False
|
self.downloading = False
|
||||||
|
self._read_thumbnail_from_file(base_path + ".jpg")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
for f in glob.glob(base_path + "*"):
|
for f in glob.glob(base_path + "*"):
|
||||||
@ -211,5 +210,8 @@ class URLItem(FileItem):
|
|||||||
|
|
||||||
return display
|
return display
|
||||||
|
|
||||||
|
def format_short_string(self):
|
||||||
|
return self.title if self.title else self.url
|
||||||
|
|
||||||
def display_type(self):
|
def display_type(self):
|
||||||
return constants.strings("url")
|
return constants.strings("url")
|
||||||
|
@ -3,7 +3,6 @@ import constants
|
|||||||
import media
|
import media
|
||||||
import variables as var
|
import variables as var
|
||||||
from media.url import URLItem
|
from media.url import URLItem
|
||||||
from media.playlist import PlaylistItemWrapper
|
|
||||||
|
|
||||||
def get_playlist_info(bot, url, start_index=0, user=""):
|
def get_playlist_info(bot, url, start_index=0, user=""):
|
||||||
items = []
|
items = []
|
||||||
@ -33,15 +32,15 @@ def get_playlist_info(bot, url, start_index=0, user=""):
|
|||||||
# Add youtube url if the url in the json isn't a full url
|
# Add youtube url if the url in the json isn't a full url
|
||||||
item_url = info['entries'][j]['url'] if info['entries'][j]['url'][0:4] == 'http' \
|
item_url = info['entries'][j]['url'] if info['entries'][j]['url'][0:4] == 'http' \
|
||||||
else "https://www.youtube.com/watch?v=" + info['entries'][j]['url']
|
else "https://www.youtube.com/watch?v=" + info['entries'][j]['url']
|
||||||
|
print(info['entries'][j])
|
||||||
|
|
||||||
music = PlaylistItemWrapper(
|
music = PlaylistURLItem(
|
||||||
URLFromPlaylistItem(
|
|
||||||
bot,
|
bot,
|
||||||
item_url,
|
item_url,
|
||||||
title,
|
title,
|
||||||
url,
|
url,
|
||||||
playlist_title
|
playlist_title
|
||||||
), user)
|
)
|
||||||
|
|
||||||
items.append(music)
|
items.append(music)
|
||||||
except:
|
except:
|
||||||
@ -49,7 +48,7 @@ def get_playlist_info(bot, url, start_index=0, user=""):
|
|||||||
|
|
||||||
return items
|
return items
|
||||||
|
|
||||||
class URLFromPlaylistItem(URLItem):
|
class PlaylistURLItem(URLItem):
|
||||||
def __init__(self, bot, url, title, playlist_url, playlist_title, from_dict=None):
|
def __init__(self, bot, url, title, playlist_url, playlist_title, from_dict=None):
|
||||||
if from_dict is None:
|
if from_dict is None:
|
||||||
super().__init__(bot, url)
|
super().__init__(bot, url)
|
||||||
|
@ -330,7 +330,7 @@ class MumbleBot:
|
|||||||
# Function start if the next music isn't ready
|
# Function start if the next music isn't ready
|
||||||
# Do nothing in case the next music is already downloaded
|
# Do nothing in case the next music is already downloaded
|
||||||
self.log.debug("bot: Async download next asked ")
|
self.log.debug("bot: Async download next asked ")
|
||||||
while var.playlist.next_item() and var.playlist.next_item().item.type == 'url':
|
while var.playlist.next_item() and var.playlist.next_item().item.type in ['url', 'url_from_playlist']:
|
||||||
# usually, all validation will be done when adding to the list.
|
# usually, all validation will be done when adding to the list.
|
||||||
# however, for performance consideration, youtube playlist won't be validate when added.
|
# however, for performance consideration, youtube playlist won't be validate when added.
|
||||||
# the validation has to be done here.
|
# the validation has to be done here.
|
||||||
@ -390,15 +390,14 @@ class MumbleBot:
|
|||||||
if var.playlist.next():
|
if var.playlist.next():
|
||||||
current = var.playlist.current_item().item
|
current = var.playlist.current_item().item
|
||||||
if current.validate():
|
if current.validate():
|
||||||
print("validate")
|
|
||||||
if current.is_ready():
|
if current.is_ready():
|
||||||
print("ready")
|
|
||||||
self.launch_music()
|
self.launch_music()
|
||||||
self.async_download_next()
|
self.async_download_next()
|
||||||
else:
|
else:
|
||||||
self.log.info("bot: current music isn't ready, start downloading.")
|
self.log.info("bot: current music isn't ready, start downloading.")
|
||||||
self.wait_for_downloading = True
|
self.wait_for_downloading = True
|
||||||
current.async_prepare()
|
current.async_prepare()
|
||||||
|
self.send_msg(constants.strings('download_in_progress', item=current.format_short_string()))
|
||||||
else:
|
else:
|
||||||
var.playlist.remove_by_id(current.id)
|
var.playlist.remove_by_id(current.id)
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user