fix: update playlist item based on id #90.

This commit is contained in:
Terry Geng 2020-03-03 23:15:46 +08:00
parent 499186c738
commit 0760f3e624
4 changed files with 51 additions and 23 deletions

View File

@ -163,7 +163,7 @@ def post():
'path' : request.form['add_file_bottom'], 'path' : request.form['add_file_bottom'],
'title' : '', 'title' : '',
'user' : 'Remote Control'} '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)) 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']: elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:

View File

@ -73,7 +73,7 @@ class MumbleBot:
self.is_pause = False self.is_pause = False
self.playhead = -1 self.playhead = -1
self.song_start_at = -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 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"): if var.config.getboolean("webinterface", "enabled"):
@ -319,8 +319,10 @@ class MumbleBot:
# Check if the music is ready to be played # Check if the music is ready to be played
if music["ready"] != "yes" or not os.path.exists(music['path']): if music["ready"] != "yes" or not os.path.exists(music['path']):
self.log.info("bot: current music isn't ready, start to download.") self.wait_for_downloading = True
music = self.download_music() self.log.info("bot: current music isn't ready, start downloading.")
self.async_download(index)
return
if music['ready'] == 'failed': if music['ready'] == 'failed':
self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music)) 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): if not os.path.isfile(mp3):
# download the music # download the music
music['ready'] = "downloading" 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)) self.log.info("bot: downloading url (%s) %s " % (music['title'], url))
ydl_opts = "" ydl_opts = ""
@ -468,9 +470,9 @@ class MumbleBot:
self.log.info("bot: music file existed, skip downloading " + mp3) self.log.info("bot: music file existed, skip downloading " + mp3)
music['ready'] = "yes" 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 self.download_in_progress = False
return music return music
@ -485,18 +487,23 @@ class MumbleBot:
while var.playlist.next_item() and var.playlist.next_item()['ready'] == "validation": while var.playlist.next_item() and var.playlist.next_item()['ready'] == "validation":
music = self.validate_music(var.playlist.next_item()) music = self.validate_music(var.playlist.next_item())
if music: if music:
var.playlist.update(music, var.playlist.next_index()) var.playlist.update(music, music['id'])
break break
else: else:
var.playlist.remove(var.playlist.next_index()) var.playlist.remove(var.playlist.next_index())
if var.playlist.next_item() and var.playlist.next_item()['ready'] == "no": if var.playlist.next_item() and var.playlist.next_item()['ready'] == "no":
self.async_download(var.playlist.next_index())
def async_download(self, index):
th = threading.Thread( th = threading.Thread(
target=self.download_music, name="DownloadThread", args=(var.playlist.next_index(),)) target=self.download_music, name="DownloadThread-" + var.playlist[index]['id'][:5], args=(index,))
self.log.info( self.log.info(
"bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist.next_item())) "bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist[index]))
th.daemon = True th.daemon = True
th.start() th.start()
#self.download_threads.append(th)
return th
def check_item_path_or_remove(self, index = -1): def check_item_path_or_remove(self, index = -1):
if index == -1: if index == -1:
@ -576,7 +583,6 @@ class MumbleBot:
else: else:
self._loop_status = 'Empty queue' self._loop_status = 'Empty queue'
else: else:
print(var.playlist.current_item()["ready"])
if var.playlist.current_item()["ready"] != "downloading": if var.playlist.current_item()["ready"] != "downloading":
self.wait_for_downloading = False self.wait_for_downloading = False
self.launch_music() self.launch_music()

View File

@ -1,5 +1,6 @@
import json import json
import random import random
import hashlib
import util import util
import variables as var import variables as var
@ -7,6 +8,7 @@ import variables as var
""" """
FORMAT OF A MUSIC INTO THE PLAYLIST FORMAT OF A MUSIC INTO THE PLAYLIST
type : url type : url
id
url url
title title
path path
@ -20,12 +22,14 @@ type : url
playlist_url playlist_url
type : radio type : radio
id
url url
name name
current_title current_title
user user
type : file type : file
id
path path
title title
artist artist
@ -63,7 +67,7 @@ class PlayList(list):
def append(self, item): def append(self, item):
self.version += 1 self.version += 1
item = util.get_music_tag_info(item) item = util.attach_music_tag_info(item)
super().append(item) super().append(item)
return item return item
@ -74,7 +78,7 @@ class PlayList(list):
if index == -1: if index == -1:
index = self.current_index index = self.current_index
item = util.get_music_tag_info(item) 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:
@ -88,7 +92,7 @@ class PlayList(list):
def extend(self, items): def extend(self, items):
self.version += 1 self.version += 1
items = list(map( items = list(map(
lambda item: util.get_music_tag_info(item), lambda item: util.attach_music_tag_info(item),
items)) items))
super().extend(items) super().extend(items)
return items return items
@ -120,11 +124,19 @@ class PlayList(list):
else: else:
raise TypeError("Unknown playlist mode '%s'." % self.mode) 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 self.version += 1
if index == -1: index = self.find(id)
index = self.current_index if index:
self[index] = item self[index] = item
return True
return False
def __delitem__(self, key): def __delitem__(self, key):
return self.remove(key) return self.remove(key)

12
util.py
View File

@ -74,8 +74,18 @@ def get_music_path(music):
return uri 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: if "path" in music:
uri = get_music_path(music) uri = get_music_path(music)