diff --git a/pyproject.toml b/pyproject.toml index 2d487d9..d9b30fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,9 +34,7 @@ copapy = ["obj/*.o", "py.typed"] dev = [ "ruff", "mypy", - "pytest", - "requests", - "types-requests" + "pytest" ] [tool.mypy] diff --git a/tools/get_binaries.py b/tools/get_binaries.py index 74c1b60..89fdae3 100644 --- a/tools/get_binaries.py +++ b/tools/get_binaries.py @@ -1,15 +1,25 @@ import os -import requests +import json +from urllib.request import urlopen, Request OWNER = "Nonannet" REPO = "copapy" +def fetch_json(url: str): + req = Request(url, headers={"User-Agent": "Python"}) + with urlopen(req, timeout=10) as resp: + assert resp.status == 200 + return json.load(resp) + +def download_file(url: str, dest_path: str): + req = Request(url, headers={"User-Agent": "Python"}) + with urlopen(req, timeout=30) as resp, open(dest_path, "wb") as f: + f.write(resp.read()) + def main() -> None: # Get all releases (includes prereleases) api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/releases" - resp = requests.get(api_url, timeout=10) - resp.raise_for_status() - releases = resp.json() + releases = fetch_json(api_url) assert releases, "No releases found." @@ -38,11 +48,7 @@ def main() -> None: if dest: os.makedirs(dest, exist_ok=True) print(f"- Downloading {name} ...") - r = requests.get(url, stream=True, timeout=30) - r.raise_for_status() - with open(os.path.join(dest, name), "wb") as f: - for chunk in r.iter_content(chunk_size=8192): - f.write(chunk) + download_file(url, os.path.join(dest, name)) print(f"- Saved {name} to {dest}") if __name__ == "__main__":