Added playlist support

This commit is contained in:
RemixDev 2020-02-22 14:38:01 +01:00
parent a4e4ecdb98
commit e9f010628e
2 changed files with 80 additions and 39 deletions

View File

@ -2,6 +2,7 @@
from deemix.api.deezer import Deezer, APIError from deemix.api.deezer import Deezer, APIError
from deemix.utils.taggers import tagID3, tagFLAC from deemix.utils.taggers import tagID3, tagFLAC
import os.path import os.path
from urllib.error import HTTPError
dz = Deezer() dz = Deezer()
@ -15,18 +16,36 @@ extensions = {
13: '.mp4' 13: '.mp4'
} }
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
def getTrackData(trackAPI): def getTrackData(trackAPI):
if not 'MD5_ORIGIN' in trackAPI: if not 'MD5_ORIGIN' in trackAPI:
trackAPI['MD5_ORIGIN'] = dz.get_track_md5(trackAPI['SNG_ID']) trackAPI['MD5_ORIGIN'] = dz.get_track_md5(trackAPI['SNG_ID'])
track = {} track = {}
track['id'] = trackAPI['SNG_ID']
track['title'] = trackAPI['SNG_TITLE'] track['title'] = trackAPI['SNG_TITLE']
if trackAPI['VERSION']: if trackAPI['VERSION']:
track['title'] += " " + trackAPI['VERSION'] track['title'] += " " + trackAPI['VERSION']
track['duration'] = trackAPI['DURATION']
track['MD5'] = trackAPI['MD5_ORIGIN'] track = parseEssentialTrackData(track, trackAPI)
track['mediaVersion'] = trackAPI['MEDIA_VERSION']
if int(track['id']) < 0: if int(track['id']) < 0:
track['filesize'] = trackAPI['FILESIZE'] track['filesize'] = trackAPI['FILESIZE']
@ -47,15 +66,6 @@ def getTrackData(trackAPI):
track['localTrack'] = True track['localTrack'] = True
return track return track
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
if 'DISK_NUMBER' in trackAPI: if 'DISK_NUMBER' in trackAPI:
track['discNumber'] = trackAPI['DISK_NUMBER'] track['discNumber'] = trackAPI['DISK_NUMBER']
if 'EXPLICIT_LYRICS' in trackAPI: if 'EXPLICIT_LYRICS' in trackAPI:
@ -65,10 +75,6 @@ def getTrackData(trackAPI):
track['replayGain'] = "{0:.2f} dB".format((float(trackAPI['GAIN']) + 18.4) * -1) track['replayGain'] = "{0:.2f} dB".format((float(trackAPI['GAIN']) + 18.4) * -1)
track['ISRC'] = trackAPI['ISRC'] track['ISRC'] = trackAPI['ISRC']
track['trackNumber'] = trackAPI['TRACK_NUMBER'] track['trackNumber'] = trackAPI['TRACK_NUMBER']
if 'FALLBACK' in trackAPI:
track['fallbackId'] = trackAPI['FALLBACK']['SNG_ID']
else:
track['fallbackId'] = 0
track['contributors'] = trackAPI['SNG_CONTRIBUTORS'] track['contributors'] = trackAPI['SNG_CONTRIBUTORS']
track['lyrics'] = {} track['lyrics'] = {}
@ -106,6 +112,8 @@ def getTrackData(trackAPI):
if 'ALB_PICTURE' in trackAPI: if 'ALB_PICTURE' in trackAPI:
track['album']['pic'] = trackAPI['ALB_PICTURE'] track['album']['pic'] = trackAPI['ALB_PICTURE']
albumAPI = None
albumAPI2 = None
try: try:
if 'ALBUM_EXTRA' in trackAPI: if 'ALBUM_EXTRA' in trackAPI:
albumAPI = trackAPI['ALBUM_EXTRA'] albumAPI = trackAPI['ALBUM_EXTRA']
@ -118,11 +126,11 @@ def getTrackData(trackAPI):
} }
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 None track['album']['barcode'] = albumAPI['upc'] if 'upc' in albumAPI else "Unknown"
track['album']['label'] = albumAPI['label'] if 'label' in albumAPI else None 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'][43:-24]
if 'release_date' in albumAPI: if 'release_date' in albumAPI and not 'date' in track:
track['date'] = { track['date'] = {
'day': albumAPI["release_date"][8:10], 'day': albumAPI["release_date"][8:10],
'month': albumAPI["release_date"][5:7], 'month': albumAPI["release_date"][5:7],
@ -135,23 +143,23 @@ def getTrackData(trackAPI):
for genre in albumAPI['genres']['data']: for genre in albumAPI['genres']['data']:
track['album']['genre'].append(genre['name']) track['album']['genre'].append(genre['name'])
except APIError: except APIError:
albumAPI = dz.get_album_gw(track['album']['id']) albumAPI2 = dz.get_album_gw(track['album']['id'])
track['album']['artist'] = { track['album']['artist'] = {
'id': albumAPI['ART_ID'], 'id': albumAPI2['ART_ID'],
'name': albumAPI['ART_NAME'] 'name': albumAPI2['ART_NAME']
} }
track['album']['trackTotal'] = albumAPI['NUMBER_TRACK'] track['album']['trackTotal'] = albumAPI2['NUMBER_TRACK']
track['album']['discTotal'] = albumAPI['NUMBER_DISK'] track['album']['discTotal'] = albumAPI2['NUMBER_DISK']
track['album']['recordType'] = trackAPI['TYPE'] track['album']['recordType'] = trackAPI['TYPE']
track['album']['barcode'] = None track['album']['barcode'] = "Unknown"
track['album']['label'] = albumAPI['LABEL_NAME'] if 'LABEL_NAME' in albumAPI else None track['album']['label'] = albumAPI2['LABEL_NAME'] if 'LABEL_NAME' in albumAPI2 else "Unknown"
if not 'pic' in track['album']: if not 'pic' in track['album']:
track['album']['pic'] = albumAPI['ALB_PICTURE'] track['album']['pic'] = albumAPI2['ALB_PICTURE']
if 'PHYSICAL_RELEASE_DATE' in albumAPI: if 'PHYSICAL_RELEASE_DATE' in albumAPI2 and not 'date' in track:
track['date'] = { track['date'] = {
'day': albumAPI["PHYSICAL_RELEASE_DATE"][8:10], 'day': albumAPI2["PHYSICAL_RELEASE_DATE"][8:10],
'month': albumAPI["PHYSICAL_RELEASE_DATE"][5:7], 'month': albumAPI2["PHYSICAL_RELEASE_DATE"][5:7],
'year': albumAPI["PHYSICAL_RELEASE_DATE"][0:4] 'year': albumAPI2["PHYSICAL_RELEASE_DATE"][0:4]
} }
track['album']['genre'] = [] track['album']['genre'] = []
@ -161,6 +169,8 @@ def getTrackData(trackAPI):
track['replayGain'] = "{0:.2f} dB".format((float(trackAPI2['gain']) + 18.4) * -1) track['replayGain'] = "{0:.2f} dB".format((float(trackAPI2['gain']) + 18.4) * -1)
if not 'explicit' in track: if not 'explicit' in track:
track['explicit'] = trackAPI2['explicit_lyrics'] track['explicit'] = trackAPI2['explicit_lyrics']
if not 'discNumber' in track:
track['discNumber'] = trackAPI2['disk_number']
track['artist'] = {} track['artist'] = {}
track['artists'] = [] track['artists'] = []
for artist in trackAPI2['contributors']: for artist in trackAPI2['contributors']:
@ -169,19 +179,23 @@ def getTrackData(trackAPI):
track['artist'][artist['role']] = [] track['artist'][artist['role']] = []
track['artist'][artist['role']].append(artist['name']) track['artist'][artist['role']].append(artist['name'])
if not 'discTotal' in track['album']: if not 'discTotal' in track['album'] or not track['album']['discTotal']:
albumAPI2 = dz.get_album_gw(track['album']['id']) if not albumAPI2:
albumAPI2 = dz.get_album_gw(track['album']['id'])
track['album']['discTotal'] = albumAPI2['NUMBER_DISK'] track['album']['discTotal'] = albumAPI2['NUMBER_DISK']
if not 'copyright' in track: if not 'copyright' in track or not track['copyright']:
if not albumAPI2: if not albumAPI2:
albumAPI2 = dz.get_album_gw(track['album']['id']) albumAPI2 = dz.get_album_gw(track['album']['id'])
track['copyright'] = albumAPI2['COPYRIGHT'] track['copyright'] = albumAPI2['COPYRIGHT']
return track return track
def downloadTrackObj(trackAPI, settings, overwriteBitrate=False): def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None):
# Get the metadata # Get the metadata
track = getTrackData(trackAPI) if extraTrack:
track = extraTrack
else:
track = getTrackData(trackAPI)
print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title'])) print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title']))
# Get the selected bitrate # Get the selected bitrate
@ -227,11 +241,29 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False):
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['selectedFormat'])
with open(writepath, 'wb') as stream: with open(writepath, 'wb') as stream:
dz.stream_track(track['id'], track['downloadUrl'], stream) 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
if track['selectedFormat'] in [3, 1, 8]: if track['selectedFormat'] in [3, 1, 8]:
tagID3(writepath, track, settings['tags']) tagID3(writepath, track, settings['tags'])
elif track['selectedFormat'] == 9: elif track['selectedFormat'] == 9:
tagFLAC(writepath, track, settings['tags']) tagFLAC(writepath, track, settings['tags'])
print("Done!")
return True
def download_track(id, settings, overwriteBitrate=False): def download_track(id, settings, overwriteBitrate=False):
trackAPI = dz.get_track_gw(id) trackAPI = dz.get_track_gw(id)
@ -251,3 +283,10 @@ def download_album(id, settings, overwriteBitrate=False):
for trackAPI in tracksArray: for trackAPI in tracksArray:
trackAPI['ALBUM_EXTRA'] = albumAPI trackAPI['ALBUM_EXTRA'] = albumAPI
downloadTrackObj(trackAPI, settings, overwriteBitrate) downloadTrackObj(trackAPI, settings, overwriteBitrate)
def download_playlist(id, settings, overwriteBitrate=False):
playlistAPI = dz.get_playlist(id)
playlistTracksAPI = dz.get_playlist_tracks_gw(id)
for trackAPI in playlistTracksAPI:
trackAPI['PLAYLIST_EXTRA'] = playlistAPI
downloadTrackObj(trackAPI, settings, overwriteBitrate)

View File

@ -3,7 +3,7 @@ import wx
from deemix.ui.SettingsDialog import SettingsDialog from deemix.ui.SettingsDialog import SettingsDialog
from deemix.app.functions import getIDFromLink, getTypeFromLink from deemix.app.functions import getIDFromLink, getTypeFromLink
from deemix.app.downloader import download_track, download_album from deemix.app.downloader import download_track, download_album, download_playlist
from deemix.app.settings import initSettings from deemix.app.settings import initSettings
menuIDs = { menuIDs = {
@ -52,6 +52,8 @@ class MainFrame(wx.Frame):
download_track(id, self.settings) download_track(id, self.settings)
elif type == "album": elif type == "album":
download_album(id, self.settings) download_album(id, self.settings)
elif type == "playlist":
download_playlist(id, self.settings)
self.text_ctrl.SetValue("") self.text_ctrl.SetValue("")
def close_app(self, event): def close_app(self, event):