From c26478dd08b0d119d9abf8253eb0f58c7125342d Mon Sep 17 00:00:00 2001 From: "cool.gitter.choco" Date: Thu, 6 Feb 2025 14:21:15 -0600 Subject: [PATCH] custom formatting for api --- routes/track.py | 24 +++++++++++++++++------- routes/utils/track.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/routes/track.py b/routes/track.py index f19bf22..11b18ec 100755 --- a/routes/track.py +++ b/routes/track.py @@ -21,7 +21,7 @@ class FlushingFileWrapper: self.file = file def write(self, text): - # Write only lines that start with a JSON object + # Write only lines that start with a JSON object. for line in text.split('\n'): if line.startswith('{'): self.file.write(line + '\n') @@ -30,7 +30,8 @@ class FlushingFileWrapper: def flush(self): self.file.flush() -def download_task(service, url, main, fallback, quality, fall_quality, real_time, prg_path, orig_request): +def download_task(service, url, main, fallback, quality, fall_quality, real_time, + prg_path, orig_request, custom_dir_format, custom_track_format): try: from routes.utils.track import download_track with open(prg_path, 'w') as f: @@ -55,7 +56,9 @@ def download_task(service, url, main, fallback, quality, fall_quality, real_time fallback=fallback, quality=quality, fall_quality=fall_quality, - real_time=real_time + real_time=real_time, + custom_dir_format=custom_dir_format, + custom_track_format=custom_track_format ) flushing_file.write(json.dumps({"status": "complete"}) + "\n") except Exception as e: @@ -89,6 +92,10 @@ def handle_download(): real_time_arg = request.args.get('real_time', 'false') real_time = real_time_arg.lower() in ['true', '1', 'yes'] + # New query parameters for custom formatting. + custom_dir_format = request.args.get('custom_dir_format', "%ar_album%/%album%/%copyright%") + custom_track_format = request.args.get('custom_track_format', "%tracknum%. %music% - %artist%") + # Sanitize main and fallback to prevent directory traversal if main: main = os.path.basename(main) @@ -106,7 +113,7 @@ def handle_download(): try: if service == 'spotify': if fallback: - # Validate Deezer main credentials and Spotify fallback credentials + # Validate Deezer main credentials and Spotify fallback credentials. deezer_creds_path = os.path.abspath(os.path.join('./creds/deezer', main, 'credentials.json')) if not os.path.isfile(deezer_creds_path): return Response( @@ -122,7 +129,7 @@ def handle_download(): mimetype='application/json' ) else: - # Validate Spotify main credentials + # Validate Spotify main credentials. spotify_creds_path = os.path.abspath(os.path.join('./creds/spotify', main, 'credentials.json')) if not os.path.isfile(spotify_creds_path): return Response( @@ -131,7 +138,7 @@ def handle_download(): mimetype='application/json' ) elif service == 'deezer': - # Validate Deezer main credentials + # Validate Deezer main credentials. deezer_creds_path = os.path.abspath(os.path.join('./creds/deezer', main, 'credentials.json')) if not os.path.isfile(deezer_creds_path): return Response( @@ -162,7 +169,10 @@ def handle_download(): process = Process( target=download_task, - args=(service, url, main, fallback, quality, fall_quality, real_time, prg_path, orig_request) + args=( + service, url, main, fallback, quality, fall_quality, real_time, + prg_path, orig_request, custom_dir_format, custom_track_format + ) ) process.start() # Track the running process using the generated filename. diff --git a/routes/utils/track.py b/routes/utils/track.py index 4ebae88..0510ee7 100755 --- a/routes/utils/track.py +++ b/routes/utils/track.py @@ -4,7 +4,17 @@ import traceback from deezspot.spotloader import SpoLogin from deezspot.deezloader import DeeLogin -def download_track(service, url, main, fallback=None, quality=None, fall_quality=None, real_time=False): +def download_track( + service, + url, + main, + fallback=None, + quality=None, + fall_quality=None, + real_time=False, + custom_dir_format="%ar_album%/%album%/%copyright%", + custom_track_format="%tracknum%. %music% - %artist%" +): try: if service == 'spotify': if fallback: @@ -19,7 +29,7 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality with open(deezer_creds_path, 'r') as f: deezer_creds = json.load(f) dl = DeeLogin( - arl=deezer_creds.get('arl', ''), + arl=deezer_creds.get('arl', '') ) dl.download_trackspo( link_track=url, @@ -28,9 +38,12 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality recursive_quality=False, recursive_download=False, not_interface=False, - method_save=1 + method_save=1, + custom_dir_format=custom_dir_format, + custom_track_format=custom_track_format ) except Exception as e: + # If the first attempt fails, use the fallback Spotify credentials spo_creds_dir = os.path.join('./creds/spotify', fallback) spo_creds_path = os.path.abspath(os.path.join(spo_creds_dir, 'credentials.json')) spo = SpoLogin(credentials_path=spo_creds_path) @@ -42,7 +55,9 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality recursive_download=False, not_interface=False, method_save=1, - real_time_dl=real_time + real_time_dl=real_time, + custom_dir_format=custom_dir_format, + custom_track_format=custom_track_format ) else: # Directly use Spotify main account @@ -59,18 +74,20 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality recursive_download=False, not_interface=False, method_save=1, - real_time_dl=real_time + real_time_dl=real_time, + custom_dir_format=custom_dir_format, + custom_track_format=custom_track_format ) elif service == 'deezer': if quality is None: quality = 'FLAC' - # Deezer download logic remains unchanged, with real_time_dl passed accordingly + # Deezer download logic remains unchanged, with the custom formatting parameters passed along. creds_dir = os.path.join('./creds/deezer', main) creds_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json')) with open(creds_path, 'r') as f: creds = json.load(f) dl = DeeLogin( - arl=creds.get('arl', ''), + arl=creds.get('arl', '') ) dl.download_trackdee( link_track=url, @@ -78,7 +95,9 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality quality_download=quality, recursive_quality=False, recursive_download=False, - method_save=1 + method_save=1, + custom_dir_format=custom_dir_format, + custom_track_format=custom_track_format ) else: raise ValueError(f"Unsupported service: {service}")