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