deemix-py/deemix/app/downloader.py

339 lines
13 KiB
Python
Raw Normal View History

2020-02-20 14:42:12 +00:00
#!/usr/bin/env python3
from deemix.api.deezer import Deezer, APIError
from deemix.utils.taggers import tagID3, tagFLAC
2020-02-24 17:36:11 +00:00
from deemix.utils.pathtemplates import generateFilename, generateFilepath
2020-02-20 14:42:12 +00:00
import os.path
2020-02-24 17:36:11 +00:00
from os import makedirs
2020-02-27 17:37:40 +00:00
from urllib.request import urlopen
2020-02-22 13:38:01 +00:00
from urllib.error import HTTPError
2020-02-27 17:37:40 +00:00
from tempfile import gettempdir
2020-02-20 14:42:12 +00:00
dz = Deezer()
2020-02-27 17:37:40 +00:00
TEMPDIR = os.path.join(gettempdir(), 'deezloader-imgs')
if not os.path.isdir(TEMPDIR):
makedirs(TEMPDIR)
2020-02-20 14:42:12 +00:00
extensions = {
9: '.flac',
3: '.mp3',
1: '.mp3',
8: '.mp3',
15: '.mp4',
14: '.mp4',
13: '.mp4'
}
2020-02-24 17:36:11 +00:00
def getPreferredBitrare(filesize, bitrate):
bitrateFound = False;
selectedFormat = 0
selectedFilesize = 0
if int(bitrate) == 9:
selectedFormat = 9
selectedFilesize = filesize['flac']
if filesize['flac'] > 0:
bitrateFound = True
else:
bitrateFound = False
bitrate = 3
if int(bitrate) == 3:
selectedFormat = 3
selectedFilesize = filesize['mp3_320']
if filesize['mp3_320'] > 0:
bitrateFound = True
else:
bitrateFound = False
bitrate = 1
if int(bitrate) == 1:
selectedFormat = 3
selectedFilesize = filesize['mp3_320']
if filesize['mp3_320'] > 0:
bitrateFound = True
else:
bitrateFound = False
if not bitrateFound:
selectedFormat = 8
selectedFilesize = filesize['default']
return (selectedFormat, selectedFilesize)
2020-02-22 13:38:01 +00:00
def parseEssentialTrackData(track, trackAPI):
track['id'] = trackAPI['SNG_ID']
track['duration'] = trackAPI['DURATION']
track['MD5'] = trackAPI['MD5_ORIGIN']
track['mediaVersion'] = trackAPI['MEDIA_VERSION']
if 'FALLBACK' in trackAPI:
track['fallbackId'] = trackAPI['FALLBACK']['SNG_ID']
else:
track['fallbackId'] = 0
track['filesize'] = {}
track['filesize']['default'] = int(trackAPI['FILESIZE']) if 'FILESIZE' in trackAPI else None
track['filesize']['mp3_128'] = int(trackAPI['FILESIZE_MP3_128']) if 'FILESIZE_MP3_128' in trackAPI else None
track['filesize']['mp3_320'] = int(trackAPI['FILESIZE_MP3_320']) if 'FILESIZE_MP3_320' in trackAPI else None
track['filesize']['flac'] = int(trackAPI['FILESIZE_FLAC']) if 'FILESIZE_FLAC' in trackAPI else None
track['filesize']['mp4_ra1'] = int(trackAPI['FILESIZE_MP4_RA1']) if 'FILESIZE_MP4_RA1' in trackAPI else None
track['filesize']['mp4_ra2'] = int(trackAPI['FILESIZE_MP4_RA2']) if 'FILESIZE_MP4_RA2' in trackAPI else None
track['filesize']['mp4_ra3'] = int(trackAPI['FILESIZE_MP4_RA3']) if 'FILESIZE_MP4_RA3' in trackAPI else None
return track
2020-02-27 16:34:49 +00:00
def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = None):
if not 'MD5_ORIGIN' in trackAPI_gw:
trackAPI_gw['MD5_ORIGIN'] = dz.get_track_md5(trackAPI_gw['SNG_ID'])
2020-02-20 14:42:12 +00:00
track = {}
2020-02-27 16:34:49 +00:00
track['title'] = trackAPI_gw['SNG_TITLE']
if trackAPI_gw['VERSION']:
track['title'] += " " + trackAPI_gw['VERSION']
2020-02-22 13:38:01 +00:00
2020-02-27 16:34:49 +00:00
track = parseEssentialTrackData(track, trackAPI_gw)
2020-02-20 14:42:12 +00:00
if int(track['id']) < 0:
2020-02-27 16:34:49 +00:00
track['filesize'] = trackAPI_gw['FILESIZE']
2020-02-20 14:42:12 +00:00
track['album'] = {}
track['album']['id'] = 0
2020-02-27 16:34:49 +00:00
track['album']['title'] = trackAPI_gw['ALB_TITLE']
if 'ALB_PICTURE' in trackAPI_gw:
track['album']['pic'] = trackAPI_gw['ALB_PICTURE']
2020-02-20 14:42:12 +00:00
track['mainArtist'] = {}
track['mainArtist']['id'] = 0
2020-02-27 16:34:49 +00:00
track['mainArtist']['name'] = trackAPI_gw['ART_NAME']
track['artists'] = [trackAPI_gw['ART_NAME']]
2020-02-24 17:36:11 +00:00
track['aritst'] = {
2020-02-27 16:34:49 +00:00
'Main': [trackAPI_gw['ART_NAME']]
2020-02-24 17:36:11 +00:00
}
2020-02-20 14:42:12 +00:00
track['date'] = {
'day': 0,
'month': 0,
'year': 0
}
track['localTrack'] = True
return track
2020-02-27 16:34:49 +00:00
if 'DISK_NUMBER' in trackAPI_gw:
track['discNumber'] = trackAPI_gw['DISK_NUMBER']
if 'EXPLICIT_LYRICS' in trackAPI_gw:
track['explicit'] = trackAPI_gw['EXPLICIT_LYRICS'] != "0"
if 'COPYRIGHT' in trackAPI_gw:
track['copyright'] = trackAPI_gw['COPYRIGHT']
track['replayGain'] = "{0:.2f} dB".format((float(trackAPI_gw['GAIN']) + 18.4) * -1) if 'GAIN' in trackAPI_gw else None
track['ISRC'] = trackAPI_gw['ISRC']
track['trackNumber'] = trackAPI_gw['TRACK_NUMBER']
track['contributors'] = trackAPI_gw['SNG_CONTRIBUTORS']
if 'POSITION' in trackAPI_gw:
track['position'] = trackAPI_gw['POSITION']
2020-02-20 14:42:12 +00:00
track['lyrics'] = {}
2020-02-27 16:34:49 +00:00
if 'LYRICS_ID' in trackAPI_gw:
track['lyrics']['id'] = trackAPI_gw['LYRICS_ID']
2020-02-29 20:22:44 +00:00
if not "LYRICS" in trackAPI_gw and int(track['lyrics']['id']) != 0:
trackAPI_gw["LYRICS"] = dz.get_lyrics_gw(track['id'])
if int(track['lyrics']['id']) != 0:
2020-02-27 16:34:49 +00:00
if "LYRICS_TEXT" in trackAPI_gw["LYRICS"]:
track['lyrics']['unsync'] = trackAPI_gw["LYRICS"]["LYRICS_TEXT"]
if "LYRICS_SYNC_JSON" in trackAPI_gw["LYRICS"]:
2020-02-20 14:42:12 +00:00
track['lyrics']['sync'] = ""
2020-02-27 16:34:49 +00:00
for i in range(len(trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"])):
if "lrc_timestamp" in trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"][i]:
track['lyrics']['sync'] += trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"][i]["lrc_timestamp"] + \
trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"][i]["line"] + "\r\n"
elif i + 1 < len(trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"]):
track['lyrics']['sync'] += trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"][i + 1]["lrc_timestamp"] + \
trackAPI_gw["LYRICS"]["LYRICS_SYNC_JSON"][i]["line"] + "\r\n"
2020-02-20 14:42:12 +00:00
track['mainArtist'] = {}
2020-02-27 16:34:49 +00:00
track['mainArtist']['id'] = trackAPI_gw['ART_ID']
track['mainArtist']['name'] = trackAPI_gw['ART_NAME']
if 'ART_PICTURE' in trackAPI_gw:
track['mainArtist']['pic'] = trackAPI_gw['ART_PICTURE']
2020-02-20 14:42:12 +00:00
2020-02-27 16:34:49 +00:00
if 'PHYSICAL_RELEASE_DATE' in trackAPI_gw:
2020-02-20 14:42:12 +00:00
track['date'] = {
2020-02-27 16:34:49 +00:00
'day': trackAPI_gw["PHYSICAL_RELEASE_DATE"][8:10],
'month': trackAPI_gw["PHYSICAL_RELEASE_DATE"][5:7],
'year': trackAPI_gw["PHYSICAL_RELEASE_DATE"][0:4]
2020-02-20 14:42:12 +00:00
}
track['album'] = {}
2020-02-27 16:34:49 +00:00
track['album']['id'] = trackAPI_gw['ALB_ID']
track['album']['title'] = trackAPI_gw['ALB_TITLE']
if 'ALB_PICTURE' in trackAPI_gw:
track['album']['pic'] = trackAPI_gw['ALB_PICTURE']
2020-02-20 14:42:12 +00:00
try:
2020-02-27 16:34:49 +00:00
if not albumAPI:
2020-02-20 14:42:12 +00:00
albumAPI = dz.get_album(track['album']['id'])
track['album']['artist'] = {
'id': albumAPI['artist']['id'],
'name': albumAPI['artist']['name'],
'pic': albumAPI['artist']['picture_small'][46:-24]
}
track['album']['trackTotal'] = albumAPI['nb_tracks']
track['album']['recordType'] = albumAPI['record_type']
2020-02-22 13:38:01 +00:00
track['album']['barcode'] = albumAPI['upc'] if 'upc' in albumAPI else "Unknown"
track['album']['label'] = albumAPI['label'] if 'label' in albumAPI else "Unknown"
2020-02-20 14:42:12 +00:00
if not 'pic' in track['album']:
track['album']['pic'] = albumAPI['cover_small'][43:-24]
2020-02-24 17:36:11 +00:00
if 'release_date' in albumAPI:
track['album']['date'] = {
2020-02-20 14:42:12 +00:00
'day': albumAPI["release_date"][8:10],
'month': albumAPI["release_date"][5:7],
'year': albumAPI["release_date"][0:4]
}
track['album']['discTotal'] = albumAPI['nb_disk'] if 'nb_disk' in albumAPI else None
track['copyright'] = albumAPI['copyright'] if 'copyright' in albumAPI else None
track['album']['genre'] = []
if 'genres' in albumAPI and 'data' in albumAPI['genres'] and len(albumAPI['genres']['data']) > 0:
for genre in albumAPI['genres']['data']:
track['album']['genre'].append(genre['name'])
except APIError:
2020-02-27 16:34:49 +00:00
if not albumAPI_gw:
albumAPI_gw = dz.get_album_gw(track['album']['id'])
2020-02-20 14:42:12 +00:00
track['album']['artist'] = {
2020-02-27 16:34:49 +00:00
'id': albumAPI_gw['ART_ID'],
'name': albumAPI_gw['ART_NAME']
2020-02-20 14:42:12 +00:00
}
2020-02-27 16:34:49 +00:00
track['album']['trackTotal'] = albumAPI_gw['NUMBER_TRACK']
track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK']
track['album']['recordType'] = "Album"
2020-02-22 13:38:01 +00:00
track['album']['barcode'] = "Unknown"
2020-02-27 16:34:49 +00:00
track['album']['label'] = albumAPI_gw['LABEL_NAME'] if 'LABEL_NAME' in albumAPI_gw else "Unknown"
2020-02-20 14:42:12 +00:00
if not 'pic' in track['album']:
2020-02-27 16:34:49 +00:00
track['album']['pic'] = albumAPI_gw['ALB_PICTURE']
if 'PHYSICAL_RELEASE_DATE' in albumAPI_gw:
2020-02-24 17:36:11 +00:00
track['album']['date'] = {
2020-02-27 16:34:49 +00:00
'day': albumAPI_gw["PHYSICAL_RELEASE_DATE"][8:10],
'month': albumAPI_gw["PHYSICAL_RELEASE_DATE"][5:7],
'year': albumAPI_gw["PHYSICAL_RELEASE_DATE"][0:4]
2020-02-20 14:42:12 +00:00
}
track['album']['genre'] = []
2020-02-24 17:36:11 +00:00
if 'date' in track['album']:
track['date'] = track['album']['date']
2020-02-27 16:34:49 +00:00
if not trackAPI:
trackAPI = dz.get_track(track['id'])
track['bpm'] = trackAPI['bpm']
2020-02-20 14:42:12 +00:00
if not 'replayGain' in track:
2020-02-27 16:34:49 +00:00
track['replayGain'] = "{0:.2f} dB".format((float(trackAPI['gain']) + 18.4) * -1) if 'gain' in trackAPI else ""
2020-02-20 14:42:12 +00:00
if not 'explicit' in track:
2020-02-27 16:34:49 +00:00
track['explicit'] = trackAPI['explicit_lyrics']
2020-02-22 13:38:01 +00:00
if not 'discNumber' in track:
2020-02-27 16:34:49 +00:00
track['discNumber'] = trackAPI['disk_number']
2020-02-20 14:42:12 +00:00
track['artist'] = {}
track['artists'] = []
2020-02-27 16:34:49 +00:00
for artist in trackAPI['contributors']:
if artist['id'] != 5080:
track['artists'].append(artist['name'])
if not artist['role'] in track['artist']:
track['artist'][artist['role']] = []
track['artist'][artist['role']].append(artist['name'])
2020-02-20 14:42:12 +00:00
2020-02-22 13:38:01 +00:00
if not 'discTotal' in track['album'] or not track['album']['discTotal']:
2020-02-27 16:34:49 +00:00
if not albumAPI_gw:
albumAPI_gw = dz.get_album_gw(track['album']['id'])
track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK']
2020-02-22 13:38:01 +00:00
if not 'copyright' in track or not track['copyright']:
2020-02-27 16:34:49 +00:00
if not albumAPI_gw:
albumAPI_gw = dz.get_album_gw(track['album']['id'])
track['copyright'] = albumAPI_gw['COPYRIGHT']
2020-02-20 14:42:12 +00:00
return track
2020-02-22 13:38:01 +00:00
def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None):
2020-02-20 14:42:12 +00:00
# Get the metadata
2020-02-22 13:38:01 +00:00
if extraTrack:
track = extraTrack
else:
2020-02-27 16:34:49 +00:00
track = getTrackData(
trackAPI_gw = trackAPI,
trackAPI = trackAPI['_EXTRA_TRACK'] if '_EXTRA_TRACK' in trackAPI else None,
albumAPI = trackAPI['_EXTRA_ALBUM'] if '_EXTRA_ALBUM' in trackAPI else None
)
2020-02-20 14:42:12 +00:00
print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title']))
# Get the selected bitrate
2020-02-27 17:37:40 +00:00
bitrate = overwriteBitrate if overwriteBitrate else settings['maxBitrate']
2020-02-24 17:36:11 +00:00
(format, filesize) = getPreferredBitrare(track['filesize'], bitrate)
track['selectedFormat'] = format
track['selectedFilesize'] = filesize
track['album']['bitrate'] = format
2020-02-27 17:46:32 +00:00
track['album']['picUrl'] = "http://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.{}".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg')
2020-02-27 17:37:40 +00:00
# Generate filename and filepath from metadata
2020-02-29 20:22:44 +00:00
filename = generateFilename(track, trackAPI, settings)
2020-02-24 17:36:11 +00:00
(filepath, artistPath, coverPath, extrasPath) = generateFilepath(track, trackAPI, settings)
2020-02-27 17:37:40 +00:00
# Download and cache coverart
2020-02-27 17:46:32 +00:00
track['album']['picPath'] = os.path.join(TEMPDIR, f"alb{track['album']['id']}_{settings['embeddedArtworkSize']}.{'png' if settings['PNGcovers'] else 'jpg'}")
2020-02-27 17:37:40 +00:00
if not os.path.isfile(track['album']['picPath']):
with open(track['album']['picPath'], 'wb') as f:
try:
f.write(urlopen(track['album']['picUrl']).read())
except HTTPError:
track['album']['picPath'] = None
2020-02-24 17:36:11 +00:00
makedirs(filepath, exist_ok=True)
2020-02-29 20:22:44 +00:00
writepath = os.path.join(filepath, filename + extensions[track['selectedFormat']])
# Save lyrics in lrc file
if settings['syncedLyrics'] and 'sync' in track['lyrics']:
with open(os.path.join(filepath, filename + '.lrc'), 'w') as f:
f.write(track['lyrics']['sync'])
2020-02-20 14:42:12 +00:00
2020-02-24 17:36:11 +00:00
track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat'])
2020-02-20 14:42:12 +00:00
with open(writepath, 'wb') as stream:
2020-02-22 13:38:01 +00:00
try:
dz.stream_track(track['id'], track['downloadUrl'], stream)
except HTTPError:
if track['selectedFormat'] == 9:
print("Track not available in flac, trying mp3")
track['filesize']['flac'] = 0
return downloadTrackObj(trackAPI, settings, extraTrack=track)
elif track['fallbackId'] != 0:
print("Track not available, using fallback id")
trackNew = dz.get_track_gw(track['fallbackId'])
if not 'MD5_ORIGIN' in trackNew:
trackNew['MD5_ORIGIN'] = dz.get_track_md5(trackNew['SNG_ID'])
track = parseEssentialTrackData(track, trackNew)
return downloadTrackObj(trackNew, settings, extraTrack=track)
else:
print("ERROR: Track not available on deezer's servers!")
return False
2020-02-20 14:42:12 +00:00
if track['selectedFormat'] in [3, 1, 8]:
tagID3(writepath, track, settings['tags'])
2020-02-20 14:42:12 +00:00
elif track['selectedFormat'] == 9:
tagFLAC(writepath, track, settings['tags'])
2020-02-22 13:38:01 +00:00
print("Done!")
return True
2020-02-20 14:42:12 +00:00
def download_track(id, settings, overwriteBitrate=False):
trackAPI = dz.get_track_gw(id)
2020-02-24 17:36:11 +00:00
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
trackAPI['SINGLE_TRACK'] = True
2020-02-20 14:42:12 +00:00
downloadTrackObj(trackAPI, settings, overwriteBitrate)
def download_album(id, settings, overwriteBitrate=False):
albumAPI = dz.get_album(id)
2020-02-27 16:34:49 +00:00
albumAPI_gw = dz.get_album_gw(id)
albumAPI['nb_disk'] = albumAPI_gw['NUMBER_DISK']
albumAPI['copyright'] = albumAPI_gw['COPYRIGHT']
2020-02-20 14:42:12 +00:00
if albumAPI['nb_tracks'] == 1:
trackAPI = dz.get_track_gw(albumAPI['tracks']['data'][0]['id'])
2020-02-27 16:34:49 +00:00
trackAPI['_EXTRA_ALBUM'] = albumAPI
2020-02-24 17:36:11 +00:00
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
trackAPI['SINGLE_TRACK'] = True
2020-02-20 14:42:12 +00:00
downloadTrackObj(trackAPI, settings, overwriteBitrate)
else:
tracksArray = dz.get_album_tracks_gw(id)
for trackAPI in tracksArray:
2020-02-27 16:34:49 +00:00
trackAPI['_EXTRA_ALBUM'] = albumAPI
2020-02-24 17:36:11 +00:00
trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate']
2020-02-20 14:42:12 +00:00
downloadTrackObj(trackAPI, settings, overwriteBitrate)
2020-02-22 13:38:01 +00:00
def download_playlist(id, settings, overwriteBitrate=False):
playlistAPI = dz.get_playlist(id)
playlistTracksAPI = dz.get_playlist_tracks_gw(id)
2020-02-24 17:36:11 +00:00
for pos, trackAPI in enumerate(playlistTracksAPI, start=1):
2020-02-27 16:34:49 +00:00
trackAPI['_EXTRA_PLAYLIST'] = playlistAPI
2020-02-24 17:36:11 +00:00
trackAPI['POSITION'] = pos
trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
2020-02-22 13:38:01 +00:00
downloadTrackObj(trackAPI, settings, overwriteBitrate)