Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
[enh] allow searx query parts anywhere in the query - closes #831
Browse files Browse the repository at this point in the history
  • Loading branch information
asciimoo committed Oct 25, 2020
1 parent 9b87287 commit aa3c18d
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions searx/query.py
Expand Up @@ -37,6 +37,7 @@ def __init__(self, query, disabled_engines):
self.disabled_engines = disabled_engines

self.query_parts = []
self.user_query_parts = []
self.enginerefs = []
self.languages = []
self.timeout_limit = None
Expand All @@ -52,19 +53,13 @@ def _parse_query(self):
# split query, including whitespaces
raw_query_parts = re.split(r'(\s+)', self.query)

parse_next = True

for query_part in raw_query_parts:
if not parse_next:
self.query_parts[-1] += query_part
continue

parse_next = False
searx_query_part = False

# part does only contain spaces, skip
if query_part.isspace()\
or query_part == '':
parse_next = True
searx_query_part = True
self.query_parts.append(query_part)
continue

Expand All @@ -78,7 +73,7 @@ def _parse_query(self):
else:
# 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout )
self.timeout_limit = raw_timeout_limit / 1000.0
parse_next = True
searx_query_part = True
except ValueError:
# error not reported to the user
pass
Expand All @@ -99,7 +94,7 @@ def _parse_query(self):
or lang == english_name
or lang.replace('-', ' ') == country)\
and lang not in self.languages:
parse_next = True
searx_query_part = True
lang_parts = lang_id.split('-')
if len(lang_parts) == 2:
self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
Expand All @@ -116,34 +111,34 @@ def _parse_query(self):
lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
if lang not in self.languages:
self.languages.append(lang)
parse_next = True
searx_query_part = True

# external bang
if query_part[0:2] == "!!":
self.external_bang = query_part[2:]
parse_next = True
searx_query_part = True
continue
# this force a engine or category
if query_part[0] == '!' or query_part[0] == '?':
prefix = query_part[1:].replace('-', ' ').replace('_', ' ')

# check if prefix is equal with engine shortcut
if prefix in engine_shortcuts:
parse_next = True
searx_query_part = True
engine_name = engine_shortcuts[prefix]
if engine_name in engines:
self.enginerefs.append(EngineRef(engine_name, 'none', True))

# check if prefix is equal with engine name
elif prefix in engines:
parse_next = True
searx_query_part = True
self.enginerefs.append(EngineRef(prefix, 'none', True))

# check if prefix is equal with categorie name
elif prefix in categories:
# using all engines for that search, which
# are declared under that categorie name
parse_next = True
searx_query_part = True
self.enginerefs.extend(EngineRef(engine.name, prefix)
for engine in categories[prefix]
if (engine.name, prefix) not in self.disabled_engines)
Expand All @@ -153,19 +148,15 @@ def _parse_query(self):

# append query part to query_part list
self.query_parts.append(query_part)
if not searx_query_part:
self.user_query_parts.append(query_part)

def changeQuery(self, query):
if len(self.query_parts):
self.query_parts[-1] = query
else:
self.query_parts.append(query)
self.user_query_parts = query.strip().split()
return self

def getQuery(self):
if len(self.query_parts):
return self.query_parts[-1]
else:
return ''
return ' '.join(self.user_query_parts)

def getFullQuery(self):
# get full querry including whitespaces
Expand Down

0 comments on commit aa3c18d

Please sign in to comment.