Spaces:
Running
Running
Upload repo_fetcher.py with huggingface_hub
Browse files- repo_fetcher.py +18 -3
repo_fetcher.py
CHANGED
|
@@ -38,23 +38,37 @@ def _github_get(path: str) -> dict | None:
|
|
| 38 |
|
| 39 |
|
| 40 |
def fetch_readme(owner: str, name: str) -> str | None:
|
| 41 |
-
"""获取仓库 README 文件内容"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
data = _github_get(f"/repos/{owner}/{name}/readme")
|
| 44 |
if not data:
|
| 45 |
return None
|
| 46 |
content = data.get("content", "")
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
except Exception:
|
| 49 |
return None
|
| 50 |
|
| 51 |
|
| 52 |
def fetch_dependencies(owner: str, name: str) -> dict[str, str]:
|
| 53 |
-
"""获取仓库的依赖文件
|
| 54 |
|
| 55 |
Returns:
|
| 56 |
dict: {文件名: 文件内容},找不到任何依赖文件时返回 {}
|
| 57 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
result = {}
|
| 59 |
try:
|
| 60 |
contents = _github_get(f"/repos/{owner}/{name}/contents") or []
|
|
@@ -70,6 +84,7 @@ def fetch_dependencies(owner: str, name: str) -> dict[str, str]:
|
|
| 70 |
pass
|
| 71 |
except Exception:
|
| 72 |
pass
|
|
|
|
| 73 |
return result
|
| 74 |
|
| 75 |
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def fetch_readme(owner: str, name: str) -> str | None:
|
| 41 |
+
"""获取仓库 README 文件内容(带缓存,1 小时 TTL)"""
|
| 42 |
+
from cache import github_readme_cache
|
| 43 |
+
cache_key = f"{owner}/{name}"
|
| 44 |
+
cached = github_readme_cache.get(cache_key)
|
| 45 |
+
if cached is not None:
|
| 46 |
+
return cached
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
data = _github_get(f"/repos/{owner}/{name}/readme")
|
| 50 |
if not data:
|
| 51 |
return None
|
| 52 |
content = data.get("content", "")
|
| 53 |
+
result = base64.b64decode(content).decode("utf-8", errors="replace")
|
| 54 |
+
github_readme_cache.set(cache_key, result)
|
| 55 |
+
return result
|
| 56 |
except Exception:
|
| 57 |
return None
|
| 58 |
|
| 59 |
|
| 60 |
def fetch_dependencies(owner: str, name: str) -> dict[str, str]:
|
| 61 |
+
"""获取仓库的依赖文件(带缓存,1 小时 TTL)
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
dict: {文件名: 文件内容},找不到任何依赖文件时返回 {}
|
| 65 |
"""
|
| 66 |
+
from cache import github_deps_cache
|
| 67 |
+
cache_key = f"{owner}/{name}"
|
| 68 |
+
cached = github_deps_cache.get(cache_key)
|
| 69 |
+
if cached is not None:
|
| 70 |
+
return cached
|
| 71 |
+
|
| 72 |
result = {}
|
| 73 |
try:
|
| 74 |
contents = _github_get(f"/repos/{owner}/{name}/contents") or []
|
|
|
|
| 84 |
pass
|
| 85 |
except Exception:
|
| 86 |
pass
|
| 87 |
+
github_deps_cache.set(cache_key, result)
|
| 88 |
return result
|
| 89 |
|
| 90 |
|