Neon-AI commited on
Commit
8e8728a
·
verified ·
1 Parent(s): 6af0541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,46 +1,50 @@
1
  # main.py
2
- import requests
3
- import tempfile
4
  import shutil
5
- import os
6
- from fastapi import FastAPI, Query
 
7
  from fastapi.responses import FileResponse
 
8
  from urllib.parse import urlparse
9
 
10
- app = FastAPI(title="Neon AI RawLink Proxy")
11
-
12
- TEMP_DIR = tempfile.gettempdir()
13
-
14
- def safe_filename(url: str) -> str:
15
- """Generate a safe filename from URL."""
16
- parsed = urlparse(url)
17
- name = os.path.basename(parsed.path)
18
- if not name.lower().endswith(".mp4"):
19
- name += ".mp4"
20
- return name
21
 
22
  @app.get("/download")
23
- def download_endpoint(url: str = Query(..., description="Catbox/Litter video URL")):
24
  """
25
- Accepts a video URL, downloads it temporarily,
26
- and serves it with proper headers so Telegram sees a download.
27
  """
28
- try:
29
- filename = safe_filename(url)
30
- tmp_path = os.path.join(TEMP_DIR, filename)
 
31
 
 
 
 
 
 
 
 
 
 
32
  # Download the video
33
  with requests.get(url, stream=True, timeout=60) as r:
34
  r.raise_for_status()
35
- with open(tmp_path, "wb") as f:
36
  shutil.copyfileobj(r.raw, f)
37
 
38
- # Serve file with download headers
39
  return FileResponse(
40
- tmp_path,
41
  media_type="application/octet-stream",
42
- filename=filename,
 
43
  )
44
 
45
  except Exception as e:
46
- return {"ok": False, "error": str(e)}
 
 
 
 
1
  # main.py
 
 
2
  import shutil
3
+ import tempfile
4
+ import requests
5
+ from fastapi import FastAPI, Query, HTTPException
6
  from fastapi.responses import FileResponse
7
+ import os
8
  from urllib.parse import urlparse
9
 
10
+ app = FastAPI(title="Telegram-Friendly Video Proxy")
 
 
 
 
 
 
 
 
 
 
11
 
12
  @app.get("/download")
13
+ def download_video(url: str = Query(..., description="Public video URL")):
14
  """
15
+ Downloads a video from a streaming/HTML-wrapped URL and serves it
16
+ as a raw file Telegram can accept.
17
  """
18
+ # Validate URL
19
+ parsed = urlparse(url)
20
+ if not parsed.scheme.startswith("http"):
21
+ raise HTTPException(status_code=400, detail="Invalid URL")
22
 
23
+ # Extract file extension for naming
24
+ _, ext = os.path.splitext(parsed.path)
25
+ if ext.lower() not in [".mp4", ".mkv", ".webm", ".mov", ".avi"]:
26
+ ext = ".mp4" # default fallback
27
+
28
+ # Temp file to download
29
+ tmp_file = tempfile.mktemp(suffix=ext)
30
+
31
+ try:
32
  # Download the video
33
  with requests.get(url, stream=True, timeout=60) as r:
34
  r.raise_for_status()
35
+ with open(tmp_file, "wb") as f:
36
  shutil.copyfileobj(r.raw, f)
37
 
38
+ # Serve with Content-Disposition so Telegram sees it as a file
39
  return FileResponse(
40
+ tmp_file,
41
  media_type="application/octet-stream",
42
+ filename=f"video{ext}",
43
+ headers={"Content-Disposition": f'attachment; filename="video{ext}"'}
44
  )
45
 
46
  except Exception as e:
47
+ # Clean up in case of failure
48
+ if os.path.exists(tmp_file):
49
+ os.remove(tmp_file)
50
+ raise HTTPException(status_code=500, detail=f"Failed to download file: {repr(e)}")