Spaces:
Running
Running
trying to fix YT egress limit
Browse files
mcp/src/aileen3_mcp/media_tools.py
CHANGED
|
@@ -150,6 +150,7 @@ class JobRecord:
|
|
| 150 |
JOBS: Dict[str, JobRecord] = {}
|
| 151 |
REFERENCE_INDEX: Dict[tuple[str, str], str] = {}
|
| 152 |
JOB_LOCK = asyncio.Lock()
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
def _error(detail: str, reference: str | None = None, status: str = "error") -> dict:
|
|
@@ -426,6 +427,42 @@ def _load_downloaded_metadata(reference: str) -> dict | None:
|
|
| 426 |
return None
|
| 427 |
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
def _probe_duration(video_path: Path) -> Optional[float]:
|
| 430 |
try:
|
| 431 |
probe = ffmpeg.probe(str(video_path))
|
|
@@ -1168,6 +1205,16 @@ def register_media_tools(app: FastMCP) -> None:
|
|
| 1168 |
{ is_error: true, status: "error"|"failed", detail, reference }
|
| 1169 |
"""
|
| 1170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1171 |
info_reference = None
|
| 1172 |
try:
|
| 1173 |
from yt_dlp import YoutubeDL
|
|
@@ -1181,8 +1228,10 @@ def register_media_tools(app: FastMCP) -> None:
|
|
| 1181 |
reference = info_reference
|
| 1182 |
|
| 1183 |
# If already cached, skip job creation
|
| 1184 |
-
metadata =
|
| 1185 |
-
if metadata
|
|
|
|
|
|
|
| 1186 |
return {
|
| 1187 |
"reference": reference,
|
| 1188 |
"status": JobStatus.DONE,
|
|
|
|
| 150 |
JOBS: Dict[str, JobRecord] = {}
|
| 151 |
REFERENCE_INDEX: Dict[tuple[str, str], str] = {}
|
| 152 |
JOB_LOCK = asyncio.Lock()
|
| 153 |
+
SOURCE_REFERENCE_CACHE: Dict[str, tuple[str, dict]] = {}
|
| 154 |
|
| 155 |
|
| 156 |
def _error(detail: str, reference: str | None = None, status: str = "error") -> dict:
|
|
|
|
| 427 |
return None
|
| 428 |
|
| 429 |
|
| 430 |
+
def _cached_media_for_source(source: str) -> tuple[str | None, dict | None]:
|
| 431 |
+
"""Return cached metadata/reference for a media source without invoking yt-dlp."""
|
| 432 |
+
normalized = source.strip()
|
| 433 |
+
if not normalized:
|
| 434 |
+
return None, None
|
| 435 |
+
|
| 436 |
+
cached_entry = SOURCE_REFERENCE_CACHE.get(normalized)
|
| 437 |
+
if cached_entry:
|
| 438 |
+
cached_ref, _ = cached_entry
|
| 439 |
+
refreshed = _load_downloaded_metadata(cached_ref)
|
| 440 |
+
if refreshed:
|
| 441 |
+
SOURCE_REFERENCE_CACHE[normalized] = (cached_ref, refreshed)
|
| 442 |
+
return cached_ref, refreshed
|
| 443 |
+
SOURCE_REFERENCE_CACHE.pop(normalized, None)
|
| 444 |
+
|
| 445 |
+
fallback_reference = _build_reference(None, normalized)
|
| 446 |
+
fallback_metadata = _load_downloaded_metadata(fallback_reference)
|
| 447 |
+
if fallback_metadata:
|
| 448 |
+
stored_source = str(fallback_metadata.get("source") or "").strip()
|
| 449 |
+
if stored_source == normalized:
|
| 450 |
+
SOURCE_REFERENCE_CACHE[normalized] = (fallback_reference, fallback_metadata)
|
| 451 |
+
return fallback_reference, fallback_metadata
|
| 452 |
+
|
| 453 |
+
for meta_path in MEDIA_CACHE.glob("*/metadata.json"):
|
| 454 |
+
reference = meta_path.parent.name
|
| 455 |
+
metadata = _load_downloaded_metadata(reference)
|
| 456 |
+
if not metadata:
|
| 457 |
+
continue
|
| 458 |
+
stored_source = str(metadata.get("source") or "").strip()
|
| 459 |
+
if stored_source == normalized:
|
| 460 |
+
SOURCE_REFERENCE_CACHE[normalized] = (reference, metadata)
|
| 461 |
+
return reference, metadata
|
| 462 |
+
|
| 463 |
+
return None, None
|
| 464 |
+
|
| 465 |
+
|
| 466 |
def _probe_duration(video_path: Path) -> Optional[float]:
|
| 467 |
try:
|
| 468 |
probe = ffmpeg.probe(str(video_path))
|
|
|
|
| 1205 |
{ is_error: true, status: "error"|"failed", detail, reference }
|
| 1206 |
"""
|
| 1207 |
|
| 1208 |
+
normalized_source = source.strip()
|
| 1209 |
+
cached_reference, cached_metadata = _cached_media_for_source(normalized_source)
|
| 1210 |
+
if cached_reference and cached_metadata:
|
| 1211 |
+
return {
|
| 1212 |
+
"reference": cached_reference,
|
| 1213 |
+
"status": JobStatus.DONE,
|
| 1214 |
+
"cached": True,
|
| 1215 |
+
"metadata": cached_metadata,
|
| 1216 |
+
}
|
| 1217 |
+
|
| 1218 |
info_reference = None
|
| 1219 |
try:
|
| 1220 |
from yt_dlp import YoutubeDL
|
|
|
|
| 1228 |
reference = info_reference
|
| 1229 |
|
| 1230 |
# If already cached, skip job creation
|
| 1231 |
+
metadata = _load_downloaded_metadata(reference)
|
| 1232 |
+
if metadata:
|
| 1233 |
+
if normalized_source:
|
| 1234 |
+
SOURCE_REFERENCE_CACHE[normalized_source] = (reference, metadata)
|
| 1235 |
return {
|
| 1236 |
"reference": reference,
|
| 1237 |
"status": JobStatus.DONE,
|