diff options
author | Bnyro <bnyro@tutanota.com> | 2023-09-10 18:44:16 +0200 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2023-09-19 09:40:57 +0200 |
commit | dcee82334548ad8849391b5c29cdcd868b65daad (patch) | |
tree | 5313b41db400f0215c4b46a9af8bb51f1c11a8b2 /searx/query.py | |
parent | 71508abcbf77df7e996114e4f142a5f15eced6e4 (diff) | |
download | searxng-dcee82334548ad8849391b5c29cdcd868b65daad.tar.gz searxng-dcee82334548ad8849391b5c29cdcd868b65daad.zip |
[feat] implement feeling lucky feature
Diffstat (limited to 'searx/query.py')
-rw-r--r-- | searx/query.py | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/searx/query.py b/searx/query.py index 751308baa..49fa89a9c 100644 --- a/searx/query.py +++ b/searx/query.py @@ -150,7 +150,7 @@ class LanguageParser(QueryPartParser): class ExternalBangParser(QueryPartParser): @staticmethod def check(raw_value): - return raw_value.startswith('!!') + return raw_value.startswith('!!') and len(raw_value) > 2 def __call__(self, raw_value): value = raw_value[2:] @@ -177,7 +177,8 @@ class ExternalBangParser(QueryPartParser): class BangParser(QueryPartParser): @staticmethod def check(raw_value): - return raw_value[0] == '!' + # make sure it's not any bang with double '!!' + return raw_value[0] == '!' and (len(raw_value) < 2 or raw_value[1] != '!') def __call__(self, raw_value): value = raw_value[1:].replace('-', ' ').replace('_', ' ') @@ -235,14 +236,25 @@ class BangParser(QueryPartParser): self._add_autocomplete(first_char + engine_shortcut) +class FeelingLuckyParser(QueryPartParser): + @staticmethod + def check(raw_value): + return raw_value == '!!' + + def __call__(self, raw_value): + self.raw_text_query.redirect_to_first_result = True + return True + + class RawTextQuery: """parse raw text query (the value from the html input)""" PARSER_CLASSES = [ - TimeoutParser, # this force the timeout - LanguageParser, # this force a language + TimeoutParser, # force the timeout + LanguageParser, # force a language ExternalBangParser, # external bang (must be before BangParser) - BangParser, # this force a engine or category + BangParser, # force an engine or category + FeelingLuckyParser, # redirect to the first link in the results list ] def __init__(self, query, disabled_engines): @@ -261,6 +273,7 @@ class RawTextQuery: self.query_parts = [] # use self.getFullQuery() self.user_query_parts = [] # use self.getQuery() self.autocomplete_location = None + self.redirect_to_first_result = False self._parse_query() def _parse_query(self): @@ -330,5 +343,6 @@ class RawTextQuery: + f"enginerefs={self.enginerefs!r}\n " + f"autocomplete_list={self.autocomplete_list!r}\n " + f"query_parts={self.query_parts!r}\n " - + f"user_query_parts={self.user_query_parts!r} >" + + f"user_query_parts={self.user_query_parts!r} >\n" + + f"redirect_to_first_result={self.redirect_to_first_result!r}" ) |