Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -84,7 +84,7 @@ cache = TTLCache(CACHE_TTL, MAX_CACHE_ITEMS)
|
|
| 84 |
queue = Queue(MAX_CONCURRENT)
|
| 85 |
|
| 86 |
# โโโ yt-dlp helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 87 |
-
|
| 88 |
"quiet": True,
|
| 89 |
"no_warnings": True,
|
| 90 |
"skip_download": True,
|
|
@@ -102,187 +102,215 @@ def _size(b) -> str:
|
|
| 102 |
b /= 1024
|
| 103 |
return f"{b:.1f} GB"
|
| 104 |
|
| 105 |
-
def _formats(fmts: list
|
| 106 |
"""
|
| 107 |
-
|
| 108 |
-
|
| 109 |
"""
|
| 110 |
-
result
|
|
|
|
| 111 |
|
| 112 |
-
# ูุตู ุงูููุฑู
ุงุช
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
for f in fmts:
|
| 118 |
vcodec = f.get("vcodec", "none")
|
| 119 |
acodec = f.get("acodec", "none")
|
|
|
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
is_video = vcodec not in ("none", None, "")
|
| 122 |
is_audio = acodec not in ("none", None, "")
|
| 123 |
|
| 124 |
if is_video and is_audio:
|
| 125 |
-
|
|
|
|
| 126 |
elif is_video:
|
| 127 |
video_only.append(f)
|
| 128 |
elif is_audio:
|
| 129 |
audio_only.append(f)
|
| 130 |
|
| 131 |
-
# โโโ
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
height = f.get("height", 0) or 0
|
| 138 |
-
ext
|
| 139 |
-
fps = f.get("fps", 0) or 0
|
| 140 |
-
size_b = f.get("filesize") or f.get("filesize_approx")
|
| 141 |
-
fmt_id = f.get("format_id", "")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
if
|
| 145 |
continue
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
lbl = f"{height}p" if height else "Video"
|
| 149 |
if fps > 30:
|
| 150 |
lbl += f" {int(fps)}fps"
|
| 151 |
|
| 152 |
result.append({
|
| 153 |
-
"id":
|
| 154 |
-
"label":
|
| 155 |
-
"type":
|
| 156 |
-
"ext":
|
| 157 |
-
"height":
|
| 158 |
-
"has_audio":
|
| 159 |
-
"is_merged":
|
| 160 |
-
"
|
| 161 |
-
"
|
| 162 |
-
"
|
| 163 |
-
"
|
|
|
|
| 164 |
})
|
| 165 |
|
| 166 |
-
# โโโ
|
| 167 |
-
|
| 168 |
-
|
| 169 |
best_audio = max(audio_only,
|
| 170 |
-
key=lambda x: x.get("abr", 0) or x.get("asr", 0) or 0)
|
| 171 |
-
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
for f in video_only:
|
| 175 |
-
url = f.get("url", "")
|
| 176 |
-
if not url:
|
| 177 |
-
continue
|
| 178 |
-
|
| 179 |
height = f.get("height", 0) or 0
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
fmt_id = f.get("format_id", "")
|
| 184 |
|
| 185 |
-
|
| 186 |
-
if
|
| 187 |
continue
|
| 188 |
-
seen.add(key)
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
|
| 194 |
lbl = f"{height}p" if height else "Video"
|
| 195 |
if fps > 30:
|
| 196 |
lbl += f" {int(fps)}fps"
|
| 197 |
|
| 198 |
result.append({
|
| 199 |
-
"id":
|
| 200 |
-
"label":
|
| 201 |
-
"type":
|
| 202 |
-
"ext":
|
| 203 |
-
"height":
|
| 204 |
-
"has_audio":
|
| 205 |
-
"is_merged":
|
| 206 |
-
"
|
| 207 |
-
"
|
| 208 |
-
"
|
| 209 |
-
"
|
| 210 |
-
"
|
|
|
|
| 211 |
})
|
| 212 |
|
| 213 |
-
# โโโ
|
|
|
|
|
|
|
|
|
|
| 214 |
for f in audio_only:
|
| 215 |
url = f.get("url", "")
|
| 216 |
if not url:
|
| 217 |
continue
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
abr = f.get("abr", 0) or 0
|
| 220 |
ext = f.get("ext", "m4a")
|
| 221 |
size_b = f.get("filesize") or f.get("filesize_approx")
|
| 222 |
-
fmt_id = f.get("format_id", "")
|
| 223 |
-
|
| 224 |
-
lbl = f"{int(abr)}kbps" if abr else "Audio"
|
| 225 |
|
| 226 |
result.append({
|
| 227 |
-
"id":
|
| 228 |
-
"label":
|
| 229 |
-
"type":
|
| 230 |
-
"ext":
|
| 231 |
-
"height":
|
| 232 |
-
"has_audio":
|
| 233 |
-
"is_audio":
|
| 234 |
-
"
|
| 235 |
-
"
|
| 236 |
-
"
|
| 237 |
-
"
|
|
|
|
| 238 |
})
|
| 239 |
|
| 240 |
-
# ุชุฑุชูุจ: ู
ุฏู
ุฌ ุฃููุงู โ ุฃุนูู ุฌูุฏุฉ
|
| 241 |
-
result.sort(key=lambda x: (
|
| 242 |
-
not x.get("is_merged", False), # ุงูู
ุฏู
ุฌ ุงูุญูููู ุฃููุงู
|
| 243 |
-
not x.get("has_audio", False), # ุงููู ู
ุนุงู ุตูุช
|
| 244 |
-
-x["height"], # ุฃุนูู ุฌูุฏุฉ
|
| 245 |
-
x.get("is_audio", False) # ุงูุตูุช ููุท ุขุฎุฑ
|
| 246 |
-
))
|
| 247 |
-
|
| 248 |
return result
|
| 249 |
|
| 250 |
def _extract_info(url: str) -> dict:
|
| 251 |
-
"""ุชุดุบูู yt-dlp
|
| 252 |
|
| 253 |
-
opts =
|
| 254 |
|
| 255 |
-
#
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 260 |
info = ydl.extract_info(url, download=False)
|
| 261 |
if not info:
|
| 262 |
raise ValueError("ูู
ููุนุซุฑ ุนูู ู
ุนููู
ุงุช")
|
| 263 |
-
|
| 264 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
thumbs = info.get("thumbnails") or []
|
| 266 |
-
thumb
|
| 267 |
sorted(thumbs, key=lambda t: t.get("width", 0), reverse=True)[0]["url"]
|
| 268 |
if thumbs else info.get("thumbnail", "")
|
| 269 |
)
|
| 270 |
-
|
| 271 |
-
fmts = _formats(
|
|
|
|
| 272 |
if not fmts:
|
| 273 |
raise ValueError("ูุง ุชูุฌุฏ ุตูุบ ูุงุจูุฉ ููุชุญู
ูู")
|
| 274 |
-
|
| 275 |
return {
|
| 276 |
-
"id":
|
| 277 |
-
"title":
|
| 278 |
-
"thumbnail":
|
| 279 |
-
"duration":
|
| 280 |
-
"duration_str": info.get("duration_string",""),
|
| 281 |
-
"uploader":
|
| 282 |
-
"platform":
|
| 283 |
-
"webpage_url":
|
| 284 |
-
"view_count":
|
| 285 |
-
"formats":
|
| 286 |
}
|
| 287 |
|
| 288 |
# โโโ FastAPI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -314,31 +342,28 @@ class ExtractReq(BaseModel):
|
|
| 314 |
@app.get("/health")
|
| 315 |
async def health():
|
| 316 |
return {
|
| 317 |
-
"status":
|
| 318 |
"version": "1.0.0",
|
| 319 |
-
"queue":
|
| 320 |
-
"max":
|
| 321 |
-
"cache":
|
| 322 |
}
|
| 323 |
|
| 324 |
@app.post("/extract")
|
| 325 |
@app.post("/extract/")
|
| 326 |
async def extract(req: ExtractReq):
|
| 327 |
url = req.url.strip()
|
| 328 |
-
if not url.startswith(("http://","https://")):
|
| 329 |
raise HTTPException(400, "ุฑุงุจุท ุบูุฑ ุตุงูุญ")
|
| 330 |
|
| 331 |
-
# ุชุฃุฎูุฑ ุจุณูุท ููู Instagram/Facebook ุนุดุงู Rate Limit
|
| 332 |
if any(x in url.lower() for x in ["instagram", "facebook", "fb.watch"]):
|
| 333 |
await asyncio.sleep(0.5)
|
| 334 |
|
| 335 |
-
|
| 336 |
-
key = hashlib.sha256(url.encode()).hexdigest()
|
| 337 |
cached = await cache.get(key)
|
| 338 |
if cached:
|
| 339 |
return {"success": True, "cached": True, "data": cached}
|
| 340 |
|
| 341 |
-
# ุงุณุชุฎุฑุงุฌ
|
| 342 |
async with queue.slot():
|
| 343 |
log.info(f"๐ [{queue.active}/{MAX_CONCURRENT}] {url[:80]}")
|
| 344 |
loop = asyncio.get_event_loop()
|
|
@@ -351,17 +376,17 @@ async def extract(req: ExtractReq):
|
|
| 351 |
raise HTTPException(408, "ุงูุชูู ููุช ุงูุงุณุชุฎุฑุงุฌ โ ุฌุฑูุจ ู
ุฑุฉ ุฃุฎุฑู")
|
| 352 |
except yt_dlp.utils.DownloadError as e:
|
| 353 |
msg = str(e)
|
| 354 |
-
if "Private video"
|
| 355 |
raise HTTPException(403, "ุงูููุฏูู ุฎุงุต")
|
| 356 |
-
if "not available"
|
| 357 |
raise HTTPException(429, "ุชู
ุชุฌุงูุฒ ุงูุญุฏ ุงูู
ุณู
ูุญ โ ุฌุฑูุจ ุจุนุฏ ุดููุฉ")
|
| 358 |
-
if "not available"
|
| 359 |
raise HTTPException(404, "ุงูููุฏูู ุบูุฑ ู
ุชุงุญ ูู ู
ูุทูุชู")
|
| 360 |
-
if "login required" in msg.lower():
|
| 361 |
raise HTTPException(401, "ูุชุทูุจ ุชุณุฌูู ุฏุฎูู")
|
| 362 |
-
if "copyright"
|
| 363 |
raise HTTPException(451, "ู
ุญุฌูุจ ุจุณุจุจ ุญููู ุงููุดุฑ")
|
| 364 |
-
if "Unable to extract" in msg:
|
| 365 |
raise HTTPException(422, "ุงูู
ูุตุฉ ุญุธุฑุช ุงูุงุณุชุฎุฑุงุฌ โ ุฌุฑูุจ ุฑุงุจุท ุชุงูู")
|
| 366 |
raise HTTPException(422, f"ูุง ูู
ูู ุงุณุชุฎุฑุงุฌ ุงูุฑุงุจุท: {msg[:150]}")
|
| 367 |
except ValueError as e:
|
|
|
|
| 84 |
queue = Queue(MAX_CONCURRENT)
|
| 85 |
|
| 86 |
# โโโ yt-dlp helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 87 |
+
YDL_OPTS_BASE = {
|
| 88 |
"quiet": True,
|
| 89 |
"no_warnings": True,
|
| 90 |
"skip_download": True,
|
|
|
|
| 102 |
b /= 1024
|
| 103 |
return f"{b:.1f} GB"
|
| 104 |
|
| 105 |
+
def _formats(fmts: list) -> list[dict]:
|
| 106 |
"""
|
| 107 |
+
ูุฌูุจ ุงูููุฑู
ุงุช ุงูู
ุฏู
ุฌุฉ (ููุฏูู+ุตูุช) ุฃููุงู.
|
| 108 |
+
ูู ู
ููุดุ ูุฏู
ุฌ ูุฏููุงู.
|
| 109 |
"""
|
| 110 |
+
result = []
|
| 111 |
+
seen_heights = set()
|
| 112 |
|
| 113 |
+
# โโโ ุงูุฎุทูุฉ 1: ููุตู ุงูููุฑู
ุงุช โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 114 |
+
merged_formats = [] # ููุฏูู+ุตูุช ู
ุฏู
ุฌ (ุงูุฃูุถู)
|
| 115 |
+
video_only = [] # ููุฏูู ุจุฏูู ุตูุช
|
| 116 |
+
audio_only = [] # ุตูุช ููุท
|
| 117 |
|
| 118 |
for f in fmts:
|
| 119 |
vcodec = f.get("vcodec", "none")
|
| 120 |
acodec = f.get("acodec", "none")
|
| 121 |
+
url = f.get("url", "")
|
| 122 |
|
| 123 |
+
if not url:
|
| 124 |
+
continue
|
| 125 |
+
|
| 126 |
is_video = vcodec not in ("none", None, "")
|
| 127 |
is_audio = acodec not in ("none", None, "")
|
| 128 |
|
| 129 |
if is_video and is_audio:
|
| 130 |
+
# ู
ุฏู
ุฌ! ุฏู ุงููู ุนุงูุฒููู
|
| 131 |
+
merged_formats.append(f)
|
| 132 |
elif is_video:
|
| 133 |
video_only.append(f)
|
| 134 |
elif is_audio:
|
| 135 |
audio_only.append(f)
|
| 136 |
|
| 137 |
+
# โโโ ุงูุฎุทูุฉ 2: ูุถูู ุงูู
ุฏู
ุฌ (ุงูุฃูุถู) โโโโโโโโโโโโโโโโโโ
|
| 138 |
+
# ูุฑุชุจูู
: ุฃุนูู ุฌูุฏุฉ ุฃููุงู
|
| 139 |
+
merged_formats.sort(key=lambda x: x.get("height", 0) or 0, reverse=True)
|
| 140 |
+
|
| 141 |
+
for f in merged_formats:
|
|
|
|
| 142 |
height = f.get("height", 0) or 0
|
| 143 |
+
ext = f.get("ext", "mp4")
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
# dedup ุจุงูู height
|
| 146 |
+
if height in seen_heights:
|
| 147 |
continue
|
| 148 |
+
seen_heights.add(height)
|
| 149 |
+
|
| 150 |
+
fps = f.get("fps", 0) or 0
|
| 151 |
+
size_b = f.get("filesize") or f.get("filesize_approx")
|
| 152 |
|
| 153 |
lbl = f"{height}p" if height else "Video"
|
| 154 |
if fps > 30:
|
| 155 |
lbl += f" {int(fps)}fps"
|
| 156 |
|
| 157 |
result.append({
|
| 158 |
+
"id": f.get("format_id", ""),
|
| 159 |
+
"label": lbl,
|
| 160 |
+
"type": "๐ฌ ููุฏูู+ุตูุช",
|
| 161 |
+
"ext": ext,
|
| 162 |
+
"height": height,
|
| 163 |
+
"has_audio": True,
|
| 164 |
+
"is_merged": True, # โ ู
ูู
: ุฏู ู
ุฏู
ุฌ ุฃุตูุงู
|
| 165 |
+
"is_real_merged": True,
|
| 166 |
+
"size": _size(size_b),
|
| 167 |
+
"size_bytes": size_b or 0,
|
| 168 |
+
"url": f["url"],
|
| 169 |
+
"audio_url": None,
|
| 170 |
})
|
| 171 |
|
| 172 |
+
# โโโ ุงูุฎุทูุฉ 3: ูู ู
ููุด ู
ุฏู
ุฌุ ูุฏู
ุฌ ูุฏููุงู โโโโโโโโโโโโโ
|
| 173 |
+
if not result and video_only and audio_only:
|
| 174 |
+
# ุฃุญุณู ุตูุช
|
| 175 |
best_audio = max(audio_only,
|
| 176 |
+
key=lambda x: (x.get("abr", 0) or 0, x.get("asr", 0) or 0))
|
| 177 |
+
|
| 178 |
+
# ูุงุฎุฏ ููุฏูููุงุช ู
ุฎุชููุฉ ุงูุฌูุฏุงุช
|
| 179 |
+
video_only.sort(key=lambda x: x.get("height", 0) or 0, reverse=True)
|
| 180 |
|
| 181 |
for f in video_only:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
height = f.get("height", 0) or 0
|
| 183 |
+
if height in seen_heights:
|
| 184 |
+
continue
|
| 185 |
+
seen_heights.add(height)
|
|
|
|
| 186 |
|
| 187 |
+
# ูุชุฎุทู ุงูุฌูุฏุงุช ุงูุนุงููุฉ ุฌุฏุงู (4K) ุนุดุงู ู
ููุด ุตูุช ูููุง ุบุงูุจุงู
|
| 188 |
+
if height > 1080 and len([x for x in result if x["height"] >= 720]) > 0:
|
| 189 |
continue
|
|
|
|
| 190 |
|
| 191 |
+
ext = f.get("ext", "mp4")
|
| 192 |
+
fps = f.get("fps", 0) or 0
|
| 193 |
+
v_size = f.get("filesize") or f.get("filesize_approx") or 0
|
| 194 |
+
a_size = best_audio.get("filesize") or best_audio.get("filesize_approx") or 0
|
| 195 |
|
| 196 |
lbl = f"{height}p" if height else "Video"
|
| 197 |
if fps > 30:
|
| 198 |
lbl += f" {int(fps)}fps"
|
| 199 |
|
| 200 |
result.append({
|
| 201 |
+
"id": f"{f.get('format_id')}+{best_audio.get('format_id')}",
|
| 202 |
+
"label": lbl,
|
| 203 |
+
"type": "๐ฌ ููุฏูู+ุตูุช",
|
| 204 |
+
"ext": ext,
|
| 205 |
+
"height": height,
|
| 206 |
+
"has_audio": True,
|
| 207 |
+
"is_merged": False, # โ ููุฏู
ุฌ ูุฏููุงู
|
| 208 |
+
"is_real_merged": False,
|
| 209 |
+
"size": _size(v_size + a_size),
|
| 210 |
+
"size_bytes": v_size + a_size,
|
| 211 |
+
"url": f["url"],
|
| 212 |
+
"audio_url": best_audio["url"],
|
| 213 |
+
"audio_ext": best_audio.get("ext", "m4a"),
|
| 214 |
})
|
| 215 |
|
| 216 |
+
# โโโ ุงูุฎุทูุฉ 4: ูุถูู ุงูุตูุช ุงูู
ููุตู โโโโโโโโโโโโโโโโโโโโ
|
| 217 |
+
audio_only.sort(key=lambda x: x.get("abr", 0) or 0, reverse=True)
|
| 218 |
+
best_audio_added = False
|
| 219 |
+
|
| 220 |
for f in audio_only:
|
| 221 |
url = f.get("url", "")
|
| 222 |
if not url:
|
| 223 |
continue
|
| 224 |
|
| 225 |
+
# ุฃุญุณู ุตูุช ุจุณ
|
| 226 |
+
if best_audio_added:
|
| 227 |
+
continue
|
| 228 |
+
best_audio_added = True
|
| 229 |
+
|
| 230 |
abr = f.get("abr", 0) or 0
|
| 231 |
ext = f.get("ext", "m4a")
|
| 232 |
size_b = f.get("filesize") or f.get("filesize_approx")
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
result.append({
|
| 235 |
+
"id": f.get("format_id", ""),
|
| 236 |
+
"label": f"{int(abr)}kbps" if abr else "Audio",
|
| 237 |
+
"type": "๐ต ุตูุช ููุท",
|
| 238 |
+
"ext": ext,
|
| 239 |
+
"height": 0,
|
| 240 |
+
"has_audio": True,
|
| 241 |
+
"is_audio": True,
|
| 242 |
+
"is_merged": False,
|
| 243 |
+
"size": _size(size_b),
|
| 244 |
+
"size_bytes": size_b or 0,
|
| 245 |
+
"url": url,
|
| 246 |
+
"audio_url": None,
|
| 247 |
})
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
return result
|
| 250 |
|
| 251 |
def _extract_info(url: str) -> dict:
|
| 252 |
+
"""ุชุดุบูู yt-dlp"""
|
| 253 |
|
| 254 |
+
opts = YDL_OPTS_BASE.copy()
|
| 255 |
|
| 256 |
+
# โโโ ู
ูู
: ูุฌูุจ ุงูููุฑู
ุงุช ุงูู
ุฏู
ุฌุฉ ุฃููุงู โโโโโโโโโโโโโโโโโ
|
| 257 |
+
# ูุฌุฑุจ format specs ู
ุฎุชููุฉ ุญุณุจ ุงูู
ูุตุฉ
|
| 258 |
+
|
| 259 |
+
if "youtube" in url.lower() or "youtu.be" in url.lower():
|
| 260 |
+
# YouTube: ููุถู ุงูู
ุฏู
ุฌ
|
| 261 |
+
opts["format"] = "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[height<=1080][ext=mp4]/best"
|
| 262 |
+
elif "facebook" in url.lower() or "fb.watch" in url.lower():
|
| 263 |
+
# Facebook: ูุฌูุจ ูู ุญุงุฌุฉ
|
| 264 |
+
opts["format"] = "best"
|
| 265 |
+
elif "instagram" in url.lower():
|
| 266 |
+
opts["format"] = "best"
|
| 267 |
+
else:
|
| 268 |
+
opts["format"] = "best"
|
| 269 |
|
| 270 |
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 271 |
info = ydl.extract_info(url, download=False)
|
| 272 |
if not info:
|
| 273 |
raise ValueError("ูู
ููุนุซุฑ ุนูู ู
ุนููู
ุงุช")
|
| 274 |
+
|
| 275 |
+
# โโโ ู
ูู
: ูุฌูุจ ุงูู format ุงูู
ุฎุชุงุฑ ู
ู yt-dlp โโโโโโโโ
|
| 276 |
+
# ุฏู ุจูููู ุฃุญุณู ููุฏูู+ุตูุช ู
ุฏู
ุฌ
|
| 277 |
+
requested_formats = info.get("requested_formats", [])
|
| 278 |
+
format_id = info.get("format_id", "")
|
| 279 |
+
|
| 280 |
+
# ูู requested_formats ู
ูุฌูุฏุ ูุถูููู
ููู formats
|
| 281 |
+
all_formats = info.get("formats", [])
|
| 282 |
+
|
| 283 |
+
# ูุถูู ุนูุงู
ุฉ ุนูู ุงูู format ุงูู
ุฎุชุงุฑ
|
| 284 |
+
for f in all_formats:
|
| 285 |
+
if f.get("format_id") == format_id:
|
| 286 |
+
f["_selected"] = True
|
| 287 |
+
# ูู ูู ุฌุฒุก ู
ู requested_formats
|
| 288 |
+
if any(rf.get("format_id") == f.get("format_id") for rf in requested_formats):
|
| 289 |
+
f["_requested"] = True
|
| 290 |
+
|
| 291 |
+
# ุตูุฑุฉ ู
ุตุบุฑุฉ
|
| 292 |
thumbs = info.get("thumbnails") or []
|
| 293 |
+
thumb = (
|
| 294 |
sorted(thumbs, key=lambda t: t.get("width", 0), reverse=True)[0]["url"]
|
| 295 |
if thumbs else info.get("thumbnail", "")
|
| 296 |
)
|
| 297 |
+
|
| 298 |
+
fmts = _formats(all_formats)
|
| 299 |
+
|
| 300 |
if not fmts:
|
| 301 |
raise ValueError("ูุง ุชูุฌุฏ ุตูุบ ูุงุจูุฉ ููุชุญู
ูู")
|
| 302 |
+
|
| 303 |
return {
|
| 304 |
+
"id": info.get("id", ""),
|
| 305 |
+
"title": info.get("title", "ููุฏูู"),
|
| 306 |
+
"thumbnail": thumb,
|
| 307 |
+
"duration": info.get("duration"),
|
| 308 |
+
"duration_str": info.get("duration_string", ""),
|
| 309 |
+
"uploader": info.get("uploader", "") or info.get("channel", ""),
|
| 310 |
+
"platform": info.get("extractor_key", ""),
|
| 311 |
+
"webpage_url": info.get("webpage_url", url),
|
| 312 |
+
"view_count": info.get("view_count"),
|
| 313 |
+
"formats": fmts,
|
| 314 |
}
|
| 315 |
|
| 316 |
# โโโ FastAPI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 342 |
@app.get("/health")
|
| 343 |
async def health():
|
| 344 |
return {
|
| 345 |
+
"status": "online",
|
| 346 |
"version": "1.0.0",
|
| 347 |
+
"queue": queue.active,
|
| 348 |
+
"max": MAX_CONCURRENT,
|
| 349 |
+
"cache": await cache.stats(),
|
| 350 |
}
|
| 351 |
|
| 352 |
@app.post("/extract")
|
| 353 |
@app.post("/extract/")
|
| 354 |
async def extract(req: ExtractReq):
|
| 355 |
url = req.url.strip()
|
| 356 |
+
if not url.startswith(("http://", "https://")):
|
| 357 |
raise HTTPException(400, "ุฑุงุจุท ุบูุฑ ุตุงูุญ")
|
| 358 |
|
|
|
|
| 359 |
if any(x in url.lower() for x in ["instagram", "facebook", "fb.watch"]):
|
| 360 |
await asyncio.sleep(0.5)
|
| 361 |
|
| 362 |
+
key = hashlib.sha256(url.encode()).hexdigest()
|
|
|
|
| 363 |
cached = await cache.get(key)
|
| 364 |
if cached:
|
| 365 |
return {"success": True, "cached": True, "data": cached}
|
| 366 |
|
|
|
|
| 367 |
async with queue.slot():
|
| 368 |
log.info(f"๐ [{queue.active}/{MAX_CONCURRENT}] {url[:80]}")
|
| 369 |
loop = asyncio.get_event_loop()
|
|
|
|
| 376 |
raise HTTPException(408, "ุงูุชูู ููุช ุงูุงุณุชุฎุฑุงุฌ โ ุฌุฑูุจ ู
ุฑุฉ ุฃุฎุฑู")
|
| 377 |
except yt_dlp.utils.DownloadError as e:
|
| 378 |
msg = str(e)
|
| 379 |
+
if "Private video" in msg:
|
| 380 |
raise HTTPException(403, "ุงูููุฏูู ุฎุงุต")
|
| 381 |
+
if "not available" in msg and "rate-limit" in msg.lower():
|
| 382 |
raise HTTPException(429, "ุชู
ุชุฌุงูุฒ ุงูุญุฏ ุงูู
ุณู
ูุญ โ ุฌุฑูุจ ุจุนุฏ ุดููุฉ")
|
| 383 |
+
if "not available" in msg:
|
| 384 |
raise HTTPException(404, "ุงูููุฏูู ุบูุฑ ู
ุชุงุญ ูู ู
ูุทูุชู")
|
| 385 |
+
if "login required" in msg.lower():
|
| 386 |
raise HTTPException(401, "ูุชุทูุจ ุชุณุฌูู ุฏุฎูู")
|
| 387 |
+
if "copyright" in msg.lower():
|
| 388 |
raise HTTPException(451, "ู
ุญุฌูุจ ุจุณุจุจ ุญููู ุงููุดุฑ")
|
| 389 |
+
if "Unable to extract" in msg:
|
| 390 |
raise HTTPException(422, "ุงูู
ูุตุฉ ุญุธุฑุช ุงูุงุณุชุฎุฑุงุฌ โ ุฌุฑูุจ ุฑุงุจุท ุชุงูู")
|
| 391 |
raise HTTPException(422, f"ูุง ูู
ูู ุงุณุชุฎุฑุงุฌ ุงูุฑุงุจุท: {msg[:150]}")
|
| 392 |
except ValueError as e:
|