Skip to content

Commit

Permalink
Merge pull request #374 from adbenitez/adb-issue-367
Browse files Browse the repository at this point in the history
check for HTTP errors (ex. 403), to better detect when a server is blocking us
  • Loading branch information
gleitz authored Apr 8, 2021
2 parents ceef5ae + 2b3b163 commit 5cb5d82
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions howdoi/howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ def _format_url_to_filename(url, file_ext='html'):

def _get_result(url):
try:
return howdoi_session.get(url, headers={'User-Agent': _random_choice(USER_AGENTS)},
resp = howdoi_session.get(url, headers={'User-Agent': _random_choice(USER_AGENTS)},
proxies=get_proxies(),
verify=VERIFY_SSL_CERTIFICATE).text
verify=VERIFY_SSL_CERTIFICATE)
resp.raise_for_status()
return resp.text
except requests.exceptions.SSLError as error:
_print_err('Encountered an SSL Error. Try using HTTP instead of '
'HTTPS by setting the environment variable "HOWDOI_DISABLE_SSL".\n')
Expand Down Expand Up @@ -276,8 +278,11 @@ def _get_links(query):
search_engine = os.getenv('HOWDOI_SEARCH_ENGINE', 'google')
search_url = _get_search_url(search_engine)

result = _get_result(search_url.format(URL, url_quote(query)))
if _is_blocked(result):
try:
result = _get_result(search_url.format(URL, url_quote(query)))
except requests.HTTPError:
result = None
if not result or _is_blocked(result):
_print_err('Unable to find an answer because the search engine temporarily blocked the request. '
'Please wait a few minutes or select a different search engine.')
raise BlockError("Temporary block by search engine")
Expand Down

0 comments on commit 5cb5d82

Please sign in to comment.