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
main
Ben Busby 2022-04-20 14:50:32 -06:00
parent 7688c1a233
commit 5c8be4428b
No known key found for this signature in database
GPG Key ID: B9B7231E01D924A1
2 changed files with 8 additions and 4 deletions

View File

@ -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)

View File

@ -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 ''