testsAndMisc/PYTHON/downloadCats/generate_cats.py
Krzysztof kuhy Rudnicki 3ac56e541e Enable D100-D107 docstring rules: add docstrings to all modules, classes, methods, and functions
- Added module docstrings to 19 Python files
- Added class docstrings to 5 classes (ScreenLocker, PokerModifierApp, etc.)
- Added method docstrings to 22 methods
- Added function docstrings to 25 functions
- Added __init__ docstrings to 5 classes
- Removed D100-D107 from ruff ignore list (docstrings now enforced)
- Removed deprecated ANN101, ANN102, UP038 rules from ignore list
- Fixed UP038: use union types in isinstance() calls
- All ruff checks now pass with full docstring enforcement
2025-11-30 14:45:55 +01:00

43 lines
1.2 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
logging.basicConfig(level=logging.INFO)
requests_send = 0
while requests_send < 90:
res = requests.get("https://api.thecatapi.com/v1/images/search?limit=100&api_key=")
requests_send += 1
response = json.loads(res.text)
urls = []
for cat in response:
urls.append(cat.get("url"))
Path("./CATS2").mkdir(parents=True, exist_ok=True)
for url in urls:
try:
# Get the image content
response = requests.get(url)
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)
logging.info(f"Saved {url} as {image_path}")
except requests.exceptions.RequestException as e:
logging.exception(f"Failed to download {url}: {e}")