Improved everything

This commit is contained in:
cool.gitter.choco
2025-01-26 20:14:29 -06:00
parent 8509a123a6
commit feaab332c8
8 changed files with 313 additions and 262 deletions

View File

@@ -4,11 +4,13 @@ import os
import random
import string
import sys
from threading import Thread
import traceback
from multiprocessing import Process # Changed from Thread to Process
playlist_bp = Blueprint('playlist', __name__)
# Removed thread-local stdout setup since we're using process isolation
def generate_random_filename(length=6):
chars = string.ascii_lowercase + string.digits
return ''.join(random.choice(chars) for _ in range(length)) + '.prg'
@@ -24,14 +26,49 @@ class FlushingFileWrapper:
def flush(self):
self.file.flush()
# Moved download_task to top-level for picklability
def download_task(service, url, main, fallback, prg_path):
try:
from routes.utils.playlist import download_playlist
with open(prg_path, 'w') as f:
flushing_file = FlushingFileWrapper(f)
original_stdout = sys.stdout
sys.stdout = flushing_file # Process-specific stdout
try:
download_playlist(
service=service,
url=url,
main=main,
fallback=fallback
)
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 original stdout
except Exception as e:
# Handle exceptions outside the main logic
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")
@playlist_bp.route('/download', methods=['GET'])
def handle_download():
service = request.args.get('service')
url = request.args.get('url')
main = request.args.get('main') # Changed from 'account'
fallback = request.args.get('fallback') # New parameter
main = request.args.get('main')
fallback = request.args.get('fallback')
# Validate required parameters (main instead of account)
if not all([service, url, main]):
return Response(
json.dumps({"error": "Missing parameters"}),
@@ -44,42 +81,11 @@ def handle_download():
os.makedirs(prg_dir, exist_ok=True)
prg_path = os.path.join(prg_dir, filename)
def download_task():
try:
from routes.utils.playlist import download_playlist
with open(prg_path, 'w') as f:
flushing_file = FlushingFileWrapper(f)
original_stdout = sys.stdout
sys.stdout = flushing_file
try:
# Updated call with main/fallback parameters
download_playlist(
service=service,
url=url,
main=main,
fallback=fallback
)
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
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")
Thread(target=download_task).start()
# Start a new process with required arguments
Process(
target=download_task,
args=(service, url, main, fallback, prg_path)
).start()
return Response(
json.dumps({"prg_file": filename}),