Skip to content

The only downloader you need. 下载器的集大成者。

feat(sources): add fetchPypiJson with multi-source fallback

XiaoYouChR3750927b5d8e
diff --git a/app/sources.py b/app/sources.py
index 4f8e567a..193cfba4 100644
--- a/app/sources.py
+++ b/app/sources.py
@@ -177,6 +177,22 @@ def buildPypiUrl(package: str, *, source: str) -> str:
return f"{base}/{package}/json"
+async def fetchPypiJson(package: str) -> dict:
+ for source in SOURCE_ORDER:
+ url = buildPypiUrl(package, source=source)
+ client = buildClient(timeout=15)
+ try:
+ resp = await client.get(url)
+ resp.raise_for_status()
+ return await resp.json()
+ except Exception as e:
+ logger.debug("从 {} 获取 PyPI {} 失败: {}", source, package, repr(e))
+ continue
+ finally:
+ client.close()
+ raise RuntimeError(f"无法获取 PyPI 包信息: {package}")
+
+
def buildDownloadUrl(repo: str, tag: str, asset: str, *, source: str) -> str:
mapped = GITCODE_REPOS.get(repo, repo) if source == "gitcode" else repo
endpoints = SOURCES[source]
diff --git a/features/yt_dlp_pack/config.py b/features/yt_dlp_pack/config.py
index c4e9c256..aed8b982 100644
--- a/features/yt_dlp_pack/config.py
+++ b/features/yt_dlp_pack/config.py
@@ -197,17 +197,9 @@ class YouTubeRuntime(BinaryRuntime):
)
async def fetchLatestVersion(self) -> str:
- from app.client import buildClient
- from app.sources import SOURCE_ORDER, buildPypiUrl
- url = buildPypiUrl("yt-dlp", source=SOURCE_ORDER[0])
- client = buildClient(timeout=15)
- try:
- resp = await client.get(url)
- resp.raise_for_status()
- data = await resp.json()
- return data.get("info", {}).get("version", "")
- finally:
- client.close()
+ from app.sources import fetchPypiJson
+ data = await fetchPypiJson("yt-dlp")
+ return data.get("info", {}).get("version", "")
async def createInstallTask(self):
from app.install import BinaryInstallStep, ExtractStep, FetchStep, InstallTask
@@ -274,17 +266,8 @@ class YouTubeRuntime(BinaryRuntime):
return task
async def _fetchWhlAsset(self) -> tuple[str, int]:
- from app.client import buildClient
- from app.sources import SOURCE_ORDER, buildPypiUrl
-
- url = buildPypiUrl("yt-dlp", source=SOURCE_ORDER[0])
- client = buildClient(timeout=15)
- try:
- response = await client.get(url)
- response.raise_for_status()
- data = await response.json()
- finally:
- client.close()
+ from app.sources import fetchPypiJson
+ data = await fetchPypiJson("yt-dlp")
urls = data.get("urls") or []
for entry in urls: