From f04c7c5557b8605c35de4f2dd6e559cc8dbbf8d4 Mon Sep 17 00:00:00 2001 From: Vansh Comar <69755265+vacom13@users.noreply.github.com> Date: Fri, 29 Oct 2021 00:09:33 +0530 Subject: [PATCH] Support DDG style bangs with bang at the end (#503) DDG style bang searches can now have the bang (!) at the end of the search (i.e. "bologna w!" will now redirect to wikipedia just like "bologna !w" would) --- app/utils/bangs.py | 7 ++++--- test/test_routes.py | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/utils/bangs.py b/app/utils/bangs.py index 8661850..b9aebee 100644 --- a/app/utils/bangs.py +++ b/app/utils/bangs.py @@ -55,9 +55,10 @@ def resolve_bang(query: str, bangs_dict: dict) -> str: query = query.lower() split_query = query.split(' ') for operator in bangs_dict.keys(): - if operator not in split_query: + if operator not in split_query \ + and operator[1:] + operator[0] not in split_query: continue - return bangs_dict[operator]['url'].format( - query.replace(operator, '').strip()) + query.replace(operator if operator in split_query + else operator[1:] + operator[0], '').strip()) return '' diff --git a/test/test_routes.py b/test/test_routes.py index efd1c58..4aaaf68 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -36,6 +36,11 @@ def test_ddg_bang(client): assert rv._status_code == 302 assert rv.headers.get('Location').startswith('https://www.reddit.com') + # Move '!' to end of the bang + rv = client.get('/search?q=gitlab%20w!') + assert rv._status_code == 302 + assert rv.headers.get('Location').startswith('https://en.wikipedia.org') + # Ensure bang is case insensitive rv = client.get('/search?q=!GH%20whoogle') assert rv._status_code == 302