64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
from flask import Flask, request, Response, send_from_directory, jsonify
|
|
import subprocess
|
|
import requests
|
|
|
|
app = Flask(__name__, static_folder='../frontend', static_url_path='')
|
|
app.url_map.strict_slashes = False
|
|
|
|
@app.route('/api/status', methods=['POST', 'GET'])
|
|
def proxy_status():
|
|
if request.method == 'POST':
|
|
# Safely get the json body
|
|
client_data = request.get_json() or {}
|
|
target_server = client_data.get('server')
|
|
else:
|
|
target_server = request.args.get('server')
|
|
|
|
if not target_server:
|
|
return jsonify({"error": "No server provided"}), 400
|
|
|
|
try:
|
|
# Use the data gathered above
|
|
response = requests.post(target_server, json=client_data if request.method == 'POST' else {}, timeout=5)
|
|
return (response.content, response.status_code, response.headers.items())
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return send_from_directory(app.static_folder, 'index.html')
|
|
|
|
@app.route('/api/stream', methods=['POST', 'GET'])
|
|
def stream_video():
|
|
# Note: <video> tags perform GET. To support your POST requirement,
|
|
# we handle the URL via JSON post or URL params.
|
|
video_url = ""
|
|
if request.method == 'POST':
|
|
video_url = request.json.get('url')
|
|
else:
|
|
video_url = request.args.get('url')
|
|
|
|
def generate():
|
|
# yt-dlp command to get the stream and pipe to stdout
|
|
cmd = [
|
|
'yt-dlp',
|
|
'-o', '-', # output to stdout
|
|
'-f', 'best[ext=mp4]/best',
|
|
video_url
|
|
]
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
try:
|
|
while True:
|
|
chunk = process.stdout.read(1024 * 16)
|
|
if not chunk:
|
|
break
|
|
yield chunk
|
|
finally:
|
|
process.kill()
|
|
|
|
return Response(generate(), mimetype='video/mp4')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000) |