From: Terry Geng Date: Tue, 3 Mar 2020 15:15:46 +0000 (+0800) Subject: fix: update playlist item based on id #90. X-Git-Url: https://git.0d.be/?p=botaradio.git;a=commitdiff_plain;h=0760f3e6245055f96df8b669e449fee5895cdf54 fix: update playlist item based on id #90. --- diff --git a/interface.py b/interface.py index da868e7..576e801 100644 --- a/interface.py +++ b/interface.py @@ -163,7 +163,7 @@ def post(): 'path' : request.form['add_file_bottom'], 'title' : '', 'user' : 'Remote Control'} - item = var.playlist.append(util.get_music_tag_info(item)) + item = var.playlist.append(util.attach_music_tag_info(item)) log.info('web: add to playlist(bottom): ' + util.format_debug_song_string(item)) elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']: diff --git a/mumbleBot.py b/mumbleBot.py index ea8a014..4d1da1c 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -73,7 +73,7 @@ class MumbleBot: self.is_pause = False self.playhead = -1 self.song_start_at = -1 - self.download_in_progress = False + #self.download_threads = [] self.wait_for_downloading = False # flag for the loop are waiting for download to complete in the other thread if var.config.getboolean("webinterface", "enabled"): @@ -319,8 +319,10 @@ class MumbleBot: # Check if the music is ready to be played if music["ready"] != "yes" or not os.path.exists(music['path']): - self.log.info("bot: current music isn't ready, start to download.") - music = self.download_music() + self.wait_for_downloading = True + self.log.info("bot: current music isn't ready, start downloading.") + self.async_download(index) + return if music['ready'] == 'failed': self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music)) @@ -421,7 +423,7 @@ class MumbleBot: if not os.path.isfile(mp3): # download the music music['ready'] = "downloading" - var.playlist.update(music, index) + var.playlist.update(music, music['id']) self.log.info("bot: downloading url (%s) %s " % (music['title'], url)) ydl_opts = "" @@ -468,9 +470,9 @@ class MumbleBot: self.log.info("bot: music file existed, skip downloading " + mp3) music['ready'] = "yes" - music = util.get_music_tag_info(music) + music = util.attach_music_tag_info(music) - var.playlist.update(music, index) + var.playlist.update(music, music['id']) self.download_in_progress = False return music @@ -485,18 +487,23 @@ class MumbleBot: while var.playlist.next_item() and var.playlist.next_item()['ready'] == "validation": music = self.validate_music(var.playlist.next_item()) if music: - var.playlist.update(music, var.playlist.next_index()) + var.playlist.update(music, music['id']) break else: var.playlist.remove(var.playlist.next_index()) if var.playlist.next_item() and var.playlist.next_item()['ready'] == "no": - th = threading.Thread( - target=self.download_music, name="DownloadThread", args=(var.playlist.next_index(),)) - self.log.info( - "bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist.next_item())) - th.daemon = True - th.start() + self.async_download(var.playlist.next_index()) + + def async_download(self, index): + th = threading.Thread( + target=self.download_music, name="DownloadThread-" + var.playlist[index]['id'][:5], args=(index,)) + self.log.info( + "bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist[index])) + th.daemon = True + th.start() + #self.download_threads.append(th) + return th def check_item_path_or_remove(self, index = -1): if index == -1: @@ -576,7 +583,6 @@ class MumbleBot: else: self._loop_status = 'Empty queue' else: - print(var.playlist.current_item()["ready"]) if var.playlist.current_item()["ready"] != "downloading": self.wait_for_downloading = False self.launch_music() diff --git a/playlist.py b/playlist.py index 42d2be1..44bc39c 100644 --- a/playlist.py +++ b/playlist.py @@ -1,5 +1,6 @@ import json import random +import hashlib import util import variables as var @@ -7,6 +8,7 @@ import variables as var """ FORMAT OF A MUSIC INTO THE PLAYLIST type : url + id url title path @@ -20,12 +22,14 @@ type : url playlist_url type : radio + id url name current_title user type : file + id path title artist @@ -63,7 +67,7 @@ class PlayList(list): def append(self, item): self.version += 1 - item = util.get_music_tag_info(item) + item = util.attach_music_tag_info(item) super().append(item) return item @@ -74,7 +78,7 @@ class PlayList(list): if index == -1: index = self.current_index - item = util.get_music_tag_info(item) + item = util.attach_music_tag_info(item) super().insert(index, item) if index <= self.current_index: @@ -88,7 +92,7 @@ class PlayList(list): def extend(self, items): self.version += 1 items = list(map( - lambda item: util.get_music_tag_info(item), + lambda item: util.attach_music_tag_info(item), items)) super().extend(items) return items @@ -120,11 +124,19 @@ class PlayList(list): else: raise TypeError("Unknown playlist mode '%s'." % self.mode) - def update(self, item, index=-1): + def find(self, id): + for index, item in enumerate(self): + if item['id'] == id: + return index + return None + + def update(self, item, id): self.version += 1 - if index == -1: - index = self.current_index - self[index] = item + index = self.find(id) + if index: + self[index] = item + return True + return False def __delitem__(self, key): return self.remove(key) diff --git a/util.py b/util.py index 96ac83d..a988637 100644 --- a/util.py +++ b/util.py @@ -74,8 +74,18 @@ def get_music_path(music): return uri +def attach_item_id(item): + if item['type'] == 'url': + item['id'] = hashlib.md5(item['url'].encode()).hexdigest() + elif item['type'] == 'file': + item['id'] = hashlib.md5(item['path'].encode()).hexdigest() + elif item['type'] == 'radio': + item['id'] = hashlib.md5(item['url'].encode()).hexdigest() + return item + +def attach_music_tag_info(music): + music = attach_item_id(music) -def get_music_tag_info(music): if "path" in music: uri = get_music_path(music)