From 7e9c25ef53a4a9fa72212a76d12ff8cd25be806d Mon Sep 17 00:00:00 2001 From: Salt Date: Fri, 6 Mar 2020 04:08:07 -0600 Subject: [PATCH] Wrap most of the download function in a try catch --- badwitch.py | 75 ++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/badwitch.py b/badwitch.py index b02d416..8b7ebe0 100755 --- a/badwitch.py +++ b/badwitch.py @@ -75,42 +75,45 @@ class Library: Path(destpath).mkdir(parents=True, exist_ok=True) # Actually download and tag songs for song, songcontent in albumcontent.items(): - zeroes = int(math.log10(len(albumcontent)) + 1) - filename = str(songcontent['track']).zfill(zeroes) + ' - ' + song - destfile = str(destpath / filename) + '.%(ext)s' - logging.debug('Saving to: ' + destfile) - # See if we already have it - if Path(str(destpath / filename) + '.mp3').exists(): - # Skip downloading - logging.info('Already have song: ' + song) - else: - # Download the song - ytdl_opts = { - 'format': 'bestaudio', - 'outtmpl': destfile, - 'playlist_items': 1, - 'quiet': True, - 'writethumbnail': True, - 'postprocessors': [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192' - },{ - 'key': 'EmbedThumbnail' - }] - } - with youtube_dl.YoutubeDL(ytdl_opts) as ydl: - ydl.download([songcontent['source']]) - print('Downloaded song: ' + song) - # Add tags - logging.debug('Adding tags') - resultfile = eyed3.load(str(destpath / filename) + '.mp3') - resultfile.tag.album_artist = albumartist - resultfile.tag.artist = songcontent['artist'] - resultfile.tag.album = album - resultfile.tag.title = song - resultfile.tag.track_num = songcontent['track'] - resultfile.tag.save() + try: + zeroes = int(math.log10(len(albumcontent)) + 1) + filename = str(songcontent['track']).zfill(zeroes) + ' - ' + song + destfile = str(destpath / filename) + '.%(ext)s' + logging.debug('Saving to: ' + destfile) + # See if we already have it + if Path(str(destpath / filename) + '.mp3').exists(): + # Skip downloading + logging.info('Already have song: ' + song) + else: + # Download the song + ytdl_opts = { + 'format': 'bestaudio', + 'outtmpl': destfile, + 'playlist_items': 1, + 'quiet': True, + 'writethumbnail': True, + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192' + },{ + 'key': 'EmbedThumbnail' + }] + } + with youtube_dl.YoutubeDL(ytdl_opts) as ydl: + ydl.download([songcontent['source']]) + print('Downloaded song: ' + song) + # Add tags + logging.debug('Adding tags') + resultfile = eyed3.load(str(destpath / filename) + '.mp3') + resultfile.tag.album_artist = albumartist + resultfile.tag.artist = songcontent['artist'] + resultfile.tag.album = album + resultfile.tag.title = song + resultfile.tag.track_num = songcontent['track'] + resultfile.tag.save() + except (KeyboardInterrupt, EOFError): + logging.debug('Interrupt received, exiting') class BadWitch: # Our program