Wrap most of the download function in a try catch

This commit is contained in:
Salt 2020-03-06 04:08:07 -06:00
parent c57009b77f
commit 7e9c25ef53

View File

@ -75,42 +75,45 @@ class Library:
Path(destpath).mkdir(parents=True, exist_ok=True) Path(destpath).mkdir(parents=True, exist_ok=True)
# Actually download and tag songs # Actually download and tag songs
for song, songcontent in albumcontent.items(): for song, songcontent in albumcontent.items():
zeroes = int(math.log10(len(albumcontent)) + 1) try:
filename = str(songcontent['track']).zfill(zeroes) + ' - ' + song zeroes = int(math.log10(len(albumcontent)) + 1)
destfile = str(destpath / filename) + '.%(ext)s' filename = str(songcontent['track']).zfill(zeroes) + ' - ' + song
logging.debug('Saving to: ' + destfile) destfile = str(destpath / filename) + '.%(ext)s'
# See if we already have it logging.debug('Saving to: ' + destfile)
if Path(str(destpath / filename) + '.mp3').exists(): # See if we already have it
# Skip downloading if Path(str(destpath / filename) + '.mp3').exists():
logging.info('Already have song: ' + song) # Skip downloading
else: logging.info('Already have song: ' + song)
# Download the song else:
ytdl_opts = { # Download the song
'format': 'bestaudio', ytdl_opts = {
'outtmpl': destfile, 'format': 'bestaudio',
'playlist_items': 1, 'outtmpl': destfile,
'quiet': True, 'playlist_items': 1,
'writethumbnail': True, 'quiet': True,
'postprocessors': [{ 'writethumbnail': True,
'key': 'FFmpegExtractAudio', 'postprocessors': [{
'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio',
'preferredquality': '192' 'preferredcodec': 'mp3',
},{ 'preferredquality': '192'
'key': 'EmbedThumbnail' },{
}] 'key': 'EmbedThumbnail'
} }]
with youtube_dl.YoutubeDL(ytdl_opts) as ydl: }
ydl.download([songcontent['source']]) with youtube_dl.YoutubeDL(ytdl_opts) as ydl:
print('Downloaded song: ' + song) ydl.download([songcontent['source']])
# Add tags print('Downloaded song: ' + song)
logging.debug('Adding tags') # Add tags
resultfile = eyed3.load(str(destpath / filename) + '.mp3') logging.debug('Adding tags')
resultfile.tag.album_artist = albumartist resultfile = eyed3.load(str(destpath / filename) + '.mp3')
resultfile.tag.artist = songcontent['artist'] resultfile.tag.album_artist = albumartist
resultfile.tag.album = album resultfile.tag.artist = songcontent['artist']
resultfile.tag.title = song resultfile.tag.album = album
resultfile.tag.track_num = songcontent['track'] resultfile.tag.title = song
resultfile.tag.save() resultfile.tag.track_num = songcontent['track']
resultfile.tag.save()
except (KeyboardInterrupt, EOFError):
logging.debug('Interrupt received, exiting')
class BadWitch: class BadWitch:
# Our program # Our program