Fix handling of bangs
This commit is contained in:
parent
74503d542e
commit
15f6e432ff
|
@ -53,24 +53,29 @@ def resolve_bang(query: str, bangs_dict: dict) -> str:
|
||||||
wasn't a match or didn't contain a bang operator
|
wasn't a match or didn't contain a bang operator
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
# if ! no in request simply return (speed up processing)
|
||||||
|
if '!' not in query:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
split_query = query.strip().split(' ')
|
||||||
|
|
||||||
# Ensure bang search is case insensitive
|
# Ensure bang search is case insensitive
|
||||||
query = query.lower()
|
# Assumes query starts with bang
|
||||||
split_query = query.split(' ')
|
operator = split_query[0].lower()
|
||||||
for operator in bangs_dict.keys():
|
|
||||||
if operator not in split_query \
|
|
||||||
and operator[1:] + operator[0] not in split_query:
|
|
||||||
continue
|
|
||||||
|
|
||||||
bang_query = query.replace(
|
# rebuild the query string
|
||||||
operator if operator in split_query else operator[1:] +
|
bang_query = ' '.join(split_query[1:]).strip()
|
||||||
operator[0], ''
|
print("split_query", bang_query)
|
||||||
).strip()
|
|
||||||
|
|
||||||
bang_url = bangs_dict[operator]['url']
|
# Check if operator is a key in bangs and get bang if exists
|
||||||
|
bang = bangs_dict.get(operator, None)
|
||||||
|
if bang:
|
||||||
|
bang_url = bang['url']
|
||||||
|
|
||||||
if bang_query:
|
if bang_query:
|
||||||
return bang_url.replace('{}', bang_query, 1)
|
return bang_url.replace('{}', bang_query, 1)
|
||||||
else:
|
else:
|
||||||
parsed_url = urlparse.urlparse(bang_url)
|
parsed_url = urlparse.urlparse(bang_url)
|
||||||
|
print("no query", f'{parsed_url.scheme}://{parsed_url.netloc}')
|
||||||
return f'{parsed_url.scheme}://{parsed_url.netloc}'
|
return f'{parsed_url.scheme}://{parsed_url.netloc}'
|
||||||
return ''
|
return ''
|
||||||
|
|
Loading…
Reference in New Issue
Block a user