diff --git a/app/routes.py b/app/routes.py index ed288c0..7a58e06 100644 --- a/app/routes.py +++ b/app/routes.py @@ -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('/') diff --git a/app/utils/routing_utils.py b/app/utils/routing_utils.py index 40f8a90..deb0444 100644 --- a/app/utils/routing_utils.py +++ b/app/utils/routing_utils.py @@ -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