query.py 6.3 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. continue
  48. # this force the timeout
  49. if query_part[0] == '<':
  50. try:
  51. raw_timeout_limit = int(query_part[1:])
  52. if raw_timeout_limit < 100:
  53. # below 100, the unit is the second ( <3 = 3 seconds timeout )
  54. self.timeout_limit = float(raw_timeout_limit)
  55. else:
  56. # 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout )
  57. self.timeout_limit = raw_timeout_limit / 1000.0
  58. searx_query_part = True
  59. except ValueError:
  60. # error not reported to the user
  61. pass
  62. # this force a language
  63. if query_part[0] == ':' and len(query_part) > 1:
  64. lang = query_part[1:].lower().replace('_', '-')
  65. # check if any language-code is equal with
  66. # declared language-codes
  67. for lc in language_codes:
  68. lang_id, lang_name, country, english_name = map(str.lower, lc)
  69. # if correct language-code is found
  70. # set it as new search-language
  71. if (lang == lang_id
  72. or lang == lang_name
  73. or lang == english_name
  74. or lang.replace('-', ' ') == country)\
  75. and lang not in self.languages:
  76. searx_query_part = True
  77. lang_parts = lang_id.split('-')
  78. if len(lang_parts) == 2:
  79. self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
  80. else:
  81. self.languages.append(lang_id)
  82. # to ensure best match (first match is not necessarily the best one)
  83. if lang == lang_id:
  84. break
  85. # user may set a valid, yet not selectable language
  86. if VALID_LANGUAGE_CODE.match(lang):
  87. lang_parts = lang.split('-')
  88. if len(lang_parts) > 1:
  89. lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
  90. if lang not in self.languages:
  91. self.languages.append(lang)
  92. searx_query_part = True
  93. # external bang
  94. if query_part[0:2] == "!!":
  95. self.external_bang = query_part[2:]
  96. searx_query_part = True
  97. continue
  98. # this force a engine or category
  99. if query_part[0] == '!' or query_part[0] == '?':
  100. prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
  101. # check if prefix is equal with engine shortcut
  102. if prefix in engine_shortcuts:
  103. searx_query_part = True
  104. engine_name = engine_shortcuts[prefix]
  105. if engine_name in engines:
  106. self.enginerefs.append(EngineRef(engine_name, 'none'))
  107. # check if prefix is equal with engine name
  108. elif prefix in engines:
  109. searx_query_part = True
  110. self.enginerefs.append(EngineRef(prefix, 'none'))
  111. # check if prefix is equal with categorie name
  112. elif prefix in categories:
  113. # using all engines for that search, which
  114. # are declared under that categorie name
  115. searx_query_part = True
  116. self.enginerefs.extend(EngineRef(engine.name, prefix)
  117. for engine in categories[prefix]
  118. if (engine.name, prefix) not in self.disabled_engines)
  119. if query_part[0] == '!':
  120. self.specific = True
  121. # append query part to query_part list
  122. if searx_query_part:
  123. self.query_parts.append(query_part)
  124. else:
  125. self.user_query_parts.append(query_part)
  126. def changeQuery(self, query):
  127. self.user_query_parts = query.strip().split()
  128. return self
  129. def getQuery(self):
  130. return ' '.join(self.user_query_parts)
  131. def getFullQuery(self):
  132. # get full querry including whitespaces
  133. return '{0} {1}'.format(''.join(self.query_parts), self.getQuery()).strip()