mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 13:23:15 +02:00
Enable S113: add timeout to requests calls
This commit is contained in:
parent
af162cc321
commit
1f9061e1c9
@ -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
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user