Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,11 @@ from fastapi import FastAPI, HTTPException, status, Request
|
|
| 8 |
from yt_dlp import YoutubeDL
|
| 9 |
from yt_dlp.version import __version__ as yt_dlp_version
|
| 10 |
from typing import Union, Dict
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(docs_url=None, redoc_url=None)
|
| 13 |
|
| 14 |
-
# Set cache directory to /tmp which is writable in
|
| 15 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 16 |
|
| 17 |
# Rate limiting configuration
|
|
@@ -110,7 +111,7 @@ async def version_info():
|
|
| 110 |
|
| 111 |
@app.get('/')
|
| 112 |
def main():
|
| 113 |
-
return "Chrunos Downloader API Is Running well."
|
| 114 |
|
| 115 |
@app.get("/api/info")
|
| 116 |
async def get_info(
|
|
@@ -169,8 +170,6 @@ async def get_info(
|
|
| 169 |
headers={"Cache-Control": "no-store, max-age=0"},
|
| 170 |
)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
@app.get("/api/playlist")
|
| 175 |
async def get_playlist_info(
|
| 176 |
request: Request,
|
|
@@ -182,20 +181,16 @@ async def get_playlist_info(
|
|
| 182 |
Fetches paginated items from a playlist or user profile.
|
| 183 |
Strictly enforces a maximum of 50 items per request and provides a next_page URL.
|
| 184 |
"""
|
| 185 |
-
# 1. Validate inputs
|
| 186 |
if start < 1:
|
| 187 |
raise HTTPException(status_code=400, detail="'start' must be 1 or greater.")
|
| 188 |
if end < start:
|
| 189 |
raise HTTPException(status_code=400, detail="'end' must be greater than or equal to 'start'.")
|
| 190 |
|
| 191 |
-
# 2. Enforce a hard limit of 50 items per request
|
| 192 |
-
# If a client requests start=51 & end=1000, we force end=100.
|
| 193 |
requested_count = end - start + 1
|
| 194 |
if requested_count > 50:
|
| 195 |
end = start + 49
|
| 196 |
requested_count = 50
|
| 197 |
|
| 198 |
-
# Rate Limiting
|
| 199 |
client_ip = get_client_ip(request)
|
| 200 |
is_allowed, remaining = check_rate_limit(client_ip)
|
| 201 |
|
|
@@ -219,7 +214,6 @@ async def get_playlist_info(
|
|
| 219 |
"ignoreerrors": True,
|
| 220 |
"cachedir": "/tmp/yt-dlp-cache",
|
| 221 |
"js-runtimes": "node",
|
| 222 |
-
# Pass the strictly clamped pagination settings to yt-dlp
|
| 223 |
"playliststart": start,
|
| 224 |
"playlistend": end
|
| 225 |
}
|
|
@@ -230,26 +224,15 @@ async def get_playlist_info(
|
|
| 230 |
if not response:
|
| 231 |
raise HTTPException(status_code=404, detail="Playlist or profile not found.")
|
| 232 |
|
| 233 |
-
# Extract entries. yt-dlp might omit 'entries' if out of bounds, so default to empty list.
|
| 234 |
raw_entries = response.get("entries") or []
|
| 235 |
-
|
| 236 |
-
# Filter out 'None' entries (yt-dlp sometimes returns None for deleted/private videos in a playlist)
|
| 237 |
valid_entries = [e for e in raw_entries if e is not None]
|
| 238 |
|
| 239 |
-
# 3. Determine if there is a next page
|
| 240 |
next_page_url = None
|
| 241 |
-
|
| 242 |
-
# If yt-dlp found enough items to fill our requested quota, there is likely a next page.
|
| 243 |
-
# (Note: we check the length of raw_entries because deleted videos still consume an index slot)
|
| 244 |
if len(raw_entries) >= requested_count:
|
| 245 |
next_start = end + 1
|
| 246 |
next_end = next_start + 49
|
| 247 |
-
|
| 248 |
-
# Safely encode the target URL
|
| 249 |
encoded_url = urllib.parse.quote(url)
|
| 250 |
base_url = str(request.base_url).rstrip('/')
|
| 251 |
-
|
| 252 |
-
# Construct the ready-to-use next_page URL for the client
|
| 253 |
next_page_url = f"{base_url}/api/playlist?url={encoded_url}&start={next_start}&end={next_end}"
|
| 254 |
|
| 255 |
clean_response = {
|
|
@@ -278,10 +261,8 @@ async def get_playlist_info(
|
|
| 278 |
headers={"Cache-Control": "no-store, max-age=0"},
|
| 279 |
)
|
| 280 |
|
| 281 |
-
|
| 282 |
@app.get("/api/rate-limit-status")
|
| 283 |
async def get_rate_limit_status(request: Request):
|
| 284 |
-
"""Endpoint to check current rate limit status for the requesting IP"""
|
| 285 |
client_ip = get_client_ip(request)
|
| 286 |
rate_limits = load_rate_limits()
|
| 287 |
rate_limits = cleanup_old_entries(rate_limits)
|
|
@@ -300,4 +281,8 @@ async def get_rate_limit_status(request: Request):
|
|
| 300 |
"used": used,
|
| 301 |
"remaining": remaining,
|
| 302 |
"reset_time": f"{current_date}T00:00:00Z"
|
| 303 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from yt_dlp import YoutubeDL
|
| 9 |
from yt_dlp.version import __version__ as yt_dlp_version
|
| 10 |
from typing import Union, Dict
|
| 11 |
+
import uvicorn
|
| 12 |
|
| 13 |
app = FastAPI(docs_url=None, redoc_url=None)
|
| 14 |
|
| 15 |
+
# Set cache directory to /tmp which is writable in Hugging Face containers
|
| 16 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 17 |
|
| 18 |
# Rate limiting configuration
|
|
|
|
| 111 |
|
| 112 |
@app.get('/')
|
| 113 |
def main():
|
| 114 |
+
return "Chrunos Downloader API Is Running well on Hugging Face."
|
| 115 |
|
| 116 |
@app.get("/api/info")
|
| 117 |
async def get_info(
|
|
|
|
| 170 |
headers={"Cache-Control": "no-store, max-age=0"},
|
| 171 |
)
|
| 172 |
|
|
|
|
|
|
|
| 173 |
@app.get("/api/playlist")
|
| 174 |
async def get_playlist_info(
|
| 175 |
request: Request,
|
|
|
|
| 181 |
Fetches paginated items from a playlist or user profile.
|
| 182 |
Strictly enforces a maximum of 50 items per request and provides a next_page URL.
|
| 183 |
"""
|
|
|
|
| 184 |
if start < 1:
|
| 185 |
raise HTTPException(status_code=400, detail="'start' must be 1 or greater.")
|
| 186 |
if end < start:
|
| 187 |
raise HTTPException(status_code=400, detail="'end' must be greater than or equal to 'start'.")
|
| 188 |
|
|
|
|
|
|
|
| 189 |
requested_count = end - start + 1
|
| 190 |
if requested_count > 50:
|
| 191 |
end = start + 49
|
| 192 |
requested_count = 50
|
| 193 |
|
|
|
|
| 194 |
client_ip = get_client_ip(request)
|
| 195 |
is_allowed, remaining = check_rate_limit(client_ip)
|
| 196 |
|
|
|
|
| 214 |
"ignoreerrors": True,
|
| 215 |
"cachedir": "/tmp/yt-dlp-cache",
|
| 216 |
"js-runtimes": "node",
|
|
|
|
| 217 |
"playliststart": start,
|
| 218 |
"playlistend": end
|
| 219 |
}
|
|
|
|
| 224 |
if not response:
|
| 225 |
raise HTTPException(status_code=404, detail="Playlist or profile not found.")
|
| 226 |
|
|
|
|
| 227 |
raw_entries = response.get("entries") or []
|
|
|
|
|
|
|
| 228 |
valid_entries = [e for e in raw_entries if e is not None]
|
| 229 |
|
|
|
|
| 230 |
next_page_url = None
|
|
|
|
|
|
|
|
|
|
| 231 |
if len(raw_entries) >= requested_count:
|
| 232 |
next_start = end + 1
|
| 233 |
next_end = next_start + 49
|
|
|
|
|
|
|
| 234 |
encoded_url = urllib.parse.quote(url)
|
| 235 |
base_url = str(request.base_url).rstrip('/')
|
|
|
|
|
|
|
| 236 |
next_page_url = f"{base_url}/api/playlist?url={encoded_url}&start={next_start}&end={next_end}"
|
| 237 |
|
| 238 |
clean_response = {
|
|
|
|
| 261 |
headers={"Cache-Control": "no-store, max-age=0"},
|
| 262 |
)
|
| 263 |
|
|
|
|
| 264 |
@app.get("/api/rate-limit-status")
|
| 265 |
async def get_rate_limit_status(request: Request):
|
|
|
|
| 266 |
client_ip = get_client_ip(request)
|
| 267 |
rate_limits = load_rate_limits()
|
| 268 |
rate_limits = cleanup_old_entries(rate_limits)
|
|
|
|
| 281 |
"used": used,
|
| 282 |
"remaining": remaining,
|
| 283 |
"reset_time": f"{current_date}T00:00:00Z"
|
| 284 |
+
})
|
| 285 |
+
|
| 286 |
+
# Add the Hugging Face compatible execution block
|
| 287 |
+
if __name__ == "__main__":
|
| 288 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|