Neon-AI commited on
Commit
a9b9784
·
verified ·
1 Parent(s): 7bf21f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -31
app.py CHANGED
@@ -1,41 +1,48 @@
1
- from fastapi import FastAPI, HTTPException, Query
2
- from fastapi.middleware.cors import CORSMiddleware
3
  import requests
 
 
 
4
  import re
5
 
6
- app = FastAPI(title="Catbox / Litterbox Raw File Extractor API")
7
 
8
- # Allow cross-origin calls (optional, needed if frontend calls it)
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"],
12
- allow_methods=["*"],
13
- allow_headers=["*"],
14
- )
15
 
16
- @app.get("/api/extract")
17
- def extract_raw_file(url: str = Query(..., description="Full Catbox/Litterbox URL")):
 
 
 
 
 
 
 
 
18
  """
19
- Extracts a direct raw file link from a Catbox/Litterbox URL.
20
- Returns JSON with `direct_link` if successful.
21
  """
22
- if not url.startswith("http"):
23
- raise HTTPException(status_code=400, detail="Invalid URL")
 
 
 
 
24
 
25
- # If it already points to a raw file, return as-is
26
- if re.search(r'\.(mp4|png|jpg|jpeg|gif|webm)$', url, re.IGNORECASE):
27
- return {"ok": True, "direct_link": url}
28
 
29
- # Otherwise, fetch page and try to extract the direct file
30
- try:
31
- resp = requests.get(url, timeout=10)
32
- resp.raise_for_status()
33
- html = resp.text
 
 
 
 
34
 
35
- match = re.search(r'src="([^"]+\.(mp4|png|jpg|jpeg|gif|webm))"', html, re.IGNORECASE)
36
- if match:
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)}")