Enable S113: add timeout to requests calls

This commit is contained in:
Krzysztof kuhy Rudnicki 2025-11-30 15:17:52 +01:00
parent af162cc321
commit 1f9061e1c9
3 changed files with 9 additions and 4 deletions

View File

@ -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

View File

@ -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")

View File

@ -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