Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,48 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import requests
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
app = FastAPI(title="
|
| 7 |
|
| 8 |
-
|
| 9 |
-
app.add_middleware(
|
| 10 |
-
CORSMiddleware,
|
| 11 |
-
allow_origins=["*"],
|
| 12 |
-
allow_methods=["*"],
|
| 13 |
-
allow_headers=["*"],
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
Returns JSON with `direct_link` if successful.
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return {"ok": True, "direct_link": match.group(1)}
|
| 38 |
-
else:
|
| 39 |
-
return {"ok": False, "error": "Could not find raw file link"}
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return {"ok": False, "error": str(e)}
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
+
import shutil
|
| 3 |
import requests
|
| 4 |
+
from fastapi import FastAPI, Query, HTTPException
|
| 5 |
+
from fastapi.responses import FileResponse
|
| 6 |
+
import os
|
| 7 |
import re
|
| 8 |
|
| 9 |
+
app = FastAPI(title="MP4 Extractor Passthrough")
|
| 10 |
|
| 11 |
+
TEMP_DIR = tempfile.gettempdir()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def sanitize_filename(url: str) -> str:
|
| 14 |
+
"""Generate safe filename from URL."""
|
| 15 |
+
name = os.path.basename(url)
|
| 16 |
+
name = re.sub(r"[^a-zA-Z0-9_.-]", "_", name)
|
| 17 |
+
if not name.lower().endswith(".mp4"):
|
| 18 |
+
name += ".mp4"
|
| 19 |
+
return name
|
| 20 |
+
|
| 21 |
+
@app.get("/download/")
|
| 22 |
+
def download_mp4(url: str = Query(..., description="Full Catbox/other file URL")):
|
| 23 |
"""
|
| 24 |
+
Fetch raw MP4 from given URL and return it as direct file response.
|
|
|
|
| 25 |
"""
|
| 26 |
+
try:
|
| 27 |
+
# Stream download
|
| 28 |
+
r = requests.get(url, stream=True, timeout=60)
|
| 29 |
+
r.raise_for_status()
|
| 30 |
+
filename = sanitize_filename(url)
|
| 31 |
+
tmp_path = os.path.join(TEMP_DIR, filename)
|
| 32 |
|
| 33 |
+
# Write bytes to temp file
|
| 34 |
+
with open(tmp_path, "wb") as f:
|
| 35 |
+
shutil.copyfileobj(r.raw, f)
|
| 36 |
|
| 37 |
+
# Serve file
|
| 38 |
+
return FileResponse(
|
| 39 |
+
path=tmp_path,
|
| 40 |
+
filename=filename,
|
| 41 |
+
media_type="video/mp4",
|
| 42 |
+
headers={
|
| 43 |
+
"Content-Disposition": f'inline; filename="{filename}"'
|
| 44 |
+
}
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
except requests.exceptions.RequestException as e:
|
| 48 |
+
raise HTTPException(status_code=400, detail=f"Error fetching file: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|