Added ddg-style !bang-operators

This is a proof of concept! The code works, but uses hardcoded operators
and may be placed in the wrong file/class.
The best-case scenario would be the possibility to use the 13.000+ ddg
operators, but I don't know if that's possible without having to
redirect to duckduckgo first.
main
Marvin Borner 2020-06-26 00:26:02 +02:00
parent 4e970a4796
commit dd9d87d25b
No known key found for this signature in database
GPG Key ID: EE7C47A7F1E65C4B
2 changed files with 21 additions and 0 deletions

View File

@ -144,6 +144,10 @@ def search():
search_util = RoutingUtils(request, g.user_config, session, cookies_disabled=g.cookies_disabled)
query = search_util.new_search_query()
resolved_bangs = search_util.bang_operator()
if resolved_bangs != '':
return redirect(resolved_bangs)
# Redirect to home if invalid/blank search
if not query:
return redirect('/')

View File

@ -17,6 +17,14 @@ class RoutingUtils:
self.query = ''
self.cookies_disabled = cookies_disabled
self.search_type = self.request_params.get('tbm') if 'tbm' in self.request_params else ''
self.bang_operators = {
'!gh': 'https://github.com/search?q=',
'!ddg': 'https://duckduckgo.com/?q=',
'!w': 'https://wikipedia.com/wiki/',
'!so': 'https://stackoverflow.com/search?q=',
'!a': 'https://amazon.com/s?k=',
'!ebay': 'https://ebay.com/sch/i.html?_nkw=',
}
def __getitem__(self, name):
return getattr(self, name)
@ -55,6 +63,15 @@ class RoutingUtils:
self.query = q[2:] if self.feeling_lucky else q
return self.query
def bang_operator(self) -> str:
print(self.query)
for operator, url in self.bang_operators.items():
if operator in self.query:
return url + self.query.replace(operator, '').strip()
return ''
def generate_response(self) -> Tuple[Any, int]:
mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent