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

os.environ['HOWDOI_SEARCH_ENGINE'] FIX #402

Merged
merged 5 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 10 additions & 7 deletions howdoi/howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def _get_links(query):
try:
result = _get_result(search_url)
except requests.HTTPError:
logging.info('Received HTTPError')
result = None
if not result or _is_blocked(result):
logging.error('%sUnable to find an answer because the search engine temporarily blocked the request. '
Expand Down Expand Up @@ -475,7 +476,7 @@ def _is_help_query(query):

def _format_answers(args, res):
if "error" in res:
return res["error"]
return f'ERROR: {RED}{res["error"]}{END_FORMAT}'

if args["json_output"]:
return json.dumps(res)
Expand Down Expand Up @@ -588,7 +589,13 @@ def howdoi(raw_query):
else:
args = raw_query

os.environ['HOWDOI_SEARCH_ENGINE'] = args['search_engine']
os.environ['HOWDOI_SEARCH_ENGINE'] = args['search_engine'] or os.getenv('HOWDOI_SEARCH_ENGINE') or 'google'
search_engine = os.getenv('HOWDOI_SEARCH_ENGINE')
if search_engine not in SUPPORTED_SEARCH_ENGINES:
supported_search_engines = ', '.join(SUPPORTED_SEARCH_ENGINES)
message = f'Unsupported engine {search_engine}. The supported engines are: {supported_search_engines}'
res = {'error': message}
return _parse_cmd(args, res)

args['query'] = ' '.join(args['query']).replace('?', '')
cache_key = _get_cache_key(args)
Expand Down Expand Up @@ -647,7 +654,7 @@ def get_parser():
parser.add_argument('-v', '--version', help='displays the current version of howdoi',
action='store_true')
parser.add_argument('-e', '--engine', help='search engine for this query (google, bing, duckduckgo)',
dest='search_engine', nargs="?", default='google', metavar='ENGINE')
dest='search_engine', nargs="?", metavar='ENGINE')
parser.add_argument('--save', '--stash', help='stash a howdoi answer',
action='store_true')
parser.add_argument('--view', help='view your stash',
Expand Down Expand Up @@ -778,10 +785,6 @@ def command_line_runner(): # pylint: disable=too-many-return-statements,too-man
if os.getenv('HOWDOI_COLORIZE'):
args['color'] = True

if not args['search_engine'] in SUPPORTED_SEARCH_ENGINES:
logging.error('Unsupported engine.\nThe supported engines are: %s' ', '.join(SUPPORTED_SEARCH_ENGINES))
return

utf8_result = howdoi(args).encode('utf-8', 'ignore')
if sys.version < '3':
print(utf8_result)
Expand Down
22 changes: 12 additions & 10 deletions test_howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,20 @@ def test_answers_bing(self):

os.environ['HOWDOI_SEARCH_ENGINE'] = ''

def test_answers_duckduckgo(self):
os.environ['HOWDOI_SEARCH_ENGINE'] = 'duckduckgo'
for query in self.queries:
self.assertValidResponse(howdoi.howdoi(query))
for query in self.bad_queries:
self.assertValidResponse(howdoi.howdoi(query))
# commenting out duckduckgo test, re-enable when issue #404 (duckduckgo blocking requests) is resolved

os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
for query in self.pt_queries:
self.assertValidResponse(howdoi.howdoi(query))
# def test_answers_duckduckgo(self):
# os.environ['HOWDOI_SEARCH_ENGINE'] = 'duckduckgo'
# for query in self.queries:
# self.assertValidResponse(howdoi.howdoi(query))
# for query in self.bad_queries:
# self.assertValidResponse(howdoi.howdoi(query))

os.environ['HOWDOI_SEARCH_ENGINE'] = ''
# os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
# for query in self.pt_queries:
# self.assertValidResponse(howdoi.howdoi(query))

# os.environ['HOWDOI_SEARCH_ENGINE'] = ''

def test_answer_links_using_l_option(self):
for query in self.queries:
Expand Down