autocomplete.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """This module implements functions needed for the autocompleter.
  4. """
  5. # pylint: disable=use-dict-literal
  6. import json
  7. from urllib.parse import urlencode
  8. import lxml
  9. from httpx import HTTPError
  10. from searx import settings
  11. from searx.engines import (
  12. engines,
  13. google,
  14. )
  15. from searx.network import get as http_get
  16. from searx.exceptions import SearxEngineResponseException
  17. # a fetch_supported_languages() for XPath engines isn't available right now
  18. # _brave = ENGINES_LANGUAGES['brave'].keys()
  19. def get(*args, **kwargs):
  20. if 'timeout' not in kwargs:
  21. kwargs['timeout'] = settings['outgoing']['request_timeout']
  22. kwargs['raise_for_httperror'] = True
  23. return http_get(*args, **kwargs)
  24. def brave(query, _lang):
  25. # brave search autocompleter
  26. url = 'https://search.brave.com/api/suggest?'
  27. url += urlencode({'q': query})
  28. country = 'all'
  29. # if lang in _brave:
  30. # country = lang
  31. kwargs = {'cookies': {'country': country}}
  32. resp = get(url, **kwargs)
  33. results = []
  34. if resp.ok:
  35. data = resp.json()
  36. for item in data[1]:
  37. results.append(item)
  38. return results
  39. def dbpedia(query, _lang):
  40. # dbpedia autocompleter, no HTTPS
  41. autocomplete_url = 'https://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'
  42. response = get(autocomplete_url + urlencode(dict(QueryString=query)))
  43. results = []
  44. if response.ok:
  45. dom = lxml.etree.fromstring(response.content)
  46. results = dom.xpath('//Result/Label//text()')
  47. return results
  48. def duckduckgo(query, sxng_locale):
  49. """Autocomplete from DuckDuckGo. Supports DuckDuckGo's languages"""
  50. traits = engines['duckduckgo'].traits
  51. args = {
  52. 'q': query,
  53. 'kl': traits.get_region(sxng_locale, traits.all_locale),
  54. }
  55. url = 'https://duckduckgo.com/ac/?type=list&' + urlencode(args)
  56. resp = get(url)
  57. ret_val = []
  58. if resp.ok:
  59. j = resp.json()
  60. if len(j) > 1:
  61. ret_val = j[1]
  62. return ret_val
  63. def google_complete(query, sxng_locale):
  64. """Autocomplete from Google. Supports Google's languages and subdomains
  65. (:py:obj:`searx.engines.google.get_google_info`) by using the async REST
  66. API::
  67. https://{subdomain}/complete/search?{args}
  68. """
  69. google_info = google.get_google_info({'searxng_locale': sxng_locale}, engines['google'].traits)
  70. url = 'https://{subdomain}/complete/search?{args}'
  71. args = urlencode(
  72. {
  73. 'q': query,
  74. 'client': 'gws-wiz',
  75. 'hl': google_info['params']['hl'],
  76. }
  77. )
  78. results = []
  79. resp = get(url.format(subdomain=google_info['subdomain'], args=args))
  80. if resp.ok:
  81. json_txt = resp.text[resp.text.find('[') : resp.text.find(']', -3) + 1]
  82. data = json.loads(json_txt)
  83. for item in data[0]:
  84. results.append(lxml.html.fromstring(item[0]).text_content())
  85. return results
  86. def seznam(query, _lang):
  87. # seznam search autocompleter
  88. url = 'https://suggest.seznam.cz/fulltext/cs?{query}'
  89. resp = get(
  90. url.format(
  91. query=urlencode(
  92. {'phrase': query, 'cursorPosition': len(query), 'format': 'json-2', 'highlight': '1', 'count': '6'}
  93. )
  94. )
  95. )
  96. if not resp.ok:
  97. return []
  98. data = resp.json()
  99. return [
  100. ''.join([part.get('text', '') for part in item.get('text', [])])
  101. for item in data.get('result', [])
  102. if item.get('itemType', None) == 'ItemType.TEXT'
  103. ]
  104. def startpage(query, sxng_locale):
  105. """Autocomplete from Startpage. Supports Startpage's languages"""
  106. lui = engines['startpage'].traits.get_language(sxng_locale, 'english')
  107. url = 'https://startpage.com/suggestions?{query}'
  108. resp = get(url.format(query=urlencode({'q': query, 'segment': 'startpage.udog', 'lui': lui})))
  109. data = resp.json()
  110. return [e['text'] for e in data.get('suggestions', []) if 'text' in e]
  111. def swisscows(query, _lang):
  112. # swisscows autocompleter
  113. url = 'https://swisscows.ch/api/suggest?{query}&itemsCount=5'
  114. resp = json.loads(get(url.format(query=urlencode({'query': query}))).text)
  115. return resp
  116. def qwant(query, sxng_locale):
  117. """Autocomplete from Qwant. Supports Qwant's regions."""
  118. results = []
  119. locale = engines['qwant'].traits.get_region(sxng_locale, 'en_US')
  120. url = 'https://api.qwant.com/v3/suggest?{query}'
  121. resp = get(url.format(query=urlencode({'q': query, 'locale': locale, 'version': '2'})))
  122. if resp.ok:
  123. data = resp.json()
  124. if data['status'] == 'success':
  125. for item in data['data']['items']:
  126. results.append(item['value'])
  127. return results
  128. def wikipedia(query, sxng_locale):
  129. """Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc)."""
  130. results = []
  131. eng_traits = engines['wikipedia'].traits
  132. wiki_lang = eng_traits.get_language(sxng_locale, 'en')
  133. wiki_netloc = eng_traits.custom['wiki_netloc'].get(wiki_lang, 'en.wikipedia.org')
  134. url = 'https://{wiki_netloc}/w/api.php?{args}'
  135. args = urlencode(
  136. {
  137. 'action': 'opensearch',
  138. 'format': 'json',
  139. 'formatversion': '2',
  140. 'search': query,
  141. 'namespace': '0',
  142. 'limit': '10',
  143. }
  144. )
  145. resp = get(url.format(args=args, wiki_netloc=wiki_netloc))
  146. if resp.ok:
  147. data = resp.json()
  148. if len(data) > 1:
  149. results = data[1]
  150. return results
  151. def yandex(query, _lang):
  152. # yandex autocompleter
  153. url = "https://suggest.yandex.com/suggest-ff.cgi?{0}"
  154. resp = json.loads(get(url.format(urlencode(dict(part=query)))).text)
  155. if len(resp) > 1:
  156. return resp[1]
  157. return []
  158. backends = {
  159. 'dbpedia': dbpedia,
  160. 'duckduckgo': duckduckgo,
  161. 'google': google_complete,
  162. 'seznam': seznam,
  163. 'startpage': startpage,
  164. 'swisscows': swisscows,
  165. 'qwant': qwant,
  166. 'wikipedia': wikipedia,
  167. 'brave': brave,
  168. 'yandex': yandex,
  169. }
  170. def search_autocomplete(backend_name, query, sxng_locale):
  171. backend = backends.get(backend_name)
  172. if backend is None:
  173. return []
  174. if engines[backend_name].traits.data_type != "traits_v1":
  175. # vintage / deprecated
  176. if not sxng_locale or sxng_locale == 'all':
  177. sxng_locale = 'en'
  178. else:
  179. sxng_locale = sxng_locale.split('-')[0]
  180. try:
  181. return backend(query, sxng_locale)
  182. except (HTTPError, SearxEngineResponseException):
  183. return []