下载并校验。返回 (是否成功, 错误描述)。
(url: str, local: Path, timeout: int = 15)
| 45 | |
| 46 | |
| 47 | def 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 | |
| 62 | def scan_urls() -> list[tuple[Path, str]]: |
no test coverage detected