FarelDeveloper commited on
Commit
ac7affe
·
verified ·
1 Parent(s): 8f078f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -1,70 +1,58 @@
1
  from fastapi import FastAPI, Query
2
- from fastapi.responses import RedirectResponse, JSONResponse
3
  import yt_dlp
 
4
 
5
  app = FastAPI(
6
  title="TikTok Downloader API",
7
- description="Streaming TikTok MP3 & MP4 (HF Space)",
8
- version="1.0"
9
  )
10
 
11
- YDL_BASE = {
12
  "quiet": True,
13
  "skip_download": True,
14
  "noplaylist": True,
15
  }
16
 
17
- @app.get("/")
18
- def home():
19
- return {
20
- "status": True,
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
- @app.get("/api/mp4")
29
- def mp4(url: str = Query(..., description="TikTok video URL")):
30
- opts = YDL_BASE | {
31
- "format": "best"
32
- }
 
 
33
 
 
 
34
  try:
35
- with yt_dlp.YoutubeDL(opts) as ydl:
36
  info = ydl.extract_info(url, download=False)
37
 
38
  video_url = info.get("url")
39
  if not video_url:
40
- raise Exception("MP4 URL not found")
41
 
42
- return RedirectResponse(video_url)
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(..., description="TikTok video URL")):
52
- opts = YDL_BASE | {
53
- "format": "bestaudio"
54
- }
55
-
56
  try:
57
- with yt_dlp.YoutubeDL(opts) as ydl:
58
  info = ydl.extract_info(url, download=False)
59
 
60
  audio_url = info.get("url")
61
  if not audio_url:
62
- raise Exception("MP3 URL not found")
63
 
64
- return RedirectResponse(audio_url)
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)})