fix: delete item keyerror #91

This commit is contained in:
Terry Geng 2020-03-08 09:26:36 +08:00
parent da6b028e2c
commit e10059a76e
6 changed files with 30 additions and 22 deletions

View File

@ -287,7 +287,7 @@ def cmd_play_url(bot, user, text, command, parameter):
var.playlist.append(music_wrapper) var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string()) log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text) bot.send_msg(constants.strings('file_added', item=music_wrapper.format_short_string()), text)
if len(var.playlist) == 2: if len(var.playlist) == 2:
# If I am the second item on the playlist. (I am the next one!) # If I am the second item on the playlist. (I am the next one!)
bot.async_download_next() bot.async_download_next()

View File

@ -86,7 +86,8 @@ class BaseItem:
return "" return ""
def send_client_message(self, msg): def send_client_message(self, msg):
self.bot.send_msg(msg) if self.bot:
self.bot.send_msg(msg)
def to_dict(self): def to_dict(self):
return {"type" : "base", "id": self.id, "ready": self.ready, "path": self.path, "tags": self.tags} return {"type" : "base", "id": self.id, "ready": self.ready, "path": self.path, "tags": self.tags}

View File

@ -73,18 +73,22 @@ class MusicLibrary(dict):
self.log.debug("library: music save into database: %s" % self[id].format_debug_string()) self.log.debug("library: music save into database: %s" % self[id].format_debug_string())
self.db.insert_music(self[id].to_dict()) self.db.insert_music(self[id].to_dict())
def delete(self, item): def delete(self, id):
self.log.debug("library: DELETE item from the database: %s" % item.format_debug_string()) try:
item = self.get_item_by_id(None, id)
self.log.debug("library: DELETE item from the database: %s" % item.format_debug_string())
if item.type == 'file' and item.path in self.file_id_lookup: if item.type == 'file' and item.path in self.file_id_lookup:
if item.path in self.file_id_lookup: if item.path in self.file_id_lookup:
del self.file_id_lookup[item.path] del self.file_id_lookup[item.path]
self.files.remove(item.path) self.files.remove(item.path)
self.save_dir_cache() self.save_dir_cache()
if item.id in self: if item.id in self:
del self[item.id] del self[item.id]
self.db.delete_music(id=item.id) self.db.delete_music(id=item.id)
except KeyError:
return
def free(self, id): def free(self, id):
if id in self: if id in self:

View File

@ -235,8 +235,6 @@ class BasePlaylist(list):
for index in to_be_removed: for index in to_be_removed:
self.remove(index) self.remove(index)
var.library.free(id)
def current_item(self): def current_item(self):
if len(self) == 0: if len(self) == 0:
return False return False
@ -318,8 +316,8 @@ class BasePlaylist(list):
self.log.debug("playlist: validating %s" % item.format_debug_string()) self.log.debug("playlist: validating %s" % item.format_debug_string())
if not item.validate() or item.is_failed(): if not item.validate() or item.is_failed():
self.log.debug("playlist: validating failed.") self.log.debug("playlist: validating failed.")
var.library.delete(item.id)
self.remove_by_id(item.id) self.remove_by_id(item.id)
var.library.delete(item.item())
self.log.debug("playlist: validating finished.") self.log.debug("playlist: validating finished.")
self.validating_thread_lock.release() self.validating_thread_lock.release()
@ -332,9 +330,11 @@ class OneshotPlaylist(BasePlaylist):
self.current_index = -1 self.current_index = -1
def from_list(self, _list, current_index): def from_list(self, _list, current_index):
for i in range(current_index): if len(_list) > 0:
_list.pop() for i in range(current_index):
return super().from_list(_list, -1) _list.pop()
return super().from_list(_list, -1)
return self
def next(self): def next(self):
if len(self) == 0: if len(self) == 0:

View File

@ -68,15 +68,18 @@ class URLItem(BaseItem):
return True return True
def validate(self): def validate(self):
self.validating_lock.acquire()
if self.ready in ['yes', 'validated']: if self.ready in ['yes', 'validated']:
return True return True
if self.ready == 'failed':
return False
if os.path.exists(self.path): if os.path.exists(self.path):
self.ready = "yes" self.ready = "yes"
return True return True
# avoid multiple process validating in the meantime # avoid multiple process validating in the meantime
self.validating_lock.acquire()
info = self._get_info_from_url() info = self._get_info_from_url()
self.validating_lock.release() self.validating_lock.release()
@ -232,7 +235,7 @@ class URLItem(BaseItem):
return display return display
def format_short_string(self): def format_short_string(self):
return self.title if self.title else self.url return self.title if self.title.strip() else self.url
def display_type(self): def display_type(self):
return constants.strings("url") return constants.strings("url")

View File

@ -347,7 +347,7 @@ class MumbleBot:
break break
else: else:
var.playlist.remove_by_id(next.id) var.playlist.remove_by_id(next.id)
var.library.delete(next.item()) var.library.delete(next.id)
# ======================= # =======================
@ -407,7 +407,7 @@ class MumbleBot:
self.send_msg(constants.strings('download_in_progress', item=current.format_short_string())) 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)
var.library.delete(current.item()) var.library.delete(current.id)
else: else:
self._loop_status = 'Empty queue' self._loop_status = 'Empty queue'
else: else: