152 lines
5.4 KiB
Python
Executable File
152 lines
5.4 KiB
Python
Executable File
from flask import Blueprint, Response, request
|
|
import json
|
|
import os
|
|
import random
|
|
import string
|
|
import sys
|
|
import traceback
|
|
from multiprocessing import Process
|
|
|
|
album_bp = Blueprint('album', __name__)
|
|
|
|
def generate_random_filename(length=6):
|
|
chars = string.ascii_lowercase + string.digits
|
|
return ''.join(random.choice(chars) for _ in range(length)) + '.prg'
|
|
|
|
class FlushingFileWrapper:
|
|
def __init__(self, file):
|
|
self.file = file
|
|
|
|
def write(self, text):
|
|
# Filter lines to only write JSON objects
|
|
for line in text.split('\n'):
|
|
if line.startswith('{'):
|
|
self.file.write(line + '\n')
|
|
self.file.flush()
|
|
|
|
def flush(self):
|
|
self.file.flush()
|
|
|
|
def download_task(service, url, main, fallback, quality, fall_quality, prg_path):
|
|
try:
|
|
from routes.utils.album import download_album
|
|
with open(prg_path, 'w') as f:
|
|
flushing_file = FlushingFileWrapper(f)
|
|
original_stdout = sys.stdout
|
|
sys.stdout = flushing_file # Redirect stdout
|
|
|
|
try:
|
|
download_album(
|
|
service=service,
|
|
url=url,
|
|
main=main,
|
|
fallback=fallback,
|
|
quality=quality,
|
|
fall_quality=fall_quality
|
|
)
|
|
flushing_file.write(json.dumps({"status": "complete"}) + "\n")
|
|
except Exception as e:
|
|
error_data = json.dumps({
|
|
"status": "error",
|
|
"message": str(e),
|
|
"traceback": traceback.format_exc()
|
|
})
|
|
flushing_file.write(error_data + "\n")
|
|
finally:
|
|
sys.stdout = original_stdout # Restore stdout
|
|
except Exception as e:
|
|
with open(prg_path, 'w') as f:
|
|
error_data = json.dumps({
|
|
"status": "error",
|
|
"message": str(e),
|
|
"traceback": traceback.format_exc()
|
|
})
|
|
f.write(error_data + "\n")
|
|
|
|
@album_bp.route('/download', methods=['GET'])
|
|
def handle_download():
|
|
service = request.args.get('service')
|
|
url = request.args.get('url')
|
|
main = request.args.get('main')
|
|
fallback = request.args.get('fallback')
|
|
quality = request.args.get('quality')
|
|
fall_quality = request.args.get('fall_quality')
|
|
|
|
# Sanitize main and fallback to prevent directory traversal
|
|
if main:
|
|
main = os.path.basename(main)
|
|
if fallback:
|
|
fallback = os.path.basename(fallback)
|
|
|
|
if not all([service, url, main]):
|
|
return Response(
|
|
json.dumps({"error": "Missing parameters"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
|
|
# Validate credentials based on service and fallback
|
|
try:
|
|
if service == 'spotify':
|
|
if fallback:
|
|
# Validate Deezer main 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(
|
|
json.dumps({"error": "Invalid Deezer credentials directory"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
spotify_fallback_path = os.path.abspath(os.path.join('./creds/spotify', fallback, 'credentials.json'))
|
|
if not os.path.isfile(spotify_fallback_path):
|
|
return Response(
|
|
json.dumps({"error": "Invalid Spotify fallback credentials directory"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
else:
|
|
# 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(
|
|
json.dumps({"error": "Invalid Spotify credentials directory"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
elif service == 'deezer':
|
|
# 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(
|
|
json.dumps({"error": "Invalid Deezer credentials directory"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
else:
|
|
return Response(
|
|
json.dumps({"error": "Unsupported service"}),
|
|
status=400,
|
|
mimetype='application/json'
|
|
)
|
|
except Exception as e:
|
|
return Response(
|
|
json.dumps({"error": f"Credential validation failed: {str(e)}"}),
|
|
status=500,
|
|
mimetype='application/json'
|
|
)
|
|
|
|
filename = generate_random_filename()
|
|
prg_dir = './prgs'
|
|
os.makedirs(prg_dir, exist_ok=True)
|
|
prg_path = os.path.join(prg_dir, filename)
|
|
|
|
Process(
|
|
target=download_task,
|
|
args=(service, url, main, fallback, quality, fall_quality, prg_path)
|
|
).start()
|
|
|
|
return Response(
|
|
json.dumps({"prg_file": filename}),
|
|
status=202,
|
|
mimetype='application/json'
|
|
) |