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,27 +54,37 @@ def resolve_bang(query: str, bangs_dict: dict) -> str:
""" """
split_query = query.strip().split(' ') #if ! not in query simply return (speed up processing)
if '!' not in query:
# 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:
return '' return ''
# rebuild the query string split_query = query.strip().split(' ')
bang_query = ' '.join(split_query[1:]).strip()
# Check if operator is a key in bangs and get bang if exists # look for operator in query if one is found, list operator should be of
bang = bangs_dict.get(operator, None) # length 1, operator should not be case-sensitive here to remove it later
if bang: operator = [
bang_url = bang['url'] word
for word in split_query
if word.lower() in bangs_dict
]
if len(operator) == 1:
# get operator
operator = operator[0]
if bang_query: # removes operator from query
return bang_url.replace('{}', bang_query, 1) split_query.remove(operator)
else:
parsed_url = urlparse.urlparse(bang_url) # rebuild the query string
return f'{parsed_url.scheme}://{parsed_url.netloc}' bang_query = ' '.join(split_query).strip()
# Check if operator is a key in bangs and get bang if exists
bang = bangs_dict.get(operator.lower(), None)
if bang:
bang_url = bang['url']
if bang_query:
return bang_url.replace('{}', bang_query, 1)
else:
parsed_url = urlparse.urlparse(bang_url)
return f'{parsed_url.scheme}://{parsed_url.netloc}'
return '' return ''