Moved frontend functions to deemix.app.main and added artist support

This commit is contained in:
RemixDev 2020-03-02 13:46:48 +01:00
parent 0e9ee1d874
commit 880ee2a32e
4 changed files with 46 additions and 31 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import click import click
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt import deemix.app.main as app
from deemix.app.downloader import download_track, download_album, download_playlist
from deemix.app.settings import initSettings from deemix.app.settings import initSettings
@click.command() @click.command()
@ -9,19 +8,7 @@ from deemix.app.settings import initSettings
@click.argument('url') @click.argument('url')
def download(bitrate, url): def download(bitrate, url):
settings = initSettings() settings = initSettings()
forcedBitrate = getBitrateInt(bitrate) app.downloadLink(url, settings, bitrate)
type = getTypeFromLink(url)
id = getIDFromLink(url, type)
if type == None or id == None:
click.echo("URL not recognized")
if type == "track":
download_track(id, settings, forcedBitrate)
elif type == "album":
download_album(id, settings, forcedBitrate)
elif type == "playlist":
download_playlist(id, settings, forcedBitrate)
else:
click.echo("URL not supported yet")
click.echo("All done!") click.echo("All done!")
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -253,7 +253,7 @@ class Deezer:
i += 1 i += 1
def stream_track(self, track_id, url, stream): def stream_track(self, track_id, url, stream):
request = requests.get(url, stream=True) request = requests.get(url, headers=self.http_headers, stream=True)
request.raise_for_status() request.raise_for_status()
blowfish_key = str.encode(self._get_blowfish_key(str(track_id))) blowfish_key = str.encode(self._get_blowfish_key(str(track_id)))
i = 0 i = 0

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from deemix.api.deezer import Deezer, APIError from deemix.api.deezer import APIError, USER_AGENT_HEADER
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
@ -8,8 +8,8 @@ from requests import get
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from tempfile import gettempdir from tempfile import gettempdir
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import json
dz = Deezer()
TEMPDIR = os.path.join(gettempdir(), 'deezloader-imgs') TEMPDIR = os.path.join(gettempdir(), 'deezloader-imgs')
if not os.path.isdir(TEMPDIR): if not os.path.isdir(TEMPDIR):
makedirs(TEMPDIR) makedirs(TEMPDIR)
@ -28,7 +28,7 @@ def downloadImage(url, path):
if not os.path.isfile(path): if not os.path.isfile(path):
with open(path, 'wb') as f: with open(path, 'wb') as f:
try: try:
f.write(get(url).content) f.write(get(url, headers={'User-Agent': USER_AGENT_HEADER}).content)
return path return path
except HTTPError: except HTTPError:
print("Couldn't download Image") print("Couldn't download Image")
@ -90,7 +90,7 @@ def parseEssentialTrackData(track, trackAPI):
return track return track
def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = None): def getTrackData(dz, trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = None):
if not 'MD5_ORIGIN' in trackAPI_gw: if not 'MD5_ORIGIN' in trackAPI_gw:
trackAPI_gw['MD5_ORIGIN'] = dz.get_track_md5(trackAPI_gw['SNG_ID']) trackAPI_gw['MD5_ORIGIN'] = dz.get_track_md5(trackAPI_gw['SNG_ID'])
@ -254,13 +254,13 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No
track['copyright'] = albumAPI_gw['COPYRIGHT'] track['copyright'] = albumAPI_gw['COPYRIGHT']
return track return track
def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None): def downloadTrackObj(dz, trackAPI, settings, overwriteBitrate=False, extraTrack=None):
result = {} result = {}
# Get the metadata # Get the metadata
if extraTrack: if extraTrack:
track = extraTrack track = extraTrack
else: else:
track = getTrackData( track = getTrackData(dz,
trackAPI_gw = trackAPI, trackAPI_gw = trackAPI,
trackAPI = trackAPI['_EXTRA_TRACK'] if '_EXTRA_TRACK' in trackAPI else None, trackAPI = trackAPI['_EXTRA_TRACK'] if '_EXTRA_TRACK' in trackAPI else None,
albumAPI = trackAPI['_EXTRA_ALBUM'] if '_EXTRA_ALBUM' in trackAPI else None albumAPI = trackAPI['_EXTRA_ALBUM'] if '_EXTRA_ALBUM' in trackAPI else None
@ -315,14 +315,14 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
if track['selectedFormat'] == 9: if track['selectedFormat'] == 9:
print("Track not available in flac, trying mp3") print("Track not available in flac, trying mp3")
track['filesize']['flac'] = 0 track['filesize']['flac'] = 0
return downloadTrackObj(trackAPI, settings, extraTrack=track) return downloadTrackObj(dz, trackAPI, settings, extraTrack=track)
elif track['fallbackId'] != 0: elif track['fallbackId'] != 0:
print("Track not available, using fallback id") print("Track not available, using fallback id")
trackNew = dz.get_track_gw(track['fallbackId']) trackNew = dz.get_track_gw(track['fallbackId'])
if not 'MD5_ORIGIN' in trackNew: if not 'MD5_ORIGIN' in trackNew:
trackNew['MD5_ORIGIN'] = dz.get_track_md5(trackNew['SNG_ID']) trackNew['MD5_ORIGIN'] = dz.get_track_md5(trackNew['SNG_ID'])
track = parseEssentialTrackData(track, trackNew) track = parseEssentialTrackData(track, trackNew)
return downloadTrackObj(trackNew, settings, extraTrack=track) return downloadTrackObj(dz, trackNew, settings, extraTrack=track)
else: else:
print("ERROR: Track not available on deezer's servers!") print("ERROR: Track not available on deezer's servers!")
return False return False
@ -333,13 +333,13 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
print("Done!") print("Done!")
return result return result
def download_track(id, settings, overwriteBitrate=False): def download_track(dz, id, settings, overwriteBitrate=False):
trackAPI = dz.get_track_gw(id) trackAPI = dz.get_track_gw(id)
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate'] trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
trackAPI['SINGLE_TRACK'] = True trackAPI['SINGLE_TRACK'] = True
downloadTrackObj(trackAPI, settings, overwriteBitrate) downloadTrackObj(dz, trackAPI, settings, overwriteBitrate)
def download_album(id, settings, overwriteBitrate=False): def download_album(dz, id, settings, overwriteBitrate=False):
albumAPI = dz.get_album(id) albumAPI = dz.get_album(id)
albumAPI_gw = dz.get_album_gw(id) albumAPI_gw = dz.get_album_gw(id)
albumAPI['nb_disk'] = albumAPI_gw['NUMBER_DISK'] albumAPI['nb_disk'] = albumAPI_gw['NUMBER_DISK']
@ -349,7 +349,7 @@ def download_album(id, settings, overwriteBitrate=False):
trackAPI['_EXTRA_ALBUM'] = albumAPI trackAPI['_EXTRA_ALBUM'] = albumAPI
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate'] trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
trackAPI['SINGLE_TRACK'] = True trackAPI['SINGLE_TRACK'] = True
downloadTrackObj(trackAPI, settings, overwriteBitrate) downloadTrackObj(dz, trackAPI, settings, overwriteBitrate)
else: else:
tracksArray = dz.get_album_tracks_gw(id) tracksArray = dz.get_album_tracks_gw(id)
playlist = [None] * len(tracksArray) playlist = [None] * len(tracksArray)
@ -358,7 +358,7 @@ def download_album(id, settings, overwriteBitrate=False):
trackAPI['_EXTRA_ALBUM'] = albumAPI trackAPI['_EXTRA_ALBUM'] = albumAPI
trackAPI['POSITION'] = pos trackAPI['POSITION'] = pos
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, dz, trackAPI, settings, overwriteBitrate)
executor.shutdown(wait=True) executor.shutdown(wait=True)
extrasPath = None extrasPath = None
for index in range(len(playlist)): for index in range(len(playlist)):
@ -376,8 +376,13 @@ def download_album(id, settings, overwriteBitrate=False):
for line in playlist: for line in playlist:
f.write(line+"\n") f.write(line+"\n")
def download_artist(dz, id, settings, overwriteBitrate=False):
artistAPI = dz.get_artist_albums(id)
for album in artistAPI['data']:
print(f"Album: {album['title']}")
download_album(dz, album['id'], settings, overwriteBitrate)
def download_playlist(id, settings, overwriteBitrate=False): def download_playlist(dz, id, settings, overwriteBitrate=False):
playlistAPI = dz.get_playlist(id) playlistAPI = dz.get_playlist(id)
playlistTracksAPI = dz.get_playlist_tracks_gw(id) playlistTracksAPI = dz.get_playlist_tracks_gw(id)
playlist = [None] * len(playlistTracksAPI) playlist = [None] * len(playlistTracksAPI)
@ -386,7 +391,7 @@ def download_playlist(id, settings, overwriteBitrate=False):
trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['_EXTRA_PLAYLIST'] = playlistAPI
trackAPI['POSITION'] = pos trackAPI['POSITION'] = pos
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, dz, trackAPI, settings, overwriteBitrate)
executor.shutdown(wait=True) executor.shutdown(wait=True)
extrasPath = None extrasPath = None
for index in range(len(playlist)): for index in range(len(playlist)):

23
deemix/app/main.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
from deemix.api.deezer import Deezer
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt
from deemix.app.downloader import download_track, download_album, download_playlist, download_artist
dz = Deezer()
def downloadLink(url, settings, bitrate=None):
forcedBitrate = getBitrateInt(bitrate)
type = getTypeFromLink(url)
id = getIDFromLink(url, type)
if type == None or id == None:
print("URL not recognized")
if type == "track":
download_track(dz, id, settings, forcedBitrate)
elif type == "album":
download_album(dz, id, settings, forcedBitrate)
elif type == "playlist":
download_playlist(dz, id, settings, forcedBitrate)
elif type == "artist":
download_artist(dz, id, settings, forcedBitrate)
else:
print("URL not supported yet")