(url: str, *, timeout: float, method: str)
| 317 | |
| 318 | |
| 319 | def fetch_remote_image(url: str, *, timeout: float, method: str) -> str | None: |
| 320 | headers = { |
| 321 | "Accept": "image/*,*/*;q=0.8", |
| 322 | "User-Agent": "bestjavaer-article-quality/1.0", |
| 323 | } |
| 324 | if method == "GET": |
| 325 | headers["Range"] = "bytes=0-0" |
| 326 | |
| 327 | request = Request(url, headers=headers, method=method) |
| 328 | with urlopen(request, timeout=timeout) as response: |
| 329 | status = response.getcode() |
| 330 | if not 200 <= status < 400: |
| 331 | return f"remote image returned HTTP {status}" |
| 332 | |
| 333 | content_type = response.headers.get("Content-Type", "").split(";", 1)[0].lower() |
| 334 | if content_type == "text/html": |
| 335 | return "remote image returned HTML instead of an image" |
| 336 | return None |
| 337 | |
| 338 | |
| 339 | def remote_error_message(error: BaseException) -> str: |
no outgoing calls
no test coverage detected