Fixed some other issues

This commit is contained in:
RemixDev 2020-03-01 13:14:37 +01:00
parent 3c69c37c11
commit 3669505e10
1 changed files with 72 additions and 58 deletions

View File

@ -3,7 +3,7 @@ from deemix.api.deezer import Deezer, APIError
from deemix.utils.taggers import tagID3, tagFLAC from deemix.utils.taggers import tagID3, tagFLAC
from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist
import os.path import os.path
from os import makedirs from os import makedirs, remove
from requests import get from requests import get
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from tempfile import gettempdir from tempfile import gettempdir
@ -24,6 +24,20 @@ extensions = {
13: '.mp4' 13: '.mp4'
} }
def downloadImage(url, path):
if not os.path.isfile(path):
with open(path, 'wb') as f:
try:
f.write(get(url).content)
return path
except HTTPError:
print("Couldn't download Image")
remove(path)
return None
else:
return path
def getPreferredBitrate(filesize, bitrate): def getPreferredBitrate(filesize, bitrate):
bitrateFound = False bitrateFound = False
selectedFormat = 0 selectedFormat = 0
@ -162,17 +176,18 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No
try: try:
if not albumAPI: if not albumAPI:
albumAPI = dz.get_album(track['album']['id']) albumAPI = dz.get_album(track['album']['id'])
print(albumAPI['artist']['picture_small'])
track['album']['artist'] = { track['album']['artist'] = {
'id': albumAPI['artist']['id'], 'id': albumAPI['artist']['id'],
'name': albumAPI['artist']['name'], 'name': albumAPI['artist']['name'],
'pic': albumAPI['artist']['picture_small'][46:-24] 'pic': albumAPI['artist']['picture_small'][albumAPI['artist']['picture_small'].find('artist/')+7:-24]
} }
track['album']['trackTotal'] = albumAPI['nb_tracks'] track['album']['trackTotal'] = albumAPI['nb_tracks']
track['album']['recordType'] = albumAPI['record_type'] track['album']['recordType'] = albumAPI['record_type']
track['album']['barcode'] = albumAPI['upc'] if 'upc' in albumAPI else "Unknown" track['album']['barcode'] = albumAPI['upc'] if 'upc' in albumAPI else "Unknown"
track['album']['label'] = albumAPI['label'] if 'label' in albumAPI else "Unknown" track['album']['label'] = albumAPI['label'] if 'label' in albumAPI else "Unknown"
if not 'pic' in track['album']: if not 'pic' in track['album']:
track['album']['pic'] = albumAPI['cover_small'][43:-24] track['album']['pic'] = albumAPI['cover_small'][albumAPI['cover_small'].find('cover/')+6:-24]
if 'release_date' in albumAPI: if 'release_date' in albumAPI:
track['album']['date'] = { track['album']['date'] = {
'day': albumAPI["release_date"][8:10], 'day': albumAPI["release_date"][8:10],
@ -193,7 +208,7 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No
'name': albumAPI_gw['ART_NAME'] 'name': albumAPI_gw['ART_NAME']
} }
artistAPI = dz.get_artist(track['album']['artist']['id']) artistAPI = dz.get_artist(track['album']['artist']['id'])
track['album']['artist']['pic'] = artistAPI['picture_small'][44:-24] track['album']['artist']['pic'] = artistAPI['picture_small'][artistAPI['picture_small'].find('artist/')+7:-24]
track['album']['trackTotal'] = albumAPI_gw['NUMBER_TRACK'] track['album']['trackTotal'] = albumAPI_gw['NUMBER_TRACK']
track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK'] track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK']
track['album']['recordType'] = "Album" track['album']['recordType'] = "Album"
@ -241,7 +256,7 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No
return track return track
def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None): def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None):
result = [None, None] result = {}
# Get the metadata # Get the metadata
if extraTrack: if extraTrack:
track = extraTrack track = extraTrack
@ -267,12 +282,7 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
# Download and cache coverart # Download and cache coverart
track['album']['picPath'] = os.path.join(TEMPDIR, f"alb{track['album']['id']}_{settings['embeddedArtworkSize']}.{'png' if settings['PNGcovers'] else 'jpg'}") track['album']['picPath'] = os.path.join(TEMPDIR, f"alb{track['album']['id']}_{settings['embeddedArtworkSize']}.{'png' if settings['PNGcovers'] else 'jpg'}")
if not os.path.isfile(track['album']['picPath']): track['album']['picPath'] = downloadImage(track['album']['picUrl'], track['album']['picPath'])
with open(track['album']['picPath'], 'wb') as f:
try:
f.write(get(track['album']['picUrl']).content)
except HTTPError:
track['album']['picPath'] = None
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['selectedFormat']])
@ -284,49 +294,39 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
# Save local album art # Save local album art
if coverPath: if coverPath:
track['album']['picUrlLocal'] = track['album']['picUrl'].replace(f"{settings['embeddedArtworkSize']}x{settings['embeddedArtworkSize']}", f"{settings['localArtworkSize']}x{settings['localArtworkSize']}") result['albumURL'] = track['album']['picUrl'].replace(f"{settings['embeddedArtworkSize']}x{settings['embeddedArtworkSize']}", f"{settings['localArtworkSize']}x{settings['localArtworkSize']}")
track['album']['picPathLocal'] = os.path.join(coverPath, f"{settingsRegexAlbum(settings['coverImageTemplate'], track['album'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}") result['albumPath'] = os.path.join(coverPath, f"{settingsRegexAlbum(settings['coverImageTemplate'], track['album'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}")
if not os.path.isfile(track['album']['picPathLocal']):
with open(track['album']['picPathLocal'], 'wb') as f:
try:
f.write(get(track['album']['picUrlLocal']).content)
except HTTPError:
track['album']['picPathLocal'] = None
# Save artist art # Save artist art
if artistPath: if artistPath:
track['album']['artist']['picUrl'] = "https://e-cdns-images.dzcdn.net/images/artist/{}/{}x{}-000000-80-0-0.{}".format(track['album']['artist']['pic'], settings['localArtworkSize'], settings['localArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg') result['artistURL'] = "https://e-cdns-images.dzcdn.net/images/artist/{}/{}x{}-000000-80-0-0.{}".format(track['album']['artist']['pic'], settings['localArtworkSize'], settings['localArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg')
track['album']['artist']['picPathLocal'] = os.path.join(artistPath, f"{settingsRegexArtist(settings['artistImageTemplate'], track['album']['artist'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}") result['artistPath'] = os.path.join(artistPath, f"{settingsRegexArtist(settings['artistImageTemplate'], track['album']['artist'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}")
if not os.path.isfile(track['album']['artist']['picPathLocal']):
with open(track['album']['artist']['picPathLocal'], 'wb') as f:
try:
f.write(get(track['album']['artist']['picUrl']).content)
except HTTPError:
track['album']['artist']['picPathLocal'] = None
# Data for m3u file # Data for m3u file
if extrasPath: if extrasPath:
playlistPosition = writepath[len(extrasPath):] result['extrasPath'] = extrasPath
result = [playlistPosition, extrasPath] result['playlistPosition'] = writepath[len(extrasPath):]
track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat']) track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat'])
with open(writepath, 'wb') as stream: try:
try: with open(writepath, 'wb') as stream:
dz.stream_track(track['id'], track['downloadUrl'], stream) dz.stream_track(track['id'], track['downloadUrl'], stream)
except HTTPError: except HTTPError:
if track['selectedFormat'] == 9: remove(writePath)
print("Track not available in flac, trying mp3") if track['selectedFormat'] == 9:
track['filesize']['flac'] = 0 print("Track not available in flac, trying mp3")
return downloadTrackObj(trackAPI, settings, extraTrack=track) track['filesize']['flac'] = 0
elif track['fallbackId'] != 0: return downloadTrackObj(trackAPI, settings, extraTrack=track)
print("Track not available, using fallback id") elif track['fallbackId'] != 0:
trackNew = dz.get_track_gw(track['fallbackId']) print("Track not available, using fallback id")
if not 'MD5_ORIGIN' in trackNew: trackNew = dz.get_track_gw(track['fallbackId'])
trackNew['MD5_ORIGIN'] = dz.get_track_md5(trackNew['SNG_ID']) if not 'MD5_ORIGIN' in trackNew:
track = parseEssentialTrackData(track, trackNew) trackNew['MD5_ORIGIN'] = dz.get_track_md5(trackNew['SNG_ID'])
return downloadTrackObj(trackNew, settings, extraTrack=track) track = parseEssentialTrackData(track, trackNew)
else: return downloadTrackObj(trackNew, settings, extraTrack=track)
print("ERROR: Track not available on deezer's servers!") else:
return False print("ERROR: Track not available on deezer's servers!")
return False
if track['selectedFormat'] in [3, 1, 8]: if track['selectedFormat'] in [3, 1, 8]:
tagID3(writepath, track, settings['tags'], settings['saveID3v1'], settings['useNullSeparator']) tagID3(writepath, track, settings['tags'], settings['saveID3v1'], settings['useNullSeparator'])
elif track['selectedFormat'] == 9: elif track['selectedFormat'] == 9:
@ -361,13 +361,20 @@ def download_album(id, settings, overwriteBitrate=False):
trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate'] trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate']
playlist[pos-1] = executor.submit(downloadTrackObj, trackAPI, settings, overwriteBitrate) playlist[pos-1] = executor.submit(downloadTrackObj, trackAPI, settings, overwriteBitrate)
executor.shutdown(wait=True) executor.shutdown(wait=True)
extrasPath = None
for index in range(len(playlist)):
result = playlist[index].result()
if not extrasPath:
extrasPath = result['extrasPath']
if settings['saveArtwork'] and result['albumPath']:
print(result['albumURL'])
downloadImage(result['albumURL'], result['albumPath'])
if settings['saveArtworkArtist'] and result['artistPath']:
print(result['artistURL'])
downloadImage(result['artistURL'], result['artistPath'])
playlist[index] = result['playlistPosition']
if settings['createM3U8File']: if settings['createM3U8File']:
extrasPath = "" if extrasPath != "" or extrasPath != None:
for index in range(len(playlist)):
if extrasPath == "":
extrasPath = playlist[index].result()[1]
playlist[index] = playlist[index].result()[0]
if extrasPath:
with open(os.path.join(extrasPath, 'playlist.m3u8'), 'w') as f: with open(os.path.join(extrasPath, 'playlist.m3u8'), 'w') as f:
for line in playlist: for line in playlist:
f.write(line+"\n") f.write(line+"\n")
@ -384,13 +391,20 @@ def download_playlist(id, settings, overwriteBitrate=False):
trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate'] trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
playlist[pos-1] = executor.submit(downloadTrackObj, trackAPI, settings, overwriteBitrate) playlist[pos-1] = executor.submit(downloadTrackObj, trackAPI, settings, overwriteBitrate)
executor.shutdown(wait=True) executor.shutdown(wait=True)
extrasPath = None
for index in range(len(playlist)):
result = playlist[index].result()
if not extrasPath:
extrasPath = result['extrasPath']
if settings['saveArtwork'] and result['albumPath']:
print(result['albumURL'])
downloadImage(result['albumURL'], result['albumPath'])
if settings['saveArtworkArtist'] and result['artistPath']:
print(result['artistURL'])
downloadImage(result['artistURL'], result['artistPath'])
playlist[index] = result['playlistPosition']
if settings['createM3U8File']: if settings['createM3U8File']:
extrasPath = "" if extrasPath != "" or extrasPath != None:
for index in range(len(playlist)):
if extrasPath == "":
extrasPath = playlist[index].result()[1]
playlist[index] = playlist[index].result()[0]
if extrasPath:
with open(os.path.join(extrasPath, 'playlist.m3u8'), 'w') as f: with open(os.path.join(extrasPath, 'playlist.m3u8'), 'w') as f:
for line in playlist: for line in playlist:
f.write(line+"\n") f.write(line+"\n")