Neon-AI commited on
Commit
75f5454
·
verified ·
1 Parent(s): 6bf8e04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
app.py CHANGED
@@ -1,35 +1,51 @@
1
  import gradio as gr
 
 
2
  import requests
3
 
4
- def upload_to_litterbox(video_file):
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
- with gr.Blocks() as demo:
28
- gr.Markdown("## Upload Video to Litterbox / Catbox")
29
- video_input = gr.File(label="Upload MP4", file_types=['.mp4'])
30
- output_url = gr.Textbox(label="Litterbox URL")
31
- upload_btn = gr.Button("Upload")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- upload_btn.click(upload_to_litterbox, inputs=video_input, outputs=output_url)
 
 
 
 
 
 
 
 
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()