Spaces:
Paused
Paused
add more logs
Browse files- app/main.py +4 -9
- app/utils.py +19 -11
app/main.py
CHANGED
|
@@ -17,21 +17,16 @@ def health_check():
|
|
| 17 |
return {"status": "ok", "message": "YouTube Downloader API is running."}
|
| 18 |
|
| 19 |
@app.post("/download")
|
| 20 |
-
async def download(payload: DownloadRequest):
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
return FileResponse(
|
| 26 |
-
path=video_path,
|
| 27 |
-
filename=filename,
|
| 28 |
-
media_type="video/mp4"
|
| 29 |
-
)
|
| 30 |
except Exception as e:
|
| 31 |
print("🔥 API download error:", str(e))
|
| 32 |
raise HTTPException(status_code=500, detail=str(e))
|
| 33 |
|
| 34 |
-
|
| 35 |
@app.get("/video/{video_id}")
|
| 36 |
def get_video(video_id: str, background_tasks: BackgroundTasks):
|
| 37 |
filepath = f"/tmp/{video_id}.mp4"
|
|
|
|
| 17 |
return {"status": "ok", "message": "YouTube Downloader API is running."}
|
| 18 |
|
| 19 |
@app.post("/download")
|
| 20 |
+
async def download(payload: DownloadRequest):
|
| 21 |
try:
|
| 22 |
+
video_id = str(uuid.uuid4())
|
| 23 |
+
video_path = download_video(payload.url, payload.cookie, video_id=video_id)
|
| 24 |
|
| 25 |
+
return {"download_url": f"/video/{video_id}.mp4"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print("🔥 API download error:", str(e))
|
| 28 |
raise HTTPException(status_code=500, detail=str(e))
|
| 29 |
|
|
|
|
| 30 |
@app.get("/video/{video_id}")
|
| 31 |
def get_video(video_id: str, background_tasks: BackgroundTasks):
|
| 32 |
filepath = f"/tmp/{video_id}.mp4"
|
app/utils.py
CHANGED
|
@@ -2,9 +2,8 @@ import yt_dlp
|
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
def download_video(url: str, cookie: str = None) -> str:
|
| 6 |
-
|
| 7 |
-
output_path = f"/tmp/{safe_filename}.mp4"
|
| 8 |
|
| 9 |
ydl_opts = {
|
| 10 |
"format": "best[ext=mp4]/best",
|
|
@@ -13,12 +12,22 @@ def download_video(url: str, cookie: str = None) -> str:
|
|
| 13 |
"noplaylist": True,
|
| 14 |
}
|
| 15 |
|
|
|
|
|
|
|
| 16 |
if cookie:
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
cookie_file
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
print(f"📥 Downloading video from {url} to {output_path}")
|
|
@@ -28,13 +37,12 @@ def download_video(url: str, cookie: str = None) -> str:
|
|
| 28 |
if not os.path.exists(output_path):
|
| 29 |
raise RuntimeError("Download finished but output file not found.")
|
| 30 |
|
| 31 |
-
print(f"✅ Video downloaded successfully: {output_path}")
|
| 32 |
return output_path
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
print("🔥 Download failed:", str(e))
|
| 36 |
raise
|
| 37 |
finally:
|
| 38 |
-
if
|
| 39 |
-
os.remove(cookie_file)
|
| 40 |
print("🧹 Temporary cookie file removed.")
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
def download_video(url: str, cookie: str = None, video_id: str = "video") -> str:
|
| 6 |
+
output_path = f"/tmp/{video_id}.mp4"
|
|
|
|
| 7 |
|
| 8 |
ydl_opts = {
|
| 9 |
"format": "best[ext=mp4]/best",
|
|
|
|
| 12 |
"noplaylist": True,
|
| 13 |
}
|
| 14 |
|
| 15 |
+
cookie_file = None
|
| 16 |
+
|
| 17 |
if cookie:
|
| 18 |
+
# Convert raw cookie string to Netscape format
|
| 19 |
+
try:
|
| 20 |
+
cookie_file = tempfile.NamedTemporaryFile(delete=False, mode="w", suffix=".txt")
|
| 21 |
+
cookie_file.write("# Netscape HTTP Cookie File\n")
|
| 22 |
+
for pair in cookie.split(";"):
|
| 23 |
+
parts = pair.strip().split("=")
|
| 24 |
+
if len(parts) == 2:
|
| 25 |
+
name, value = parts
|
| 26 |
+
cookie_file.write(f".youtube.com\tTRUE\t/\tFALSE\t2145916800\t{name}\t{value}\n")
|
| 27 |
+
cookie_file.close()
|
| 28 |
+
ydl_opts["cookiefile"] = cookie_file.name
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print("🔥 Failed to process cookies:", str(e))
|
| 31 |
|
| 32 |
try:
|
| 33 |
print(f"📥 Downloading video from {url} to {output_path}")
|
|
|
|
| 37 |
if not os.path.exists(output_path):
|
| 38 |
raise RuntimeError("Download finished but output file not found.")
|
| 39 |
|
|
|
|
| 40 |
return output_path
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
print("🔥 Download failed:", str(e))
|
| 44 |
raise
|
| 45 |
finally:
|
| 46 |
+
if cookie_file and os.path.exists(cookie_file.name):
|
| 47 |
+
os.remove(cookie_file.name)
|
| 48 |
print("🧹 Temporary cookie file removed.")
|