MCPcopy
hub / github.com/walter201230/Python / download_one

Function download_one

tools/download_legacy_images.py:47–59  ·  view source on GitHub ↗

下载并校验。返回 (是否成功, 错误描述)。

(url: str, local: Path, timeout: int = 15)

Source from the content-addressed store, hash-verified

45
46
47def download_one(url: str, local: Path, timeout: int = 15) -> tuple[bool, str]:
48 """下载并校验。返回 (是否成功, 错误描述)。"""
49 try:
50 req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
51 with urllib.request.urlopen(req, timeout=timeout) as resp:
52 local.write_bytes(resp.read())
53 except Exception as e:
54 return False, f"download failed: {e}"
55 kind = detect_image_kind(local)
56 if kind is None:
57 local.unlink(missing_ok=True)
58 return False, "downloaded file is not a valid image (magic bytes check failed)"
59 return True, kind
60
61
62def scan_urls() -> list[tuple[Path, str]]:

Callers 1

cmd_downloadFunction · 0.85

Calls 1

detect_image_kindFunction · 0.85

Tested by

no test coverage detected