Skip to content

Commit

Permalink
Merge pull request #465 from gitautoai/wes
Browse files Browse the repository at this point in the history
Improve a google rate limit error handling: google_search encountered an KeyError: 'x-ratelimit-limit' in handle_exceptions() 2
  • Loading branch information
hiroshinishio authored Jan 8, 2025
2 parents 9331cd2 + 37b39e8 commit b2701aa
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions services/google/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]


@handle_exceptions(default_return_value=[], raise_on_error=False)
@handle_exceptions(default_return_value=[], raise_on_error=False, api_type="google")
def search_urls(query: str, num_results: int = NUM_RESULTS_DEFAULT, lang: str = "en"):
"""https://pypi.org/project/googlesearch-python/"""
search_results: list[dict[str, str]] = []
Expand All @@ -42,7 +42,7 @@ def search_urls(query: str, num_results: int = NUM_RESULTS_DEFAULT, lang: str =
return search_results


@handle_exceptions(default_return_value=None, raise_on_error=False)
@handle_exceptions(default_return_value=None, raise_on_error=False, api_type="google")
def scrape_content_from_url(url: str):
headers = {"User-Agent": USER_AGENT}
response = get(url, headers=headers, timeout=TIMEOUT)
Expand All @@ -69,14 +69,16 @@ def scrape_content_from_url(url: str):
return {"title": title.strip(), "content": content, "url": url}


@handle_exceptions(default_return_value=[], raise_on_error=False)
@handle_exceptions(default_return_value=[], raise_on_error=False, api_type="google")
def google_search(
base_args: BaseArgs,
query: str,
num_results: int = NUM_RESULTS_DEFAULT,
lang: str = "en",
):
urls: list[dict[str, str]] = search_urls(query=query, num_results=num_results, lang=lang)
urls: list[dict[str, str]] = search_urls(
query=query, num_results=num_results, lang=lang
)
contents: list[str] = []
for url in urls:
contents.append(scrape_content_from_url(url["url"]))
Expand Down

0 comments on commit b2701aa

Please sign in to comment.