testsAndMisc/python_pkg/download_cats/generate_cats.py
Krzysztof kuhy Rudnicki ec8861d01c refactor: rename folders to fix N999, INP001, S311 linting rules
- Rename PYTHON/ to python_pkg/ (fix N999 uppercase folder)
- Rename camelCase folders to snake_case:
  - randomJPG -> random_jpg
  - tagDivider -> tag_divider
  - downloadCats -> download_cats
  - keyboardCoop -> keyboard_coop
  - extractLinks -> extract_links
  - scapeWebsite -> scrape_website
- Rename camelCase files:
  - generateJpeg.py -> generate_jpeg.py
  - tagDivider.py -> tag_divider.py
- Rename poker-modifier-app to poker_modifier_app (fix INP001)
- Add __init__.py to poker_modifier_app
- Replace random module with secrets.SystemRandom (fix S311)
- Fix S110 try-except-pass with contextlib.suppress
- Update all imports and config references
2025-11-30 21:20:17 +01:00

47 lines
1.3 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)
MAX_REQUESTS = 90
REQUEST_TIMEOUT = 30 # seconds
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:
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)
logging.info(f"Saved {url} as {image_path}")
except requests.exceptions.RequestException:
logging.exception(f"Failed to download {url}")