query.py 4.5KB

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