query.py 4.4KB

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