Fallback to home page for empty bang searches

Bang searches without an actual query (i.e. just searching "!gh") will
now redirect to the home page. I guess people do this for some reason
and don't like that it redirects to the correct bang result URL, but
without an actual search term.

Fixes #595
main
Ben Busby 2022-03-01 12:06:59 -07:00
parent b28fa86e33
commit 809520ec70
No known key found for this signature in database
GPG Key ID: B9B7231E01D924A1
2 changed files with 12 additions and 7 deletions

View File

@ -304,8 +304,8 @@ def search():
search_util = Search(request, g.user_config, g.session_key) search_util = Search(request, g.user_config, g.session_key)
query = search_util.new_search_query() query = search_util.new_search_query()
bang = resolve_bang(query=query, bangs_dict=bang_json) bang = resolve_bang(query, bang_json, url_for('.index'))
if bang != '': if bang:
return redirect(bang) return redirect(bang)
# Redirect to home if invalid/blank search # Redirect to home if invalid/blank search

View File

@ -38,7 +38,7 @@ def gen_bangs_json(bangs_file: str) -> None:
print('* Finished creating ddg bangs json') print('* Finished creating ddg bangs json')
def resolve_bang(query: str, bangs_dict: dict) -> str: def resolve_bang(query: str, bangs_dict: dict, fallback: str) -> str:
"""Transform's a user's query to a bang search, if an operator is found """Transform's a user's query to a bang search, if an operator is found
Args: Args:
@ -59,8 +59,13 @@ def resolve_bang(query: str, bangs_dict: dict) -> str:
if operator not in split_query \ if operator not in split_query \
and operator[1:] + operator[0] not in split_query: and operator[1:] + operator[0] not in split_query:
continue continue
return bangs_dict[operator]['url'].replace(
'{}', bang_query = query.replace(
query.replace(operator if operator in split_query operator if operator in split_query else operator[1:] + operator[0], ''
else operator[1:] + operator[0], '').strip(), 1) ).strip()
if bang_query:
return bangs_dict[operator]['url'].replace('{}', bang_query, 1)
else:
return fallback
return '' return ''