From b640ff47219419f6f5406120132e4fb1d2e38acf Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Thu, 27 Feb 2020 21:10:44 +0800 Subject: [PATCH] fix: process error from youtube-dl correctly. #80 --- configuration.default.ini | 3 +++ configuration.example.ini | 3 +++ media/playlist.py | 3 ++- mumbleBot.py | 24 ++++++++++++++++-------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/configuration.default.ini b/configuration.default.ini index ae27035..d62dd01 100644 --- a/configuration.default.ini +++ b/configuration.default.ini @@ -48,6 +48,9 @@ logfile = # in MB, 0 for no cache, -1 for unlimited size tmp_folder_max_size = 10 +# how many times the bot will try to download an item +download_attempts = 2 + ignored_folders = tmp ignored_files = Thumbs.db diff --git a/configuration.example.ini b/configuration.example.ini index 3379a3b..b490f23 100644 --- a/configuration.example.ini +++ b/configuration.example.ini @@ -53,6 +53,9 @@ port = 64738 #ignored_folders = tmp #ignored_files = Thumbs.db +# 'download_attempts': how many times the bot will try to download an item +#download_attempts = 2 + # 'auto_check_update': check for updates every time the bot starts #auto_check_update = True #pip3_path = venv/bin/pip diff --git a/media/playlist.py b/media/playlist.py index b06ce40..9b5ae66 100644 --- a/media/playlist.py +++ b/media/playlist.py @@ -188,7 +188,8 @@ def get_playlist_info(url, start_index=0, user=""): 'extract_flat': 'in_playlist' } with youtube_dl.YoutubeDL(ydl_opts) as ydl: - for i in range(2): + attempts = var.config.getint('bot', 'download_attempts', fallback=2) + for i in range(attempts): try: info = ydl.extract_info(url, download=False) # # if url is not a playlist but a video diff --git a/mumbleBot.py b/mumbleBot.py index 180959a..ce1e727 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -292,7 +292,7 @@ class MumbleBot: except: error_traceback = traceback.format_exc() error = error_traceback.rstrip().split("\n")[-1] - logging.error("bot: command %s failed with error %s:\n" % (command_exc, error_traceback)) + logging.error("bot: command %s failed with error: %s\n" % (command_exc, error_traceback)) self.send_msg(constants.strings('error_executing_command', command=command_exc, error=error), text) def send_msg(self, msg, text=None): @@ -342,6 +342,7 @@ class MumbleBot: logging.info("bot: removing music from the playlist: %s" % util.format_debug_song_string(music)) var.playlist.remove(index) return + music = downloaded_music uri = music['path'] elif music["type"] == "file": @@ -456,19 +457,26 @@ class MumbleBot: self.send_msg(constants.strings('download_in_progress', item=music['title'])) with youtube_dl.YoutubeDL(ydl_opts) as ydl: - for i in range(2): # Always try 2 times + attempts = var.config.getint('bot', 'download_attempts', fallback=2) + download_succeed = False + for i in range(attempts): + logging.info("bot: download attempts %d / %d" % (i+1, attempts)) try: ydl.extract_info(url) - if 'ready' in music and music['ready'] == "downloading": - music['ready'] = "yes" - except youtube_dl.utils.DownloadError: - pass - else: - break + music['ready'] = "yes" + download_succeed = True + except: + error_traceback = traceback.format_exc() + logging.error("bot: download failed with error:\n %s" % error_traceback) + + if not download_succeed: + self.send_msg(constants.strings('unable_download')) + return False else: logging.info("bot: music file existed, skip downloading " + mp3) music['ready'] = "yes" + logging.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)