Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import uuid
|
|
| 7 |
from fastapi import FastAPI, BackgroundTasks
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import FileResponse, JSONResponse
|
|
|
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
app = FastAPI()
|
|
@@ -36,7 +37,8 @@ def init_db():
|
|
| 36 |
conn.commit()
|
| 37 |
|
| 38 |
# --- Background Re-encoding Task ---
|
| 39 |
-
|
|
|
|
| 40 |
db = get_db()
|
| 41 |
temp_in_path = os.path.join(DOWNLOAD_DIR, f"{task_id}_in.mp4")
|
| 42 |
temp_out_path = os.path.join(ENCODED_DIR, f"{task_id}_out.mp4")
|
|
@@ -46,7 +48,16 @@ def process_video_task(url: str, task_id: str):
|
|
| 46 |
db.execute("UPDATE jobs SET status = 'downloading' WHERE task_id = ?", (task_id,))
|
| 47 |
db.commit()
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
r.raise_for_status()
|
| 51 |
with open(temp_in_path, 'wb') as f:
|
| 52 |
for chunk in r.iter_bytes(chunk_size=8192):
|
|
@@ -84,6 +95,7 @@ def process_video_task(url: str, task_id: str):
|
|
| 84 |
# --- API Endpoints ---
|
| 85 |
class VideoRequest(BaseModel):
|
| 86 |
url: str
|
|
|
|
| 87 |
|
| 88 |
@app.post("/process")
|
| 89 |
async def start_processing(request: VideoRequest, background_tasks: BackgroundTasks):
|
|
@@ -94,7 +106,8 @@ async def start_processing(request: VideoRequest, background_tasks: BackgroundTa
|
|
| 94 |
db.execute("INSERT INTO jobs (task_id, status) VALUES (?, 'queued')", (task_id,))
|
| 95 |
db.commit()
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
return {"status": "queued", "task_id": task_id}
|
| 99 |
|
| 100 |
@app.get("/status/{task_id}")
|
|
@@ -117,9 +130,6 @@ async def download_file(file_name: str):
|
|
| 117 |
|
| 118 |
return FileResponse(file_path, media_type='video/mp4', filename=file_name)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
# β
β
β
FIX IS HERE β
β
β
|
| 122 |
-
#
|
| 123 |
@app.get("/")
|
| 124 |
async def health_check():
|
| 125 |
""" Health check endpoint to fix 404 errors in log. """
|
|
@@ -134,3 +144,4 @@ async def startup_event():
|
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
| 7 |
from fastapi import FastAPI, BackgroundTasks
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import FileResponse, JSONResponse
|
| 10 |
+
from typing import Optional # <-- Import Optional
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
app = FastAPI()
|
|
|
|
| 37 |
conn.commit()
|
| 38 |
|
| 39 |
# --- Background Re-encoding Task ---
|
| 40 |
+
# β
β
β
FIX: Accept 'cookie' as an argument
|
| 41 |
+
def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
| 42 |
db = get_db()
|
| 43 |
temp_in_path = os.path.join(DOWNLOAD_DIR, f"{task_id}_in.mp4")
|
| 44 |
temp_out_path = os.path.join(ENCODED_DIR, f"{task_id}_out.mp4")
|
|
|
|
| 48 |
db.execute("UPDATE jobs SET status = 'downloading' WHERE task_id = ?", (task_id,))
|
| 49 |
db.commit()
|
| 50 |
|
| 51 |
+
# β
β
β
FIX: Add browser headers
|
| 52 |
+
headers = {
|
| 53 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
|
| 54 |
+
"Referer": "https://terabox.com/"
|
| 55 |
+
}
|
| 56 |
+
if cookie:
|
| 57 |
+
headers["Cookie"] = cookie # Add the cookie
|
| 58 |
+
|
| 59 |
+
# β
β
β
FIX: Use the 'headers' when downloading
|
| 60 |
+
with httpx.stream("GET", url, headers=headers, follow_redirects=True, timeout=600.0) as r:
|
| 61 |
r.raise_for_status()
|
| 62 |
with open(temp_in_path, 'wb') as f:
|
| 63 |
for chunk in r.iter_bytes(chunk_size=8192):
|
|
|
|
| 95 |
# --- API Endpoints ---
|
| 96 |
class VideoRequest(BaseModel):
|
| 97 |
url: str
|
| 98 |
+
cookie: Optional[str] = None # β
β
β
FIX: Accept 'cookie'
|
| 99 |
|
| 100 |
@app.post("/process")
|
| 101 |
async def start_processing(request: VideoRequest, background_tasks: BackgroundTasks):
|
|
|
|
| 106 |
db.execute("INSERT INTO jobs (task_id, status) VALUES (?, 'queued')", (task_id,))
|
| 107 |
db.commit()
|
| 108 |
|
| 109 |
+
# β
β
β
FIX: Pass the cookie to the background task
|
| 110 |
+
background_tasks.add_task(process_video_task, request.url, request.cookie, task_id)
|
| 111 |
return {"status": "queued", "task_id": task_id}
|
| 112 |
|
| 113 |
@app.get("/status/{task_id}")
|
|
|
|
| 130 |
|
| 131 |
return FileResponse(file_path, media_type='video/mp4', filename=file_name)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
@app.get("/")
|
| 134 |
async def health_check():
|
| 135 |
""" Health check endpoint to fix 404 errors in log. """
|
|
|
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 147 |
+
|