Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,51 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
Uploads a video file to Litterbox and returns the direct URL.
|
| 7 |
-
"""
|
| 8 |
-
try:
|
| 9 |
-
with open(video_file.name, "rb") as f:
|
| 10 |
-
files = {"fileToUpload": (video_file.name, f, "video/mp4")}
|
| 11 |
-
data = {"reqtype": "fileupload"}
|
| 12 |
-
|
| 13 |
-
resp = requests.post(
|
| 14 |
-
"https://litter.catbox.moe/resources/internals/api.php",
|
| 15 |
-
files=files,
|
| 16 |
-
data=data
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
if resp.status_code == 200 and resp.text.startswith("https://"):
|
| 20 |
-
return resp.text
|
| 21 |
-
else:
|
| 22 |
-
return f"Upload failed: {resp.status_code} - {resp.text}"
|
| 23 |
-
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return f"Error: {e}"
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
from pathlib import Path
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
class LitterboxClient:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
LITTERBOX_API = "https://litterbox.catbox.moe/resources/internals/api.php"
|
| 9 |
+
VALID_TIMES = ["1h", "12h", "24h", "72h"]
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
pass
|
| 13 |
+
|
| 14 |
+
def upload_file(self, filepath: str, expire_time: str = "24h") -> str:
|
| 15 |
+
if expire_time not in self.VALID_TIMES:
|
| 16 |
+
raise ValueError(f"Invalid expire_time '{expire_time}'. Must be one of {self.VALID_TIMES}")
|
| 17 |
+
|
| 18 |
+
with open(filepath, "rb") as f:
|
| 19 |
+
files = {"fileToUpload": f}
|
| 20 |
+
data = {"reqtype": "fileupload", "time": expire_time}
|
| 21 |
+
|
| 22 |
+
response = requests.post(self.LITTERBOX_API, files=files, data=data)
|
| 23 |
+
response.raise_for_status() # This is where 405 will show
|
| 24 |
+
if response.text.startswith("https://"):
|
| 25 |
+
return response.text.strip()
|
| 26 |
+
else:
|
| 27 |
+
raise RuntimeError(f"Unexpected response from Litterbox: {response.text}")
|
| 28 |
+
|
| 29 |
+
client = LitterboxClient()
|
| 30 |
+
|
| 31 |
+
def gradio_upload(video_file):
|
| 32 |
+
if not video_file:
|
| 33 |
+
return "No file provided"
|
| 34 |
+
|
| 35 |
+
path = Path(video_file.name)
|
| 36 |
+
# Save the uploaded file temporarily
|
| 37 |
+
temp_path = tempfile.mktemp(suffix=path.suffix)
|
| 38 |
+
with open(temp_path, "wb") as f:
|
| 39 |
+
f.write(video_file.read())
|
| 40 |
|
| 41 |
+
# Upload using your exact class
|
| 42 |
+
return client.upload_file(temp_path)
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## Test Litterbox Upload (Pure Script)")
|
| 46 |
+
video_input = gr.File(label="Upload Video")
|
| 47 |
+
output = gr.Textbox(label="Uploaded URL / Error")
|
| 48 |
+
run_btn = gr.Button("Upload")
|
| 49 |
+
run_btn.click(gradio_upload, inputs=video_input, outputs=output)
|
| 50 |
|
| 51 |
demo.launch()
|