custom formatting for api
This commit is contained in:
@@ -21,7 +21,7 @@ class FlushingFileWrapper:
|
|||||||
self.file = file
|
self.file = file
|
||||||
|
|
||||||
def write(self, text):
|
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'):
|
for line in text.split('\n'):
|
||||||
if line.startswith('{'):
|
if line.startswith('{'):
|
||||||
self.file.write(line + '\n')
|
self.file.write(line + '\n')
|
||||||
@@ -30,7 +30,8 @@ class FlushingFileWrapper:
|
|||||||
def flush(self):
|
def flush(self):
|
||||||
self.file.flush()
|
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:
|
try:
|
||||||
from routes.utils.track import download_track
|
from routes.utils.track import download_track
|
||||||
with open(prg_path, 'w') as f:
|
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,
|
fallback=fallback,
|
||||||
quality=quality,
|
quality=quality,
|
||||||
fall_quality=fall_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")
|
flushing_file.write(json.dumps({"status": "complete"}) + "\n")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -89,6 +92,10 @@ def handle_download():
|
|||||||
real_time_arg = request.args.get('real_time', 'false')
|
real_time_arg = request.args.get('real_time', 'false')
|
||||||
real_time = real_time_arg.lower() in ['true', '1', 'yes']
|
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
|
# Sanitize main and fallback to prevent directory traversal
|
||||||
if main:
|
if main:
|
||||||
main = os.path.basename(main)
|
main = os.path.basename(main)
|
||||||
@@ -106,7 +113,7 @@ def handle_download():
|
|||||||
try:
|
try:
|
||||||
if service == 'spotify':
|
if service == 'spotify':
|
||||||
if fallback:
|
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'))
|
deezer_creds_path = os.path.abspath(os.path.join('./creds/deezer', main, 'credentials.json'))
|
||||||
if not os.path.isfile(deezer_creds_path):
|
if not os.path.isfile(deezer_creds_path):
|
||||||
return Response(
|
return Response(
|
||||||
@@ -122,7 +129,7 @@ def handle_download():
|
|||||||
mimetype='application/json'
|
mimetype='application/json'
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Validate Spotify main credentials
|
# Validate Spotify main credentials.
|
||||||
spotify_creds_path = os.path.abspath(os.path.join('./creds/spotify', main, 'credentials.json'))
|
spotify_creds_path = os.path.abspath(os.path.join('./creds/spotify', main, 'credentials.json'))
|
||||||
if not os.path.isfile(spotify_creds_path):
|
if not os.path.isfile(spotify_creds_path):
|
||||||
return Response(
|
return Response(
|
||||||
@@ -131,7 +138,7 @@ def handle_download():
|
|||||||
mimetype='application/json'
|
mimetype='application/json'
|
||||||
)
|
)
|
||||||
elif service == 'deezer':
|
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'))
|
deezer_creds_path = os.path.abspath(os.path.join('./creds/deezer', main, 'credentials.json'))
|
||||||
if not os.path.isfile(deezer_creds_path):
|
if not os.path.isfile(deezer_creds_path):
|
||||||
return Response(
|
return Response(
|
||||||
@@ -162,7 +169,10 @@ def handle_download():
|
|||||||
|
|
||||||
process = Process(
|
process = Process(
|
||||||
target=download_task,
|
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()
|
process.start()
|
||||||
# Track the running process using the generated filename.
|
# Track the running process using the generated filename.
|
||||||
|
|||||||
@@ -4,7 +4,17 @@ import traceback
|
|||||||
from deezspot.spotloader import SpoLogin
|
from deezspot.spotloader import SpoLogin
|
||||||
from deezspot.deezloader import DeeLogin
|
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:
|
try:
|
||||||
if service == 'spotify':
|
if service == 'spotify':
|
||||||
if fallback:
|
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:
|
with open(deezer_creds_path, 'r') as f:
|
||||||
deezer_creds = json.load(f)
|
deezer_creds = json.load(f)
|
||||||
dl = DeeLogin(
|
dl = DeeLogin(
|
||||||
arl=deezer_creds.get('arl', ''),
|
arl=deezer_creds.get('arl', '')
|
||||||
)
|
)
|
||||||
dl.download_trackspo(
|
dl.download_trackspo(
|
||||||
link_track=url,
|
link_track=url,
|
||||||
@@ -28,9 +38,12 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality
|
|||||||
recursive_quality=False,
|
recursive_quality=False,
|
||||||
recursive_download=False,
|
recursive_download=False,
|
||||||
not_interface=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:
|
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_dir = os.path.join('./creds/spotify', fallback)
|
||||||
spo_creds_path = os.path.abspath(os.path.join(spo_creds_dir, 'credentials.json'))
|
spo_creds_path = os.path.abspath(os.path.join(spo_creds_dir, 'credentials.json'))
|
||||||
spo = SpoLogin(credentials_path=spo_creds_path)
|
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,
|
recursive_download=False,
|
||||||
not_interface=False,
|
not_interface=False,
|
||||||
method_save=1,
|
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:
|
else:
|
||||||
# Directly use Spotify main account
|
# Directly use Spotify main account
|
||||||
@@ -59,18 +74,20 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality
|
|||||||
recursive_download=False,
|
recursive_download=False,
|
||||||
not_interface=False,
|
not_interface=False,
|
||||||
method_save=1,
|
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':
|
elif service == 'deezer':
|
||||||
if quality is None:
|
if quality is None:
|
||||||
quality = 'FLAC'
|
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_dir = os.path.join('./creds/deezer', main)
|
||||||
creds_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json'))
|
creds_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json'))
|
||||||
with open(creds_path, 'r') as f:
|
with open(creds_path, 'r') as f:
|
||||||
creds = json.load(f)
|
creds = json.load(f)
|
||||||
dl = DeeLogin(
|
dl = DeeLogin(
|
||||||
arl=creds.get('arl', ''),
|
arl=creds.get('arl', '')
|
||||||
)
|
)
|
||||||
dl.download_trackdee(
|
dl.download_trackdee(
|
||||||
link_track=url,
|
link_track=url,
|
||||||
@@ -78,7 +95,9 @@ def download_track(service, url, main, fallback=None, quality=None, fall_quality
|
|||||||
quality_download=quality,
|
quality_download=quality,
|
||||||
recursive_quality=False,
|
recursive_quality=False,
|
||||||
recursive_download=False,
|
recursive_download=False,
|
||||||
method_save=1
|
method_save=1,
|
||||||
|
custom_dir_format=custom_dir_format,
|
||||||
|
custom_track_format=custom_track_format
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unsupported service: {service}")
|
raise ValueError(f"Unsupported service: {service}")
|
||||||
|
|||||||
Reference in New Issue
Block a user