From 203f0c9b7e7a01f71112db30356c71c77b415d9a Mon Sep 17 00:00:00 2001 From: uh_wot <3631986-uh_wot@users.noreply.gitlab.com> Date: Mon, 4 May 2020 16:36:49 +0200 Subject: [PATCH] ugly fix for deezer patching their API --- deemix/app/downloader.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index eaafea0..021806a 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -100,10 +100,12 @@ def formatDate(date, template): return template -def getPreferredBitrate(filesize, bitrate, fallback=True): +def getPreferredBitrate(dz, track, bitrate, fallback=True): if not fallback: formats = {9: 'flac', 3: 'mp3_320', 1: 'mp3_128', 15: '360_hq', 14: '360_mq', 13: '360_lq'} - if filesize[formats[int(bitrate)]] > 0: + request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], bitrate)) + filesize = request.headers["Content-Length"] + if filesize != "0": return (int(bitrate), filesize[formats[int(bitrate)]]) else: return (-100, 0) @@ -112,18 +114,26 @@ def getPreferredBitrate(filesize, bitrate, fallback=True): selectedFormat = -200 selectedFilesize = 0 for format, formatNum in formats.items(): - if formatNum <= int(bitrate) and filesize[format] > 0: + if formatNum <= int(bitrate): + request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], formatNum)) + filesize = request.headers["Content-Length"] + if filesize == "0": + continue selectedFormat = formatNum - selectedFilesize = filesize[format] + selectedFilesize = int(filesize) break else: formats = {'flac': 9, 'mp3_320': 3, 'mp3_128': 1} selectedFormat = 8 - selectedFilesize = filesize['default'] + selectedFilesize = track['filesize']['default'] for format, formatNum in formats.items(): - if formatNum <= int(bitrate) and filesize[format] > 0: + if formatNum <= int(bitrate): + request = get(dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], formatNum)) + filesize = request.headers["Content-Length"] + if filesize == "0": + continue selectedFormat = formatNum - selectedFilesize = filesize[format] + selectedFilesize = int(filesize) break return (selectedFormat, selectedFilesize) @@ -456,7 +466,7 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None return result # Get the selected bitrate - (format, filesize) = getPreferredBitrate(track['filesize'], bitrate, settings['fallbackBitrate']) + (format, filesize) = getPreferredBitrate(dz, track, bitrate, settings['fallbackBitrate']) if format == -100: print("ERROR: Track not found at desired bitrate. Enable fallback to lower bitrates to fix this issue.") result['error'] = {