-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (74 loc) · 2.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
from flask import Flask, request, jsonify
from celery import Celery
from urllib.parse import urlparse, urlunparse
import shlex
import subprocess
import config # Import configuration
# Configure logging
logging.basicConfig(
filename=config.LOG_FILE,
level=getattr(logging, config.LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s - %(levelname)s - %(message)s"
)
app = Flask(__name__)
# Configure Celery
app.config['CELERY_BROKER_URL'] = config.CELERY_BROKER_URL
app.config['CELERY_RESULT_BACKEND'] = config.CELERY_RESULT_BACKEND
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
def normalize_url(url):
"""
Adds a default scheme (http) to URLs if missing and validates the format.
"""
try:
parsed = urlparse(url)
if not parsed.scheme:
parsed = parsed._replace(scheme="http")
if not parsed.netloc:
return None
return urlunparse(parsed)
except Exception:
return None
@app.route('/vdl', methods=['POST'])
def queue_command():
"""
Handles POST requests to queue yt-dlp downloads.
"""
# Get the data from the request
data = request.json
if not data or 'url' not in data:
return jsonify({'error': 'Missing URL'}), 400
url = data['url']
normalized_url = normalize_url(url)
if not normalized_url:
return jsonify({'error': 'Invalid URL'}), 400
# Extract additional arguments
additional_args = data.get('args', [])
if not isinstance(additional_args, list):
return jsonify({'error': 'Args must be a list of strings'}), 400
# Queue the download task
task = download_video.apply_async(args=[normalized_url, additional_args])
return jsonify({'task_id': task.id, 'status': 'queued'}), 202
@celery.task
def download_video(url, additional_args):
"""
Handles the actual download process using yt-dlp.
"""
logging.info(f"Starting download for: {url} with args: {additional_args}")
# Safely escape each argument
safe_additional_args = [shlex.quote(arg) for arg in additional_args]
# Combine default arguments and additional arguments
all_args = config.YT_DLP_DEFAULT_ARGS + safe_additional_args
args_string = " ".join(all_args)
command = f'yt-dlp {args_string} -o "{config.YT_DLP_OUTPUT_TEMPLATE}" {shlex.quote(url)}'
try:
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
logging.info(f"Download complete for: {url}")
return {'result': result}
except subprocess.CalledProcessError as e:
logging.error(f"Download failed for: {url}. Error: {e.output}")
raise e
if __name__ == '__main__':
logging.info("Starting Flask app")
app.run(host=config.FLASK_HOST, port=config.FLASK_PORT)