Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
r = requests.post(UPLOAD_URL, files={"file": (filename, f)}, timeout=180)
|
| 16 |
r.raise_for_status()
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
return
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import re
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
|
| 7 |
+
# Gradio + FastAPI backend
|
| 8 |
+
app = FastAPI(title="Catbox Raw File Extractor API")
|
| 9 |
|
| 10 |
+
# Allow all origins (for frontend)
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def get_direct_link(catbox_url: str):
|
| 19 |
+
"""Extract direct raw file from a Catbox/Litterbox URL"""
|
| 20 |
+
if not catbox_url.startswith("http"):
|
| 21 |
+
return {"ok": False, "error": "Invalid URL"}
|
| 22 |
+
|
| 23 |
+
# If it already ends with a raw file extension, return as-is
|
| 24 |
+
if re.search(r'\.(mp4|png|jpg|jpeg|gif|webm)$', catbox_url, re.IGNORECASE):
|
| 25 |
+
return {"ok": True, "direct_link": catbox_url}
|
| 26 |
+
|
| 27 |
+
# Otherwise, try to fetch page and extract
|
| 28 |
try:
|
| 29 |
+
r = requests.get(catbox_url, timeout=10)
|
|
|
|
| 30 |
r.raise_for_status()
|
| 31 |
+
text = r.text
|
| 32 |
+
|
| 33 |
+
match = re.search(r'src="([^"]+\.(mp4|png|jpg|jpeg|gif|webm))"', text, re.IGNORECASE)
|
| 34 |
+
if match:
|
| 35 |
+
return {"ok": True, "direct_link": match.group(1)}
|
| 36 |
+
else:
|
| 37 |
+
return {"ok": False, "error": "Could not find raw file link"}
|
| 38 |
except Exception as e:
|
| 39 |
+
return {"ok": False, "error": str(e)}
|
| 40 |
+
|
| 41 |
+
# Gradio API interface
|
| 42 |
+
with gr.Blocks(server_name="0.0.0.0") as demo:
|
| 43 |
+
gr.Markdown("## Catbox / Litterbox Raw File Extractor API")
|
| 44 |
+
input_url = gr.Textbox(label="Paste Catbox/Litterbox URL")
|
| 45 |
+
output_json = gr.JSON(label="API Response")
|
| 46 |
+
extract_btn = gr.Button("Get Direct Link")
|
| 47 |
+
extract_btn.click(fn=get_direct_link, inputs=input_url, outputs=output_json)
|
| 48 |
|
| 49 |
+
# Mount Gradio app onto FastAPI
|
| 50 |
+
app = gr.mount_gradio_app(app, demo, path="/api")
|