Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, Response, request, stream_with_context
|
2 |
+
import subprocess
|
3 |
+
import shlex
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
@app.route('/')
|
8 |
+
def index():
|
9 |
+
return '''
|
10 |
+
<html>
|
11 |
+
<body>
|
12 |
+
<form action="/download" method="get">
|
13 |
+
<input type="url" name="url" style="width: 350px" required>
|
14 |
+
<input type="submit">
|
15 |
+
</form>
|
16 |
+
</body>
|
17 |
+
</html>
|
18 |
+
'''
|
19 |
+
|
20 |
+
@app.route('/download')
|
21 |
+
def download_file():
|
22 |
+
url = request.args.get('url')
|
23 |
+
if not url:
|
24 |
+
return 400
|
25 |
+
|
26 |
+
try:
|
27 |
+
pre_command = f'proxychains yt-dlp --print format -f "best[height<400]/bestvideo[height<400]+bestaudio" -o - {shlex.quote(url)}'
|
28 |
+
command = f'proxychains yt-dlp -f "best[height<400]/bestvideo[height<400]+bestaudio" -o - {shlex.quote(url)}'
|
29 |
+
|
30 |
+
pre_check = subprocess.run(
|
31 |
+
pre_command,
|
32 |
+
shell=True,
|
33 |
+
stdout=subprocess.PIPE,
|
34 |
+
stderr=subprocess.PIPE
|
35 |
+
)
|
36 |
+
|
37 |
+
if pre_check.returncode != 0:
|
38 |
+
error_msg = pre_check.stderr.decode('utf-8', 'ignore')
|
39 |
+
app.logger.error(f"Pre-check failed: {error_msg}")
|
40 |
+
return f"error: {error_msg}", 500
|
41 |
+
|
42 |
+
process = subprocess.Popen(
|
43 |
+
command,
|
44 |
+
shell=True,
|
45 |
+
stdout=subprocess.PIPE,
|
46 |
+
stderr=subprocess.PIPE,
|
47 |
+
bufsize=8192
|
48 |
+
)
|
49 |
+
|
50 |
+
def generate():
|
51 |
+
try:
|
52 |
+
while True:
|
53 |
+
chunk = process.stdout.read(8192)
|
54 |
+
if not chunk:
|
55 |
+
break
|
56 |
+
yield chunk
|
57 |
+
|
58 |
+
stderr = process.stderr.read()
|
59 |
+
if process.wait() != 0:
|
60 |
+
app.logger.error(f"error: {stderr.decode('utf-8', 'ignore')}")
|
61 |
+
|
62 |
+
finally:
|
63 |
+
if process.poll() is None:
|
64 |
+
process.terminate()
|
65 |
+
process.wait()
|
66 |
+
|
67 |
+
filename = 'downloaded_video'
|
68 |
+
|
69 |
+
headers = {
|
70 |
+
'Content-Type': 'application/octet-stream',
|
71 |
+
'Content-Disposition': f'attachment; filename="{filename}"'
|
72 |
+
}
|
73 |
+
|
74 |
+
return Response(
|
75 |
+
stream_with_context(generate()),
|
76 |
+
headers=headers
|
77 |
+
)
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
return f"error: {str(e)}", 500
|
81 |
+
|
82 |
+
if __name__ == '__main__':
|
83 |
+
app.run(host='0.0.0.0', port=7860)
|