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"):
# 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))
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 = ""
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
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:
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()
import json
import random
+import hashlib
import util
import variables as var
"""
FORMAT OF A MUSIC INTO THE PLAYLIST
type : url
+ id
url
title
path
playlist_url
type : radio
+ id
url
name
current_title
user
type : file
+ id
path
title
artist
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
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:
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
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)
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)