allow user to enter bang everywhere in query

This commit is contained in:
Joao Ramos 2022-09-26 12:13:49 +02:00
parent 090ab5a45c
commit 5b800c57dc

View File

@ -54,21 +54,31 @@ def resolve_bang(query: str, bangs_dict: dict) -> str:
"""
split_query = query.strip().split(' ')
# Ensure bang search is case insensitive
# Assumes query starts with bang
operator = split_query[0].lower()
#if ! not in operator simply return (speed up processing)
if '!' not in operator:
#if ! not in query simply return (speed up processing)
if '!' not in query:
return ''
split_query = query.strip().split(' ')
# look for operator in query if one is found, list operator should be of
# length 1, operator should not be case-sensitive here to remove it later
operator = [
word
for word in split_query
if word.lower() in bangs_dict
]
if len(operator) == 1:
# get operator
operator = operator[0]
# removes operator from query
split_query.remove(operator)
# rebuild the query string
bang_query = ' '.join(split_query[1:]).strip()
bang_query = ' '.join(split_query).strip()
# Check if operator is a key in bangs and get bang if exists
bang = bangs_dict.get(operator, None)
bang = bangs_dict.get(operator.lower(), None)
if bang:
bang_url = bang['url']