testsAndMisc/python_pkg/download_cats/generate_cats.py
Krzysztof kuhy Rudnicki b1a5f245a2 fix(lint): LOG015 - replace root logger with module loggers
- Add _logger = logging.getLogger(__name__) to all modules
- Replace logging.X() calls with _logger.X() calls
- Remove logging.basicConfig() from module level (keep in run_bot())
- Add G004 to global ignores (f-strings in logging are more readable)
- Remove LOG015 and G004 per-file ignores from pyproject.toml
- Fix pytest_ignore_collect hook signature in conftest.py
2025-11-30 21:59:24 +01:00

57 lines
1.4 KiB
Python

"""Download cat images from TheCatAPI.
Fetches cat images in batches and saves them to a local directory.
"""
import json
import logging
import os
from pathlib import Path
import requests
_logger = logging.getLogger(__name__)
MAX_REQUESTS = 90
REQUEST_TIMEOUT = 30 # seconds
def _download_single_image(url: str) -> None:
"""Download and save a single image from URL.
Args:
url: The URL of the image to download.
"""
try:
# Get the image content
response = requests.get(url, timeout=REQUEST_TIMEOUT)
response.raise_for_status() # Raise an exception for HTTP errors
# Extract the image name from the URL
image_name = os.path.basename(url)
image_path = os.path.join("./CATS2/", image_name)
# Save the image to the directory
with open(image_path, "wb") as file:
file.write(response.content)
_logger.info("Saved %s as %s", url, image_path)
except requests.exceptions.RequestException:
_logger.exception("Failed to download %s", url)
requests_send = 0
while requests_send < MAX_REQUESTS:
res = requests.get(
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
timeout=REQUEST_TIMEOUT,
)
requests_send += 1
response = json.loads(res.text)
urls = [cat.get("url") for cat in response]
Path("./CATS2").mkdir(parents=True, exist_ok=True)
for url in urls:
_download_single_image(url)