fix: impose file path check for launch_music and resume, fixed #77

This commit is contained in:
Terry Geng 2020-02-26 16:57:56 +08:00
parent 78b11d1fb5
commit 9ce98196a1
4 changed files with 59 additions and 30 deletions

View File

@ -257,6 +257,8 @@ def cmd_play_url(bot, user, text, command, parameter):
music['ready'] = "no" music['ready'] = "no"
var.playlist.append(music) var.playlist.append(music)
logging.info("cmd: add to playlist: " + music['url']) logging.info("cmd: add to playlist: " + music['url'])
if var.playlist.length() == 2:
# If I am the second item on the playlist. (I am the next one!)
bot.async_download_next() bot.async_download_next()
else: else:
bot.send_msg(constants.strings('unable_download'), text) bot.send_msg(constants.strings('unable_download'), text)
@ -514,7 +516,8 @@ def cmd_current_music(bot, user, text, command, parameter):
def cmd_skip(bot, user, text, command, parameter): def cmd_skip(bot, user, text, command, parameter):
if bot.next(): # Is no number send, just skip the current music if var.playlist.length() > 0:
bot.stop()
bot.launch_music() bot.launch_music()
bot.async_download_next() bot.async_download_next()
else: else:

View File

@ -182,6 +182,7 @@ cleared = Playlist emptied.
database_dropped = Database dropped. All records have gone. database_dropped = Database dropped. All records have gone.
new_version_found = <h3>Update Available!</h3> New version of botamusique is available, send <i>!update</i> to update! new_version_found = <h3>Update Available!</h3> New version of botamusique is available, send <i>!update</i> to update!
start_updating = Start updating... start_updating = Start updating...
file_missed = Music file '{file}' missed! This item has been removed from the playlist.
help = <h3>Commands</h3> help = <h3>Commands</h3>
<b>Control</b> <b>Control</b>

View File

@ -13,7 +13,7 @@ def strings(option, *argv, **kwargs):
return formatted return formatted
except KeyError as e: except KeyError as e:
raise KeyError( raise KeyError(
"Missed placeholder {{{placeholder}}} in string '{string}'. ".format(placeholder=str(e).strip("'"), string=option) + "Missed/Unexpected placeholder {{{placeholder}}} in string '{string}'. ".format(placeholder=str(e).strip("'"), string=option) +
"Please restore you configuration file back to default if necessary.") "Please restore you configuration file back to default if necessary.")
except TypeError as e: except TypeError as e:
raise KeyError( raise KeyError(

View File

@ -331,6 +331,8 @@ class MumbleBot:
uri = music['path'] uri = music['path']
elif music["type"] == "file": elif music["type"] == "file":
if not self.check_item_path_or_remove():
return
uri = var.config.get('bot', 'music_folder') + \ uri = var.config.get('bot', 'music_folder') + \
var.playlist.current_item()["path"] var.playlist.current_item()["path"]
@ -445,14 +447,20 @@ class MumbleBot:
return music return music
def resume(self): def resume(self):
self.is_playing = True
self.is_pause = False
music = var.playlist.current_item() music = var.playlist.current_item()
if music['type'] == 'radio' or self.playhead == 0 or not self.check_item_path_or_remove():
self.launch_music()
return
if var.config.getboolean('debug', 'ffmpeg'): if var.config.getboolean('debug', 'ffmpeg'):
ffmpeg_debug = "debug" ffmpeg_debug = "debug"
else: else:
ffmpeg_debug = "warning" ffmpeg_debug = "warning"
if music["type"] != "radio":
logging.info("bot: resume music at %.2f seconds" % self.playhead) logging.info("bot: resume music at %.2f seconds" % self.playhead)
uri = "" uri = ""
@ -466,11 +474,6 @@ class MumbleBot:
command = ("ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-ss', "%f" % self.playhead, '-i', command = ("ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-ss', "%f" % self.playhead, '-i',
uri, '-ac', '1', '-f', 's16le', '-ar', '48000', '-') uri, '-ac', '1', '-f', 's16le', '-ar', '48000', '-')
else:
logging.info("bot: resume radio")
uri = music["url"]
command = ("ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-i',
uri, '-ac', '1', '-f', 's16le', '-ar', '48000', '-')
if var.config.getboolean('bot', 'announce_current_music'): if var.config.getboolean('bot', 'announce_current_music'):
self.send_msg(util.format_current_playing()) self.send_msg(util.format_current_playing())
@ -482,9 +485,6 @@ class MumbleBot:
util.pipe_no_wait(pipe_rd) # Let the pipe work in non-blocking mode util.pipe_no_wait(pipe_rd) # Let the pipe work in non-blocking mode
self.thread_stderr = os.fdopen(pipe_rd) self.thread_stderr = os.fdopen(pipe_rd)
self.thread = sp.Popen(command, stdout=sp.PIPE, stderr=pipe_wd, bufsize=480) self.thread = sp.Popen(command, stdout=sp.PIPE, stderr=pipe_wd, bufsize=480)
self.is_playing = True
self.is_pause = False
self.song_start_at = -1
self.last_volume_cycle_time = time.time() self.last_volume_cycle_time = time.time()
@ -496,11 +496,39 @@ class MumbleBot:
and (var.playlist.next_item()['ready'] in ["no", "validation"]): and (var.playlist.next_item()['ready'] in ["no", "validation"]):
th = threading.Thread( th = threading.Thread(
target=self.download_music, name="DownloadThread", args=(var.playlist.next_index(),)) target=self.download_music, name="DownloadThread", args=(var.playlist.next_index(),))
else: logging.info(
return "bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist.next_item()))
logging.info("bot: start downloading item in thread: " + util.format_debug_song_string(var.playlist.next_item()))
th.daemon = True th.daemon = True
th.start() th.start()
else:
return
def check_item_path_or_remove(self, index = -1):
if index == -1:
index = var.playlist.current_index
music = var.playlist.playlist[index]
# if music['type'] == 'radio':
# return True
if not 'path' in music:
return False
else:
if music["type"] == "url":
uri = music['path']
if not os.path.exists(uri):
music['ready'] = 'validation'
return False
elif music["type"] == "file":
uri = var.config.get('bot', 'music_folder') + music["path"]
if not os.path.exists(uri):
logging.info("bot: music file missed. removing music from the playlist: %s" % util.format_debug_song_string(music))
self.send_msg(constants.strings('file_missed', file=music["path"]))
var.playlist.remove(index)
return False
return True
def volume_cycle(self): def volume_cycle(self):
delta = time.time() - self.last_volume_cycle_time delta = time.time() - self.last_volume_cycle_time
@ -564,11 +592,8 @@ class MumbleBot:
if self.is_playing: if self.is_playing:
# get next music # get next music
self.is_playing = False self.is_playing = False
self.next()
if not self.is_pause and len(var.playlist.playlist) > 0: if not self.is_pause and len(var.playlist.playlist) > 0:
if var.playlist.current_item()['type'] in ['radio', 'file'] \ self.next()
or (var.playlist.current_item()['type'] == 'url' and var.playlist.current_item()['ready'] not in ['downloading']):
# Check if the music can be start before launch the music
self.launch_music() self.launch_music()
self.async_download_next() self.async_download_next()