dragxd commited on
Commit
65b6150
·
verified ·
1 Parent(s): 20cfce6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -1,15 +1,36 @@
1
  from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
  import yt_dlp
4
  import base64
5
 
6
- app = FastAPI()
 
 
 
 
 
 
 
 
 
7
 
8
  class VideoRequest(BaseModel):
9
- video_id: str
 
 
 
 
10
 
11
- @app.post("/audio/")
 
 
 
 
12
  def get_audio_link(req: VideoRequest):
 
 
 
 
13
  url = f"https://www.youtube.com/watch?v={req.video_id}"
14
  ydl_opts = {
15
  'quiet': True,
@@ -17,7 +38,6 @@ def get_audio_link(req: VideoRequest):
17
  'skip_download': True,
18
  'forceurl': True,
19
  }
20
-
21
  try:
22
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
23
  info = ydl.extract_info(url, download=False)
@@ -30,8 +50,12 @@ def get_audio_link(req: VideoRequest):
30
  except Exception as e:
31
  raise HTTPException(status_code=400, detail=str(e))
32
 
33
- @app.post("/video/")
34
  def get_video_link(req: VideoRequest):
 
 
 
 
35
  url = f"https://www.youtube.com/watch?v={req.video_id}"
36
  ydl_opts = {
37
  'quiet': True,
 
1
  from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel, Field
3
  import yt_dlp
4
  import base64
5
 
6
+ app = FastAPI(
7
+ title="YouTube Download API",
8
+ description="API for retrieving YouTube audio/video download links using yt-dlp.",
9
+ version="1.0.0",
10
+ contact={
11
+ "name": "Your Name",
12
+ "url": "https://github.com/yourusername",
13
+ "email": "[email protected]",
14
+ }
15
+ )
16
 
17
  class VideoRequest(BaseModel):
18
+ video_id: str = Field(..., example="dQw4w9WgXcQ", description="YouTube video ID (e.g., dQw4w9WgXcQ)")
19
+
20
+ class AudioResponse(BaseModel):
21
+ status: str = Field(..., example="success")
22
+ audio_url: str = Field(..., description="Base64 encoded direct audio URL")
23
 
24
+ class VideoResponse(BaseModel):
25
+ status: str = Field(..., example="success")
26
+ video_url: str = Field(..., description="Base64 encoded direct video URL")
27
+
28
+ @app.post("/audio/", response_model=AudioResponse, summary="Get YouTube Audio URL")
29
  def get_audio_link(req: VideoRequest):
30
+ """
31
+ Returns a downloadable audio link for a given YouTube video ID.
32
+ The URL is base64-encoded.
33
+ """
34
  url = f"https://www.youtube.com/watch?v={req.video_id}"
35
  ydl_opts = {
36
  'quiet': True,
 
38
  'skip_download': True,
39
  'forceurl': True,
40
  }
 
41
  try:
42
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
43
  info = ydl.extract_info(url, download=False)
 
50
  except Exception as e:
51
  raise HTTPException(status_code=400, detail=str(e))
52
 
53
+ @app.post("/video/", response_model=VideoResponse, summary="Get YouTube Video URL")
54
  def get_video_link(req: VideoRequest):
55
+ """
56
+ Returns a downloadable video link (max 720p) for a given YouTube video ID.
57
+ The URL is base64-encoded.
58
+ """
59
  url = f"https://www.youtube.com/watch?v={req.video_id}"
60
  ydl_opts = {
61
  'quiet': True,