From 809520ec707d9f8d3f70965079d358e9ad8a6420 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 1 Mar 2022 12:06:59 -0700 Subject: [PATCH] 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 --- app/routes.py | 4 ++-- app/utils/bangs.py | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/routes.py b/app/routes.py index 350621f..f4308e1 100644 --- a/app/routes.py +++ b/app/routes.py @@ -304,8 +304,8 @@ def search(): search_util = Search(request, g.user_config, g.session_key) query = search_util.new_search_query() - bang = resolve_bang(query=query, bangs_dict=bang_json) - if bang != '': + bang = resolve_bang(query, bang_json, url_for('.index')) + if bang: return redirect(bang) # Redirect to home if invalid/blank search diff --git a/app/utils/bangs.py b/app/utils/bangs.py index 4a1a3cb..538254a 100644 --- a/app/utils/bangs.py +++ b/app/utils/bangs.py @@ -38,7 +38,7 @@ def gen_bangs_json(bangs_file: str) -> None: 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 Args: @@ -59,8 +59,13 @@ def resolve_bang(query: str, bangs_dict: dict) -> str: if operator not in split_query \ and operator[1:] + operator[0] not in split_query: continue - return bangs_dict[operator]['url'].replace( - '{}', - query.replace(operator if operator in split_query - else operator[1:] + operator[0], '').strip(), 1) + + bang_query = query.replace( + operator if operator in split_query else operator[1:] + operator[0], '' + ).strip() + + if bang_query: + return bangs_dict[operator]['url'].replace('{}', bang_query, 1) + else: + return fallback return ''