]> git.0d.be Git - botaradio.git/commitdiff
fix: update playlist item based on id #90.
authorTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 15:15:46 +0000 (23:15 +0800)
committerTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 15:15:46 +0000 (23:15 +0800)
interface.py
mumbleBot.py
playlist.py
util.py

index da868e7456d246824cc57c2acecd3dee796b0add..576e80103a41ddb617cc212fdf34c018bcbfd01d 100644 (file)
@@ -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']:
index ea8a014ba70006f5875e8a22027f3b8a15a3c2ce..4d1da1c8207fce026d09ec2d447b0e6a5add4b6f 100644 (file)
@@ -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()
index 42d2be1358e5cbe03f369a52e7a87993867d7f6e..44bc39cc9b5c536f6a2d0dc2b76ad1f1c373a29d 100644 (file)
@@ -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 96ac83d54aba814cea77b76df84cd75f81d58717..a9886372567871388589e59f2eed6cad8555d8f7 100644 (file)
--- 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)