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