mojaalagevai commited on
Commit
84a08ce
·
verified ·
1 Parent(s): e286679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -33
app.py CHANGED
@@ -1,9 +1,9 @@
1
  from fastapi import FastAPI, HTTPException, Security, Depends
2
  from fastapi.security import APIKeyHeader
3
- from pydantic import HttpUrl, BaseModel
4
  import yt_dlp
5
  import uvicorn
6
- from typing import List, Optional
7
  from datetime import timedelta
8
  import os
9
  import logging
@@ -31,22 +31,7 @@ async def get_api_key(api_key_header: Optional[str] = Security(api_key_header)):
31
  # Path to cookies file (optional)
32
  COOKIES_FILE = "cookies.txt"
33
 
34
- # Pydantic models for structured response
35
- class VideoLink(BaseModel):
36
- quality: str
37
- url: str
38
- format_id: str
39
- type: str # "direct" or "streaming"
40
-
41
- class VideoInfoResponse(BaseModel):
42
- title: str
43
- duration: str
44
- file_size: str
45
- download_links: List[VideoLink]
46
- thumbnail: str
47
- link_type: str
48
-
49
- @app.get("/video-info", response_model=VideoInfoResponse)
50
  async def get_video_info(video_url: HttpUrl, api_key: str = Depends(get_api_key)):
51
  """
52
  Extract direct download links for video-only MP4 (720p and higher) or streaming links if no direct links are available,
@@ -111,7 +96,7 @@ async def get_video_info(video_url: HttpUrl, api_key: str = Depends(get_api_key)
111
  # If no direct formats, fall back to streaming formats (720p or higher)
112
  if not direct_formats:
113
  logger.warning(f"No direct MP4 video-only formats (720p+) found for {video_url_str}, falling back to streaming formats")
114
- streaming FORMATS = [
115
  f for f in info.get('formats', [])
116
  if f.get('vcodec') != 'none'
117
  and f.get('acodec') == 'none'
@@ -134,12 +119,12 @@ async def get_video_info(video_url: HttpUrl, api_key: str = Depends(get_api_key)
134
 
135
  # Get download links
136
  download_links = [
137
- VideoLink(
138
- quality=f"{f.get('height')}p",
139
- url=f.get('url'),
140
- format_id=f.get('format_id'),
141
- type=link_type
142
- )
143
  for f in formats
144
  ]
145
 
@@ -152,14 +137,14 @@ async def get_video_info(video_url: HttpUrl, api_key: str = Depends(get_api_key)
152
  ).get('url', 'No thumbnail available')
153
 
154
  logger.info(f"Successfully extracted info for {video_url_str}: {len(download_links)} {link_type} formats found")
155
- return VideoInfoResponse(
156
- title=title,
157
- duration=duration_str,
158
- file_size=file_size_str,
159
- download_links=download_links,
160
- thumbnail=best_thumbnail,
161
- link_type=link_type
162
- )
163
 
164
  except HTTPException as he:
165
  raise he
 
1
  from fastapi import FastAPI, HTTPException, Security, Depends
2
  from fastapi.security import APIKeyHeader
3
+ from pydantic import HttpUrl
4
  import yt_dlp
5
  import uvicorn
6
+ from typing import Dict, Optional
7
  from datetime import timedelta
8
  import os
9
  import logging
 
31
  # Path to cookies file (optional)
32
  COOKIES_FILE = "cookies.txt"
33
 
34
+ @app.get("/video-info", response_model=Dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  async def get_video_info(video_url: HttpUrl, api_key: str = Depends(get_api_key)):
36
  """
37
  Extract direct download links for video-only MP4 (720p and higher) or streaming links if no direct links are available,
 
96
  # If no direct formats, fall back to streaming formats (720p or higher)
97
  if not direct_formats:
98
  logger.warning(f"No direct MP4 video-only formats (720p+) found for {video_url_str}, falling back to streaming formats")
99
+ streaming_formats = [
100
  f for f in info.get('formats', [])
101
  if f.get('vcodec') != 'none'
102
  and f.get('acodec') == 'none'
 
119
 
120
  # Get download links
121
  download_links = [
122
+ {
123
+ "quality": f"{f.get('height')}p",
124
+ "url": f.get('url'),
125
+ "format_id": f.get('format_id'),
126
+ "type": link_type
127
+ }
128
  for f in formats
129
  ]
130
 
 
137
  ).get('url', 'No thumbnail available')
138
 
139
  logger.info(f"Successfully extracted info for {video_url_str}: {len(download_links)} {link_type} formats found")
140
+ return {
141
+ "title": title,
142
+ "duration": duration_str,
143
+ "file_size": file_size_str,
144
+ "download_links": download_links,
145
+ "thumbnail": best_thumbnail,
146
+ "link_type": link_type
147
+ }
148
 
149
  except HTTPException as he:
150
  raise he