fix: launch_music didn't wait for downloading #89

This commit is contained in:
Terry Geng 2020-03-03 10:11:42 +08:00
parent 61497b1ecb
commit 28320ddfe5
4 changed files with 31 additions and 16 deletions

View File

@ -311,6 +311,8 @@ def cmd_play_playlist(bot, user, text, command, parameter):
var.playlist.extend(items) var.playlist.extend(items)
for music in items: for music in items:
log.info("cmd: add to playlist: " + util.format_debug_song_string(music)) 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): def cmd_play_radio(bot, user, text, command, parameter):

View 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. <i>!ytquery -n</i> for the next page.
yt_no_more = No more results! yt_no_more = No more results!
yt_query_error = Unable to query youtube! yt_query_error = Unable to query youtube!
playlist_fetching_failed = Unable to fetch the playlist!
help = <h3>Commands</h3> help = <h3>Commands</h3>
<b>Control</b> <b>Control</b>

View File

@ -114,6 +114,11 @@ class PlayList(list):
def current_item(self): def current_item(self):
return self[self.current_index] 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): def next_index(self):
if len(self) == 0: if len(self) == 0:
return False return False

View File

@ -45,7 +45,7 @@ type : url
artist artist
thumbnail thumbnail
user user
ready (validation, no, downloading, yes) ready (validation, no, downloading, yes, failed)
from_playlist (yes,no) from_playlist (yes,no)
playlist_title playlist_title
playlist_url playlist_url
@ -340,15 +340,19 @@ class MumbleBot:
# Check if the music is ready to be played # Check if the music is ready to be played
if music["ready"] == "downloading": 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']): elif music["ready"] != "yes" or not os.path.exists(music['path']):
self.log.info("bot: current music isn't ready, downloading...") self.log.info("bot: current music isn't ready, start to download.")
downloaded_music = self.download_music() music = self.download_music()
if not downloaded_music:
self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music)) if music['ready'] == 'failed':
var.playlist.remove(index) self.log.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music))
return var.playlist.remove(index)
music = downloaded_music return
uri = music['path'] uri = music['path']
elif music["type"] == "file": elif music["type"] == "file":
@ -468,7 +472,6 @@ class MumbleBot:
self.log.info("bot: download attempts %d / %d" % (i+1, attempts)) self.log.info("bot: download attempts %d / %d" % (i+1, attempts))
try: try:
ydl.extract_info(url) ydl.extract_info(url)
music['ready'] = "yes"
download_succeed = True download_succeed = True
break break
except: except:
@ -476,17 +479,20 @@ class MumbleBot:
error = error_traceback.rstrip().split("\n")[-1] error = error_traceback.rstrip().split("\n")[-1]
self.log.error("bot: download failed with error:\n %s" % error) 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")]: for f in [mp3, path.replace(".%(ext)s", ".jpg"), path.replace(".%(ext)s", ".m4a")]:
if os.path.exists(f): if os.path.exists(f):
os.remove(f) os.remove(f)
self.send_msg(constants.strings('unable_download')) self.send_msg(constants.strings('unable_download'))
return False music['ready'] = "failed"
else: else:
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"
self.log.info("bot: finished downloading url (%s) %s, saved to %s." % (music['title'], url, music['path']))
music = util.get_music_tag_info(music) music = util.get_music_tag_info(music)
var.playlist.update(music, index) var.playlist.update(music, index)
@ -574,9 +580,10 @@ class MumbleBot:
if self.thread is None or not raw_music: if self.thread is None or not raw_music:
# Not music into the buffet # Not music into the buffet
if not self.is_pause and var.playlist.next(): if not self.is_pause:
self.launch_music() if len(var.playlist) > 0 and var.playlist.next():
self.async_download_next() self.launch_music()
self.async_download_next()
while self.mumble.sound_output.get_buffer_size() > 0: while self.mumble.sound_output.get_buffer_size() > 0:
# Empty the buffer before exit # Empty the buffer before exit