refactor: summarize error message without line number

This commit is contained in:
Florian Hönicke
2023-05-27 16:31:49 +02:00
parent 8e65d0fc42
commit 035befcfb0
2 changed files with 22 additions and 15 deletions

View File

@@ -20,8 +20,23 @@ def google_search(search_term, search_type, top_n):
return response.json()
def search_images(search_term, top_n):
response = google_search(search_term, search_type="image", top_n=top_n)
return [item["link"] for item in response["items"]]
"""
Returns only images that have a 200 response code.
"""
response = google_search(search_term, search_type="image", top_n=10)
image_urls = []
for item in response["items"]:
if len(image_urls) >= top_n:
break
try:
response = requests.head(item["link"], timeout=2)
if response.status_code == 200:
image_urls.append(
item["link"]
)
except requests.exceptions.RequestException:
pass
return image_urls
def search_web(search_term, top_n):
response = google_search(search_term, search_type="web", top_n=top_n)