query.py 6.5 KB

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