From 1f9061e1c9a5152eb459d601af34b9c1919b9e15 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 30 Nov 2025 15:17:52 +0100 Subject: [PATCH] Enable S113: add timeout to requests calls --- PYTHON/downloadCats/generate_cats.py | 8 ++++++-- PYTHON/scapeWebsite/scrape_comics.py | 4 +++- pyproject.toml | 1 - 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/PYTHON/downloadCats/generate_cats.py b/PYTHON/downloadCats/generate_cats.py index adfc8b4..c83d6cb 100644 --- a/PYTHON/downloadCats/generate_cats.py +++ b/PYTHON/downloadCats/generate_cats.py @@ -13,10 +13,14 @@ 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=") + 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 = [] @@ -27,7 +31,7 @@ while requests_send < MAX_REQUESTS: for url in urls: try: # Get the image content - response = requests.get(url) + response = requests.get(url, timeout=REQUEST_TIMEOUT) response.raise_for_status() # Raise an exception for HTTP errors # Extract the image name from the URL diff --git a/PYTHON/scapeWebsite/scrape_comics.py b/PYTHON/scapeWebsite/scrape_comics.py index b3c0fa4..e8add47 100644 --- a/PYTHON/scapeWebsite/scrape_comics.py +++ b/PYTHON/scapeWebsite/scrape_comics.py @@ -11,6 +11,8 @@ from selenium.webdriver.common.by import By logging.basicConfig(level=logging.INFO) +REQUEST_TIMEOUT = 30 # seconds + # Initialize argument parser to accept the website URL as an argument parser = argparse.ArgumentParser(description="Download images from a comic website.") parser.add_argument( @@ -38,7 +40,7 @@ def download_image(url): logging.info(f"Image {image_name} already exists, skipping download.") return False logging.info(f"Downloading image from URL: {url}") - img_data = requests.get(url).content + img_data = requests.get(url, timeout=REQUEST_TIMEOUT).content with open(image_name, "wb") as handler: handler.write(img_data) logging.info(f"Image {image_name} downloaded successfully") diff --git a/pyproject.toml b/pyproject.toml index 75d1f54..61ecd21 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,7 +47,6 @@ ignore = [ "PLR0911", # Too many return statements - relaxed "PLR0913", # Too many arguments - relaxed "BLE001", # Blind except - will fix critical ones manually - "S113", # Request without timeout - will fix manually where critical "S603", # subprocess without shell - known pattern "S607", # start-process with partial path - acceptable "FBT001", # Boolean positional arg - common pattern