query.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 string
  20. import re
  21. class Query(object):
  22. """parse query"""
  23. def __init__(self, query, blocked_engines):
  24. self.query = query
  25. self.blocked_engines = []
  26. if blocked_engines:
  27. self.blocked_engines = blocked_engines
  28. self.query_parts = []
  29. self.engines = []
  30. self.languages = []
  31. # parse query, if tags are set, which
  32. # change the serch engine or search-language
  33. def parse_query(self):
  34. self.query_parts = []
  35. # split query, including whitespaces
  36. raw_query_parts = re.split(r'(\s+)', self.query)
  37. parse_next = True
  38. for query_part in raw_query_parts:
  39. if not parse_next:
  40. self.query_parts[-1] += query_part
  41. continue
  42. parse_next = False
  43. # part does only contain spaces, skip
  44. if query_part.isspace()\
  45. or query_part == '':
  46. parse_next = True
  47. self.query_parts.append(query_part)
  48. continue
  49. # this force a language
  50. if query_part[0] == ':':
  51. lang = query_part[1:].lower()
  52. # check if any language-code is equal with
  53. # declared language-codes
  54. for lc in language_codes:
  55. lang_id, lang_name, country = map(str.lower, lc)
  56. # if correct language-code is found
  57. # set it as new search-language
  58. if lang == lang_id\
  59. or lang_id.startswith(lang)\
  60. or lang == lang_name\
  61. or lang.replace('_', ' ') == country:
  62. parse_next = True
  63. self.languages.append(lang)
  64. break
  65. # this force a engine or category
  66. if query_part[0] == '!':
  67. prefix = query_part[1:].replace('_', ' ')
  68. # check if prefix is equal with engine shortcut
  69. if prefix in engine_shortcuts\
  70. and not engine_shortcuts[prefix] in self.blocked_engines:
  71. parse_next = True
  72. self.engines.append({'category': 'none',
  73. 'name': engine_shortcuts[prefix]})
  74. # check if prefix is equal with engine name
  75. elif prefix in engines\
  76. and prefix not in self.blocked_engines:
  77. parse_next = True
  78. self.engines.append({'category': 'none',
  79. 'name': prefix})
  80. # check if prefix is equal with categorie name
  81. elif prefix in categories:
  82. # using all engines for that search, which
  83. # are declared under that categorie name
  84. parse_next = True
  85. self.engines.extend({'category': prefix,
  86. 'name': engine.name}
  87. for engine in categories[prefix]
  88. if engine not in self.blocked_engines)
  89. # append query part to query_part list
  90. self.query_parts.append(query_part)
  91. def changeSearchQuery(self, search_query):
  92. if len(self.query_parts):
  93. self.query_parts[-1] = search_query
  94. else:
  95. self.query_parts.append(search_query)
  96. def getSearchQuery(self):
  97. if len(self.query_parts):
  98. return self.query_parts[-1]
  99. else:
  100. return ''
  101. def getFullQuery(self):
  102. # get full querry including whitespaces
  103. return string.join(self.query_parts, '')