cleaned up getPreferredBitrate function

This commit is contained in:
uh_wot 2020-05-11 08:48:45 +02:00
parent c21e5ffe27
commit 9cfb5bbd43
1 changed files with 27 additions and 35 deletions

View File

@ -105,39 +105,31 @@ def formatDate(date, template):
def getPreferredBitrate(dz, track, bitrate, fallback=True): def getPreferredBitrate(dz, track, bitrate, fallback=True):
formatsnon360 = [9, 3, 1] # flac, mp3_320, mp3_128
formats360 = [15, 14, 13] # 360_hq, 360_mq, 360_lq
if not fallback: if not fallback:
formats = {9: 'flac', 3: 'mp3_320', 1: 'mp3_128', 15: '360_hq', 14: '360_mq', 13: '360_lq'} errorNum = -100
request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], bitrate)) formats = formats360
try: formats.extend(formatsnon360)
request.raise_for_status() elif int(bitrate) in formats360:
except HTTPError: errorNum = -200
return -100 formats = formats360
return int(bitrate)
if int(bitrate) in [13, 14, 15]:
formats = {'360_hq': 15, '360_mq': 14, '360_lq': 13}
selectedFormat = -200
for format, formatNum in formats.items():
if formatNum <= int(bitrate):
request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], formatNum))
try:
request.raise_for_status()
except HTTPError:
continue
selectedFormat = formatNum
break
else: else:
formats = {'flac': 9, 'mp3_320': 3, 'mp3_128': 1} errorNum = 8
selectedFormat = 8 formats = formatsnon360
for format, formatNum in formats.items():
if formatNum <= int(bitrate): for formatNum in formats:
request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], formatNum)) if formatNum <= int(bitrate):
try: request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], formatNum))
request.raise_for_status() try:
except HTTPError: request.raise_for_status()
except HTTPError: # if the format is not available, Deezer returns a 403 error
if fallback:
continue continue
selectedFormat = formatNum else:
break return errorNum
return selectedFormat return formatNum
return errorNum # fallback is enabled and loop went through all formats
def parseEssentialTrackData(track, trackAPI): def parseEssentialTrackData(track, trackAPI):
@ -493,7 +485,7 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None
interface.send("updateQueue", {'uuid': queueItem['uuid'], 'failed': True, 'data': track, interface.send("updateQueue", {'uuid': queueItem['uuid'], 'failed': True, 'data': track,
'error': "Track is not available in Reality Audio 360."}) 'error': "Track is not available in Reality Audio 360."})
return result return result
track['selectedFormat'] = format track['errorNum'] = format
track['dateString'] = formatDate(track['date'], settings['dateFormat']) track['dateString'] = formatDate(track['date'], settings['dateFormat'])
if settings['tags']['savePlaylistAsCompilation'] and "_EXTRA_PLAYLIST" in trackAPI: if settings['tags']['savePlaylistAsCompilation'] and "_EXTRA_PLAYLIST" in trackAPI:
if 'dzcdn.net' in trackAPI["_EXTRA_PLAYLIST"]['picture_small']: if 'dzcdn.net' in trackAPI["_EXTRA_PLAYLIST"]['picture_small']:
@ -587,7 +579,7 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None
filepath = os.path.join(filepath, tempPath) filepath = os.path.join(filepath, tempPath)
filename = filename[filename.rfind(os.path.sep) + len(os.path.sep):] filename = filename[filename.rfind(os.path.sep) + len(os.path.sep):]
makedirs(filepath, exist_ok=True) makedirs(filepath, exist_ok=True)
writepath = os.path.join(filepath, filename + extensions[track['selectedFormat']]) writepath = os.path.join(filepath, filename + extensions[track['errorNum']])
# Save lyrics in lrc file # Save lyrics in lrc file
if settings['syncedLyrics'] and 'sync' in track['lyrics']: if settings['syncedLyrics'] and 'sync' in track['lyrics']:
@ -616,7 +608,7 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None
result['playlistPosition'] = writepath[len(extrasPath):] result['playlistPosition'] = writepath[len(extrasPath):]
track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'],
track['selectedFormat']) track['errorNum'])
logger.info(f"[{track['mainArtist']['name']} - {track['title']}] Downloading the track") logger.info(f"[{track['mainArtist']['name']} - {track['title']}] Downloading the track")
try: try:
with open(writepath, 'wb') as stream: with open(writepath, 'wb') as stream:
@ -669,9 +661,9 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None
'error': "Track not available on deezer's servers!"}) 'error': "Track not available on deezer's servers!"})
return result return result
logger.info(f"[{track['mainArtist']['name']} - {track['title']}] Applying tags to the track") logger.info(f"[{track['mainArtist']['name']} - {track['title']}] Applying tags to the track")
if track['selectedFormat'] in [3, 1, 8]: if track['errorNum'] in [3, 1, 8]:
tagID3(writepath, track, settings['tags']) tagID3(writepath, track, settings['tags'])
elif track['selectedFormat'] == 9: elif track['errorNum'] == 9:
tagFLAC(writepath, track, settings['tags']) tagFLAC(writepath, track, settings['tags'])
if 'searched' in track: if 'searched' in track:
result['searched'] = f'{track["mainArtist"]["name"]} - {track["title"]}' result['searched'] = f'{track["mainArtist"]["name"]} - {track["title"]}'