fix: refresh video metatdata after validation

This commit is contained in:
Terry Geng 2020-03-12 18:18:12 +08:00
parent 7af9d29932
commit a2cb955ed8
4 changed files with 28 additions and 16 deletions

View File

@ -134,7 +134,7 @@ def cmd_url_ban(bot, user, text, command, parameter):
var.cache.free_and_delete(id) var.cache.free_and_delete(id)
var.playlist.remove_by_id(id) var.playlist.remove_by_id(id)
else: else:
if var.playlist.current_item().type == 'url': if var.playlist.current_item() and var.playlist.current_item().type == 'url':
item = var.playlist.current_item().item() item = var.playlist.current_item().item()
bot.mumble.users[text.actor].send_text_message(util.url_ban(util.get_url_from_input(item.url))) bot.mumble.users[text.actor].send_text_message(util.url_ban(util.get_url_from_input(item.url)))
var.cache.free_and_delete(item.id) var.cache.free_and_delete(item.id)

View File

@ -177,15 +177,6 @@ class CachedItemWrapper:
self.lib.save(self.id) self.lib.save(self.id)
return ret return ret
def async_prepare(self):
th = threading.Thread(
target=self.prepare, name="Prepare-" + self.id[:7])
self.log.info(
"%s: start preparing item in thread: " % self.item().type + self.format_debug_string())
th.daemon = True
th.start()
return th
def uri(self): def uri(self):
return self.item().uri() return self.item().uri()

View File

@ -58,7 +58,7 @@ class BasePlaylist(list):
self.version += 1 self.version += 1
super().append(item) super().append(item)
self.pending_items.append(item) self.pending_items.append(item)
self.start_async_validating() self.async_validate()
return item return item
@ -74,7 +74,7 @@ class BasePlaylist(list):
self.current_index += 1 self.current_index += 1
self.pending_items.append(item) self.pending_items.append(item)
self.start_async_validating() self.async_validate()
return item return item
@ -82,7 +82,7 @@ class BasePlaylist(list):
self.version += 1 self.version += 1
super().extend(items) super().extend(items)
self.pending_items.extend(items) self.pending_items.extend(items)
self.start_async_validating() self.async_validate()
return items return items
def next(self): def next(self):
@ -210,7 +210,7 @@ class BasePlaylist(list):
print("%d %s" % (index, item_wrapper.format_debug_string())) print("%d %s" % (index, item_wrapper.format_debug_string()))
print("===== End =====") print("===== End =====")
def start_async_validating(self): def async_validate(self):
if not self.validating_thread_lock.locked(): if not self.validating_thread_lock.locked():
time.sleep(0.1) # Just avoid validation finishes too fast and delete songs while something is reading it. time.sleep(0.1) # Just avoid validation finishes too fast and delete songs while something is reading it.
th = threading.Thread(target=self._check_valid, name="Validating") th = threading.Thread(target=self._check_valid, name="Validating")
@ -223,14 +223,34 @@ class BasePlaylist(list):
while len(self.pending_items) > 0: while len(self.pending_items) > 0:
item = self.pending_items.pop() item = self.pending_items.pop()
self.log.debug("playlist: validating %s" % item.format_debug_string()) self.log.debug("playlist: validating %s" % item.format_debug_string())
ver = item.version
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.cache.free_and_delete(item.id) var.cache.free_and_delete(item.id)
self.remove_by_id(item.id) self.remove_by_id(item.id)
continue
if item.version > ver:
self.version += 1
self.log.debug("playlist: validating finished.") self.log.debug("playlist: validating finished.")
self.validating_thread_lock.release() self.validating_thread_lock.release()
def async_prepare(self, index):
th = threading.Thread(
target=self._prepare, name="Prepare-" + self[index].id[:7], args=(index,))
self.log.info(
"%s: start preparing item in thread: " % self[index].item().type + self[index].format_debug_string())
th.daemon = True
th.start()
return th
def _prepare(self, index):
item = self[index]
ver = item.version
item.prepare()
if item.version > ver:
self.version += 1
class OneshotPlaylist(BasePlaylist): class OneshotPlaylist(BasePlaylist):
def __init__(self): def __init__(self):

View File

@ -355,7 +355,7 @@ class MumbleBot:
next = var.playlist.next_item() next = var.playlist.next_item()
if next.validate(): if next.validate():
if not next.is_ready(): if not next.is_ready():
next.async_prepare() var.playlist.async_prepare(var.playlist.next_index())
break break
else: else:
var.playlist.remove_by_id(next.id) var.playlist.remove_by_id(next.id)
@ -427,7 +427,7 @@ class MumbleBot:
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() var.playlist.async_prepare(var.playlist.current_index)
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)
@ -439,6 +439,7 @@ class MumbleBot:
if current: if current:
if current.is_ready(): if current.is_ready():
self.wait_for_downloading = False self.wait_for_downloading = False
var.playlist.version += 1
self.launch_music() self.launch_music()
self.async_download_next() self.async_download_next()
elif current.is_failed(): elif current.is_failed():