From 5c8be4428bb30b2faf8a7e14b86fcda7db91deae Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Wed, 20 Apr 2022 14:50:32 -0600 Subject: [PATCH] Fall back to netloc for bang search if query is empty Previously, empty bang searches would redirect to the Whoogle instance home page. This now redirects to the specific site for the bang search instead (i.e. "!yt" without a query redirects to "youtube.com", "!gh" to "github.com", etc) Fixes #719 --- app/routes.py | 2 +- app/utils/bangs.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index ee97ee9..aa84a43 100644 --- a/app/routes.py +++ b/app/routes.py @@ -306,7 +306,7 @@ def search(): search_util = Search(request, g.user_config, g.session_key) query = search_util.new_search_query() - bang = resolve_bang(query, bang_json, url_for('.index')) + bang = resolve_bang(query, bang_json) if bang: return redirect(bang) diff --git a/app/utils/bangs.py b/app/utils/bangs.py index 2650658..e0bfe24 100644 --- a/app/utils/bangs.py +++ b/app/utils/bangs.py @@ -1,5 +1,6 @@ import json import requests +import urllib.parse as urlparse DDG_BANGS = 'https://duckduckgo.com/bang.v255.js' @@ -38,7 +39,7 @@ def gen_bangs_json(bangs_file: str) -> None: print('* Finished creating ddg bangs json') -def resolve_bang(query: str, bangs_dict: dict, fallback: str) -> str: +def resolve_bang(query: str, bangs_dict: dict) -> str: """Transform's a user's query to a bang search, if an operator is found Args: @@ -65,8 +66,11 @@ def resolve_bang(query: str, bangs_dict: dict, fallback: str) -> str: operator[0], '' ).strip() + bang_url = bangs_dict[operator]['url'] + if bang_query: - return bangs_dict[operator]['url'].replace('{}', bang_query, 1) + return bang_url.replace('{}', bang_query, 1) else: - return fallback + parsed_url = urlparse.urlparse(bang_url) + return f'{parsed_url.scheme}://{parsed_url.netloc}' return ''