query.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python
  2. '''
  3. searx is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. searx is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  13. (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  14. '''
  15. import re
  16. from searx.languages import language_codes
  17. from searx.engines import categories, engines, engine_shortcuts
  18. from searx.search import EngineRef
  19. from searx.webutils import VALID_LANGUAGE_CODE
  20. class RawTextQuery:
  21. """parse raw text query (the value from the html input)"""
  22. def __init__(self, query, disabled_engines):
  23. assert isinstance(query, str)
  24. self.query = query
  25. self.disabled_engines = []
  26. if disabled_engines:
  27. self.disabled_engines = disabled_engines
  28. self.query_parts = []
  29. self.user_query_parts = []
  30. self.enginerefs = []
  31. self.languages = []
  32. self.timeout_limit = None
  33. self.external_bang = None
  34. self.specific = False
  35. self._parse_query()
  36. # parse query, if tags are set, which
  37. # change the search engine or search-language
  38. def _parse_query(self):
  39. self.query_parts = []
  40. # split query, including whitespaces
  41. raw_query_parts = re.split(r'(\s+)', self.query)
  42. for query_part in raw_query_parts:
  43. searx_query_part = False
  44. # part does only contain spaces, skip
  45. if query_part.isspace()\
  46. or query_part == '':
  47. searx_query_part = True
  48. self.query_parts.append(query_part)
  49. continue
  50. # this force the timeout
  51. if query_part[0] == '<':
  52. try:
  53. raw_timeout_limit = int(query_part[1:])
  54. if raw_timeout_limit < 100:
  55. # below 100, the unit is the second ( <3 = 3 seconds timeout )
  56. self.timeout_limit = float(raw_timeout_limit)
  57. else:
  58. # 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout )
  59. self.timeout_limit = raw_timeout_limit / 1000.0
  60. searx_query_part = True
  61. except ValueError:
  62. # error not reported to the user
  63. pass
  64. # this force a language
  65. if query_part[0] == ':':
  66. lang = query_part[1:].lower().replace('_', '-')
  67. # check if any language-code is equal with
  68. # declared language-codes
  69. for lc in language_codes:
  70. lang_id, lang_name, country, english_name = map(str.lower, lc)
  71. # if correct language-code is found
  72. # set it as new search-language
  73. if (lang == lang_id
  74. or lang == lang_name
  75. or lang == english_name
  76. or lang.replace('-', ' ') == country)\
  77. and lang not in self.languages:
  78. searx_query_part = True
  79. lang_parts = lang_id.split('-')
  80. if len(lang_parts) == 2:
  81. self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
  82. else:
  83. self.languages.append(lang_id)
  84. # to ensure best match (first match is not necessarily the best one)
  85. if lang == lang_id:
  86. break
  87. # user may set a valid, yet not selectable language
  88. if VALID_LANGUAGE_CODE.match(lang):
  89. lang_parts = lang.split('-')
  90. if len(lang_parts) > 1:
  91. lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
  92. if lang not in self.languages:
  93. self.languages.append(lang)
  94. searx_query_part = True
  95. # external bang
  96. if query_part[0:2] == "!!":
  97. self.external_bang = query_part[2:]
  98. searx_query_part = True
  99. continue
  100. # this force a engine or category
  101. if query_part[0] == '!' or query_part[0] == '?':
  102. prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
  103. # check if prefix is equal with engine shortcut
  104. if prefix in engine_shortcuts:
  105. searx_query_part = True
  106. engine_name = engine_shortcuts[prefix]
  107. if engine_name in engines:
  108. self.enginerefs.append(EngineRef(engine_name, 'none', True))
  109. # check if prefix is equal with engine name
  110. elif prefix in engines:
  111. searx_query_part = True
  112. self.enginerefs.append(EngineRef(prefix, 'none', True))
  113. # check if prefix is equal with categorie name
  114. elif prefix in categories:
  115. # using all engines for that search, which
  116. # are declared under that categorie name
  117. searx_query_part = True
  118. self.enginerefs.extend(EngineRef(engine.name, prefix)
  119. for engine in categories[prefix]
  120. if (engine.name, prefix) not in self.disabled_engines)
  121. if query_part[0] == '!':
  122. self.specific = True
  123. # append query part to query_part list
  124. self.query_parts.append(query_part)
  125. if not searx_query_part:
  126. self.user_query_parts.append(query_part)
  127. def changeQuery(self, query):
  128. self.user_query_parts = query.strip().split()
  129. return self
  130. def getQuery(self):
  131. return ' '.join(self.user_query_parts)
  132. def getFullQuery(self):
  133. # get full querry including whitespaces
  134. return ''.join(self.query_parts)