]> git.0d.be Git - botaradio.git/commitdiff
fix: launch_music didn't wait for downloading #89
authorTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 02:11:42 +0000 (10:11 +0800)
committerTerry Geng <gengyanda@gmail.com>
Tue, 3 Mar 2020 02:11:50 +0000 (10:11 +0800)
command.py
configuration.default.ini
media/playlist.py
mumbleBot.py

index 2fd317b8612db5ef1e4a66a770aa5d7582772f49..92581cf5141e6409c8658b20e8c1b332b98a99f5 100644 (file)
@@ -311,6 +311,8 @@ def cmd_play_playlist(bot, user, text, command, parameter):
         var.playlist.extend(items)
         for music in items:
             log.info("cmd: add to playlist: " + util.format_debug_song_string(music))
+    else:
+        bot.send_msg(constants.strings("playlist_fetching_failed"), text)
 
 
 def cmd_play_radio(bot, user, text, command, parameter):
index 5db47fd7f0d516c63f8ab305df1dfe10c4b0eb3c..669e57411fe936edc03e92f7482ebcf85c6d9488 100644 (file)
@@ -210,6 +210,7 @@ yt_result = Youtube query result: {result_table} Use <i>!ytplay</i> {{index}} to
             <i>!ytquery -n</i> for the next page.
 yt_no_more = No more results!
 yt_query_error = Unable to query youtube!
+playlist_fetching_failed = Unable to fetch the playlist!
 
 help = <h3>Commands</h3>
        <b>Control</b>
index b8d1be4e730c45c6e49e20d39b09632bd659c1f9..576485a4f277bfabee53ff8697baea0ad612a6d4 100644 (file)
@@ -114,6 +114,11 @@ class PlayList(list):
     def current_item(self):
         return self[self.current_index]
 
+    def current_item_downloading(self):
+        if self[self.current_index]['type'] == 'url' and self[self.current_index]['ready'] == 'downloading':
+            return True
+        return False
+
     def next_index(self):
         if len(self) == 0:
             return False
index 8ce8fa7bcfbc28cf52e73d71bb4a9c62c088c8a0..827272aecda9e8741cbd2a430f2117f1db4c3e24 100644 (file)
@@ -45,7 +45,7 @@ type : url
     artist
     thumbnail
     user
-    ready (validation, no, downloading, yes)
+    ready (validation, no, downloading, yes, failed)
     from_playlist (yes,no)
     playlist_title
     playlist_url
@@ -340,15 +340,19 @@ class MumbleBot:
 
             # Check if the music is ready to be played
             if music["ready"] == "downloading":
-                return
+                self.log.info("bot: current music isn't ready, downloading in progress.")
+                while var.playlist.current_item_downloading():
+                    time.sleep(0.5)
+                music = var.playlist.current_item()
+
             elif music["ready"] != "yes" or not os.path.exists(music['path']):
-                self.log.info("bot: current music isn't ready, downloading...")
-                downloaded_music = self.download_music()
-                if not downloaded_music:
-                    self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music))
-                    var.playlist.remove(index)
-                    return
-                music = downloaded_music
+                self.log.info("bot: current music isn't ready, start to download.")
+                music = self.download_music()
+
+            if music['ready'] == 'failed':
+                self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music))
+                var.playlist.remove(index)
+                return
             uri = music['path']
 
         elif music["type"] == "file":
@@ -468,7 +472,6 @@ class MumbleBot:
                     self.log.info("bot: download attempts %d / %d" % (i+1, attempts))
                     try:
                         ydl.extract_info(url)
-                        music['ready'] = "yes"
                         download_succeed = True
                         break
                     except:
@@ -476,17 +479,20 @@ class MumbleBot:
                         error = error_traceback.rstrip().split("\n")[-1]
                         self.log.error("bot: download failed with error:\n %s" % error)
 
-                if not download_succeed:
+                if download_succeed:
+                    music['ready'] = "yes"
+                    self.log.info(
+                        "bot: finished downloading url (%s) %s, saved to %s." % (music['title'], url, music['path']))
+                else:
                     for f in [mp3, path.replace(".%(ext)s", ".jpg"), path.replace(".%(ext)s", ".m4a")]:
                         if os.path.exists(f):
                             os.remove(f)
                     self.send_msg(constants.strings('unable_download'))
-                    return False
+                    music['ready'] = "failed"
         else:
             self.log.info("bot: music file existed, skip downloading " + mp3)
             music['ready'] = "yes"
 
-        self.log.info("bot: finished downloading url (%s) %s, saved to %s." % (music['title'], url, music['path']))
         music = util.get_music_tag_info(music)
 
         var.playlist.update(music, index)
@@ -574,9 +580,10 @@ class MumbleBot:
 
             if self.thread is None or not raw_music:
                 # Not music into the buffet
-                if not self.is_pause and var.playlist.next():
-                    self.launch_music()
-                    self.async_download_next()
+                if not self.is_pause:
+                    if len(var.playlist) > 0 and var.playlist.next():
+                        self.launch_music()
+                        self.async_download_next()
 
         while self.mumble.sound_output.get_buffer_size() > 0:
             # Empty the buffer before exit