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)
main
Vansh Comar 2021-10-29 00:09:33 +05:30 committed by GitHub
parent 190b684469
commit f04c7c5557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

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

View File

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