query.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. from searx.languages import language_codes
  16. from searx.engines import (
  17. categories, engines, engine_shortcuts
  18. )
  19. import re
  20. import sys
  21. if sys.version_info[0] == 3:
  22. unicode = str
  23. VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
  24. class RawTextQuery(object):
  25. """parse raw text query (the value from the html input)"""
  26. def __init__(self, query, disabled_engines):
  27. self.query = query
  28. self.disabled_engines = []
  29. if disabled_engines:
  30. self.disabled_engines = disabled_engines
  31. self.query_parts = []
  32. self.engines = []
  33. self.languages = []
  34. self.specific = False
  35. # parse query, if tags are set, which
  36. # change the serch 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+)' if isinstance(self.query, str) else b'(\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 a language
  54. if query_part[0] == ':':
  55. lang = query_part[1:].lower().replace('_', '-')
  56. # check if any language-code is equal with
  57. # declared language-codes
  58. for lc in language_codes:
  59. lang_id, lang_name, country, english_name = map(unicode.lower, lc)
  60. # if correct language-code is found
  61. # set it as new search-language
  62. if (lang == lang_id
  63. or lang == lang_name
  64. or lang == english_name
  65. or lang.replace('-', ' ') == country)\
  66. and lang not in self.languages:
  67. parse_next = True
  68. lang_parts = lang_id.split('-')
  69. if len(lang_parts) == 2:
  70. self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
  71. else:
  72. self.languages.append(lang_id)
  73. # to ensure best match (first match is not necessarily the best one)
  74. if lang == lang_id:
  75. break
  76. # user may set a valid, yet not selectable language
  77. if VALID_LANGUAGE_CODE.match(lang):
  78. lang_parts = lang.split('-')
  79. if len(lang_parts) > 1:
  80. lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
  81. if lang not in self.languages:
  82. self.languages.append(lang)
  83. parse_next = True
  84. # this force a engine or category
  85. if query_part[0] == '!' or query_part[0] == '?':
  86. prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
  87. # check if prefix is equal with engine shortcut
  88. if prefix in engine_shortcuts:
  89. parse_next = True
  90. engine_name = engine_shortcuts[prefix]
  91. if engine_name in engines:
  92. for engine_category in engines[engine_name].categories:
  93. self.engines.append({'category': engine_category,
  94. 'name': engine_name,
  95. 'from_bang': True})
  96. # check if prefix is equal with engine name
  97. elif prefix in engines:
  98. parse_next = True
  99. if prefix in engines:
  100. for engine_category in engines[prefix].categories:
  101. self.engines.append({'category': engine_category,
  102. 'name': prefix,
  103. 'from_bang': True})
  104. # check if prefix is equal with categorie name
  105. elif prefix in categories:
  106. # using all engines for that search, which
  107. # are declared under that categorie name
  108. parse_next = True
  109. self.engines.extend({'category': prefix,
  110. 'name': engine.name}
  111. for engine in categories[prefix]
  112. if (engine.name, prefix) not in self.disabled_engines)
  113. if query_part[0] == '!':
  114. self.specific = True
  115. # append query part to query_part list
  116. self.query_parts.append(query_part)
  117. def changeSearchQuery(self, search_query):
  118. if len(self.query_parts):
  119. self.query_parts[-1] = search_query
  120. else:
  121. self.query_parts.append(search_query)
  122. def getSearchQuery(self):
  123. if len(self.query_parts):
  124. return self.query_parts[-1]
  125. else:
  126. return ''
  127. def getFullQuery(self):
  128. # get full querry including whitespaces
  129. return u''.join(self.query_parts)
  130. class SearchQuery(object):
  131. """container for all the search parameters (query, language, etc...)"""
  132. def __init__(self, query, engines, categories, lang, safesearch, pageno, time_range):
  133. self.query = query.encode('utf-8')
  134. self.engines = engines
  135. self.categories = categories
  136. self.lang = lang
  137. self.safesearch = safesearch
  138. self.pageno = pageno
  139. self.time_range = time_range
  140. def __str__(self):
  141. return str(self.query) + ";" + str(self.engines)