[enh] allow searx query parts anywhere in the query - closes #831

This commit is contained in:
Adam Tauber 2020-10-25 17:40:16 +01:00
parent 9b872871fe
commit aa3c18dda9

View File

@ -37,6 +37,7 @@ class RawTextQuery:
self.disabled_engines = disabled_engines self.disabled_engines = disabled_engines
self.query_parts = [] self.query_parts = []
self.user_query_parts = []
self.enginerefs = [] self.enginerefs = []
self.languages = [] self.languages = []
self.timeout_limit = None self.timeout_limit = None
@ -52,19 +53,13 @@ class RawTextQuery:
# split query, including whitespaces # split query, including whitespaces
raw_query_parts = re.split(r'(\s+)', self.query) raw_query_parts = re.split(r'(\s+)', self.query)
parse_next = True
for query_part in raw_query_parts: for query_part in raw_query_parts:
if not parse_next: searx_query_part = False
self.query_parts[-1] += query_part
continue
parse_next = False
# part does only contain spaces, skip # part does only contain spaces, skip
if query_part.isspace()\ if query_part.isspace()\
or query_part == '': or query_part == '':
parse_next = True searx_query_part = True
self.query_parts.append(query_part) self.query_parts.append(query_part)
continue continue
@ -78,7 +73,7 @@ class RawTextQuery:
else: else:
# 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout ) # 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout )
self.timeout_limit = raw_timeout_limit / 1000.0 self.timeout_limit = raw_timeout_limit / 1000.0
parse_next = True searx_query_part = True
except ValueError: except ValueError:
# error not reported to the user # error not reported to the user
pass pass
@ -99,7 +94,7 @@ class RawTextQuery:
or lang == english_name or lang == english_name
or lang.replace('-', ' ') == country)\ or lang.replace('-', ' ') == country)\
and lang not in self.languages: and lang not in self.languages:
parse_next = True searx_query_part = True
lang_parts = lang_id.split('-') lang_parts = lang_id.split('-')
if len(lang_parts) == 2: if len(lang_parts) == 2:
self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper()) self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
@ -116,12 +111,12 @@ class RawTextQuery:
lang = lang_parts[0].lower() + '-' + lang_parts[1].upper() lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
if lang not in self.languages: if lang not in self.languages:
self.languages.append(lang) self.languages.append(lang)
parse_next = True searx_query_part = True
# external bang # external bang
if query_part[0:2] == "!!": if query_part[0:2] == "!!":
self.external_bang = query_part[2:] self.external_bang = query_part[2:]
parse_next = True searx_query_part = True
continue continue
# this force a engine or category # this force a engine or category
if query_part[0] == '!' or query_part[0] == '?': if query_part[0] == '!' or query_part[0] == '?':
@ -129,21 +124,21 @@ class RawTextQuery:
# check if prefix is equal with engine shortcut # check if prefix is equal with engine shortcut
if prefix in engine_shortcuts: if prefix in engine_shortcuts:
parse_next = True searx_query_part = True
engine_name = engine_shortcuts[prefix] engine_name = engine_shortcuts[prefix]
if engine_name in engines: if engine_name in engines:
self.enginerefs.append(EngineRef(engine_name, 'none', True)) self.enginerefs.append(EngineRef(engine_name, 'none', True))
# check if prefix is equal with engine name # check if prefix is equal with engine name
elif prefix in engines: elif prefix in engines:
parse_next = True searx_query_part = True
self.enginerefs.append(EngineRef(prefix, 'none', True)) self.enginerefs.append(EngineRef(prefix, 'none', True))
# check if prefix is equal with categorie name # check if prefix is equal with categorie name
elif prefix in categories: elif prefix in categories:
# using all engines for that search, which # using all engines for that search, which
# are declared under that categorie name # are declared under that categorie name
parse_next = True searx_query_part = True
self.enginerefs.extend(EngineRef(engine.name, prefix) self.enginerefs.extend(EngineRef(engine.name, prefix)
for engine in categories[prefix] for engine in categories[prefix]
if (engine.name, prefix) not in self.disabled_engines) if (engine.name, prefix) not in self.disabled_engines)
@ -153,19 +148,15 @@ class RawTextQuery:
# append query part to query_part list # append query part to query_part list
self.query_parts.append(query_part) self.query_parts.append(query_part)
if not searx_query_part:
self.user_query_parts.append(query_part)
def changeQuery(self, query): def changeQuery(self, query):
if len(self.query_parts): self.user_query_parts = query.strip().split()
self.query_parts[-1] = query
else:
self.query_parts.append(query)
return self return self
def getQuery(self): def getQuery(self):
if len(self.query_parts): return ' '.join(self.user_query_parts)
return self.query_parts[-1]
else:
return ''
def getFullQuery(self): def getFullQuery(self):
# get full querry including whitespaces # get full querry including whitespaces