-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
60 lines (46 loc) · 1.73 KB
/
server.py
File metadata and controls
60 lines (46 loc) · 1.73 KB
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
import os
from flask import Flask, request, jsonify
from youtube_transcript_api import YouTubeTranscriptApi
app = Flask(__name__)
ytt = YouTubeTranscriptApi()
@app.route("/api/transcript", methods=["POST", "OPTIONS"])
def transcript():
if request.method == "OPTIONS":
return "", 204, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
}
try:
body = request.get_json(silent=True) or {}
video_id = body.get("videoId", "")
if not video_id:
return jsonify({"error": "Missing videoId"}), 400
fetched = ytt.fetch(video_id)
text = " ".join(item.text for item in fetched)
if not text or len(text) < 50:
return jsonify({"error": "Transcript too short or empty"}), 404
return jsonify({"transcript": text}), 200, {
"Access-Control-Allow-Origin": "*",
}
except Exception as e:
return jsonify({"error": str(e)}), 500, {
"Access-Control-Allow-Origin": "*",
}
@app.route("/api/transcript", methods=["GET"])
def health():
import subprocess
warp_status = "unknown"
try:
result = subprocess.run(
["warp-cli", "--accept-tos", "status"],
capture_output=True, text=True, timeout=5
)
warp_status = result.stdout.strip() or result.stderr.strip()
except Exception as e:
warp_status = str(e)
proxy = os.environ.get("ALL_PROXY", "not set")
return jsonify({"status": "ok", "warp": warp_status, "proxy": proxy}), 200
if __name__ == "__main__":
port = int(os.environ.get("PORT", 10000))
app.run(host="0.0.0.0", port=port)