Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for HTTP errors (ex. 403), to better detect when a server is blocking us #374

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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