fix: process error from youtube-dl correctly. #80

This commit is contained in:
Terry Geng 2020-02-27 21:10:44 +08:00
parent f90482ab3c
commit b640ff4721
4 changed files with 24 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)