Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,58 @@
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
-
from fastapi.responses import
|
| 3 |
import yt_dlp
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI(
|
| 6 |
title="TikTok Downloader API",
|
| 7 |
-
|
| 8 |
-
version="1.0"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
"quiet": True,
|
| 13 |
"skip_download": True,
|
| 14 |
"noplaylist": True,
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"author": "Farel",
|
| 22 |
-
"endpoints": {
|
| 23 |
-
"mp4": "/api/mp4?url=https://tiktok.com/...",
|
| 24 |
-
"mp3": "/api/mp3?url=https://tiktok.com/..."
|
| 25 |
-
}
|
| 26 |
-
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
with yt_dlp.YoutubeDL(
|
| 36 |
info = ydl.extract_info(url, download=False)
|
| 37 |
|
| 38 |
video_url = info.get("url")
|
| 39 |
if not video_url:
|
| 40 |
-
raise Exception("
|
| 41 |
|
| 42 |
-
return
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
return JSONResponse(
|
| 46 |
-
status_code=500,
|
| 47 |
-
content={"status": False, "error": str(e)}
|
| 48 |
-
)
|
| 49 |
|
| 50 |
@app.get("/api/mp3")
|
| 51 |
-
def mp3(url: str = Query(...
|
| 52 |
-
opts = YDL_BASE | {
|
| 53 |
-
"format": "bestaudio"
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
try:
|
| 57 |
-
with yt_dlp.YoutubeDL(
|
| 58 |
info = ydl.extract_info(url, download=False)
|
| 59 |
|
| 60 |
audio_url = info.get("url")
|
| 61 |
if not audio_url:
|
| 62 |
-
raise Exception("
|
| 63 |
|
| 64 |
-
return
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
return JSONResponse(
|
| 68 |
-
status_code=500,
|
| 69 |
-
content={"status": False, "error": str(e)}
|
| 70 |
-
)
|
|
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
import yt_dlp
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
app = FastAPI(
|
| 7 |
title="TikTok Downloader API",
|
| 8 |
+
version="1.1"
|
|
|
|
| 9 |
)
|
| 10 |
|
| 11 |
+
YDL_OPTS = {
|
| 12 |
"quiet": True,
|
| 13 |
"skip_download": True,
|
| 14 |
"noplaylist": True,
|
| 15 |
}
|
| 16 |
|
| 17 |
+
HEADERS = {
|
| 18 |
+
"User-Agent": "Mozilla/5.0",
|
| 19 |
+
"Referer": "https://www.tiktok.com/"
|
| 20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def proxy_stream(url: str, content_type: str):
|
| 23 |
+
r = requests.get(url, headers=HEADERS, stream=True, timeout=15)
|
| 24 |
+
r.raise_for_status()
|
| 25 |
+
return StreamingResponse(
|
| 26 |
+
r.iter_content(chunk_size=1024 * 64),
|
| 27 |
+
media_type=content_type
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
@app.get("/api/mp4")
|
| 31 |
+
def mp4(url: str = Query(...)):
|
| 32 |
try:
|
| 33 |
+
with yt_dlp.YoutubeDL(YDL_OPTS | {"format": "best"}) as ydl:
|
| 34 |
info = ydl.extract_info(url, download=False)
|
| 35 |
|
| 36 |
video_url = info.get("url")
|
| 37 |
if not video_url:
|
| 38 |
+
raise Exception("Video URL not found")
|
| 39 |
|
| 40 |
+
return proxy_stream(video_url, "video/mp4")
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
@app.get("/api/mp3")
|
| 46 |
+
def mp3(url: str = Query(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
+
with yt_dlp.YoutubeDL(YDL_OPTS | {"format": "bestaudio"}) as ydl:
|
| 49 |
info = ydl.extract_info(url, download=False)
|
| 50 |
|
| 51 |
audio_url = info.get("url")
|
| 52 |
if not audio_url:
|
| 53 |
+
raise Exception("Audio URL not found")
|
| 54 |
|
| 55 |
+
return proxy_stream(audio_url, "audio/mpeg")
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
|
|