query.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 (
  18. categories, engines, engine_shortcuts
  19. )
  20. VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
  21. class RawTextQuery:
  22. """parse raw text query (the value from the html input)"""
  23. def __init__(self, query, disabled_engines):
  24. assert isinstance(query, str)
  25. self.query = query
  26. self.disabled_engines = []
  27. if disabled_engines:
  28. self.disabled_engines = disabled_engines
  29. self.query_parts = []
  30. self.engines = []
  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. parse_next = True
  43. for query_part in raw_query_parts:
  44. if not parse_next:
  45. self.query_parts[-1] += query_part
  46. continue
  47. parse_next = False
  48. # part does only contain spaces, skip
  49. if query_part.isspace()\
  50. or query_part == '':
  51. parse_next = True
  52. self.query_parts.append(query_part)
  53. continue
  54. # this force the timeout
  55. if query_part[0] == '<':
  56. try:
  57. raw_timeout_limit = int(query_part[1:])
  58. if raw_timeout_limit < 100:
  59. # below 100, the unit is the second ( <3 = 3 seconds timeout )
  60. self.timeout_limit = float(raw_timeout_limit)
  61. else:
  62. # 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout )
  63. self.timeout_limit = raw_timeout_limit / 1000.0
  64. parse_next = True
  65. except ValueError:
  66. # error not reported to the user
  67. pass
  68. # this force a language
  69. if query_part[0] == ':':
  70. lang = query_part[1:].lower().replace('_', '-')
  71. # check if any language-code is equal with
  72. # declared language-codes
  73. for lc in language_codes:
  74. lang_id, lang_name, country, english_name = map(str.lower, lc)
  75. # if correct language-code is found
  76. # set it as new search-language
  77. if (lang == lang_id
  78. or lang == lang_name
  79. or lang == english_name
  80. or lang.replace('-', ' ') == country)\
  81. and lang not in self.languages:
  82. parse_next = True
  83. lang_parts = lang_id.split('-')
  84. if len(lang_parts) == 2:
  85. self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
  86. else:
  87. self.languages.append(lang_id)
  88. # to ensure best match (first match is not necessarily the best one)
  89. if lang == lang_id:
  90. break
  91. # user may set a valid, yet not selectable language
  92. if VALID_LANGUAGE_CODE.match(lang):
  93. lang_parts = lang.split('-')
  94. if len(lang_parts) > 1:
  95. lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
  96. if lang not in self.languages:
  97. self.languages.append(lang)
  98. parse_next = True
  99. # external bang
  100. if query_part[0:2] == "!!":
  101. self.external_bang = query_part[2:]
  102. parse_next = True
  103. continue
  104. # this force a engine or category
  105. if query_part[0] == '!' or query_part[0] == '?':
  106. prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
  107. # check if prefix is equal with engine shortcut
  108. if prefix in engine_shortcuts:
  109. parse_next = True
  110. engine_name = engine_shortcuts[prefix]
  111. if engine_name in engines:
  112. self.engines.append({'category': 'none',
  113. 'name': engine_name,
  114. 'from_bang': True})
  115. # check if prefix is equal with engine name
  116. elif prefix in engines:
  117. parse_next = True
  118. self.engines.append({'category': 'none',
  119. 'name': prefix,
  120. 'from_bang': True})
  121. # check if prefix is equal with categorie name
  122. elif prefix in categories:
  123. # using all engines for that search, which
  124. # are declared under that categorie name
  125. parse_next = True
  126. self.engines.extend({'category': prefix,
  127. 'name': engine.name}
  128. for engine in categories[prefix]
  129. if (engine.name, prefix) not in self.disabled_engines)
  130. if query_part[0] == '!':
  131. self.specific = True
  132. # append query part to query_part list
  133. self.query_parts.append(query_part)
  134. def changeSearchQuery(self, search_query):
  135. if len(self.query_parts):
  136. self.query_parts[-1] = search_query
  137. else:
  138. self.query_parts.append(search_query)
  139. return self
  140. def getSearchQuery(self):
  141. if len(self.query_parts):
  142. return self.query_parts[-1]
  143. else:
  144. return ''
  145. def getFullQuery(self):
  146. # get full querry including whitespaces
  147. return ''.join(self.query_parts)
  148. class SearchQuery:
  149. """container for all the search parameters (query, language, etc...)"""
  150. def __init__(self, query, engines, categories, lang, safesearch, pageno, time_range,
  151. timeout_limit=None, preferences=None, external_bang=None):
  152. self.query = query
  153. self.engines = engines
  154. self.categories = categories
  155. self.lang = lang
  156. self.safesearch = safesearch
  157. self.pageno = pageno
  158. self.time_range = None if time_range in ('', 'None', None) else time_range
  159. self.timeout_limit = timeout_limit
  160. self.preferences = preferences
  161. self.external_bang = external_bang
  162. def __str__(self):
  163. return self.query + ";" + str(self.engines)