autocomplete.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. '''
  2. searx is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Affero General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
  13. '''
  14. from lxml import etree
  15. from json import loads
  16. from urllib.parse import urlencode
  17. from httpx import HTTPError
  18. from searx import settings
  19. from searx.data import ENGINES_LANGUAGES
  20. from searx.network import get as http_get
  21. from searx.exceptions import SearxEngineResponseException
  22. def get(*args, **kwargs):
  23. if 'timeout' not in kwargs:
  24. kwargs['timeout'] = settings['outgoing']['request_timeout']
  25. kwargs['raise_for_httperror'] = True
  26. return http_get(*args, **kwargs)
  27. def brave(query, lang):
  28. # brave search autocompleter
  29. url = 'https://search.brave.com/api/suggest?{query}'
  30. resp = get(url.format(query=urlencode({'q': query})))
  31. results = []
  32. if resp.ok:
  33. data = loads(resp.text)
  34. for item in data[1]:
  35. results.append(item)
  36. return results
  37. def dbpedia(query, lang):
  38. # dbpedia autocompleter, no HTTPS
  39. autocomplete_url = 'https://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'
  40. response = get(autocomplete_url + urlencode(dict(QueryString=query)))
  41. results = []
  42. if response.ok:
  43. dom = etree.fromstring(response.content)
  44. results = dom.xpath('//Result/Label//text()')
  45. return results
  46. def duckduckgo(query, lang):
  47. # duckduckgo autocompleter
  48. url = 'https://ac.duckduckgo.com/ac/?{0}&type=list'
  49. resp = loads(get(url.format(urlencode(dict(q=query)))).text)
  50. if len(resp) > 1:
  51. return resp[1]
  52. return []
  53. def google(query, lang):
  54. # google autocompleter
  55. autocomplete_url = 'https://suggestqueries.google.com/complete/search?client=toolbar&'
  56. response = get(autocomplete_url + urlencode(dict(hl=lang, q=query)))
  57. results = []
  58. if response.ok:
  59. dom = etree.fromstring(response.text)
  60. results = dom.xpath('//suggestion/@data')
  61. return results
  62. def startpage(query, lang):
  63. # startpage autocompleter
  64. lui = ENGINES_LANGUAGES['startpage'].get(lang, 'english')
  65. url = 'https://startpage.com/suggestions?{query}'
  66. resp = get(url.format(query=urlencode({'q': query, 'segment': 'startpage.udog', 'lui': lui})))
  67. data = resp.json()
  68. return [e['text'] for e in data.get('suggestions', []) if 'text' in e]
  69. def swisscows(query, lang):
  70. # swisscows autocompleter
  71. url = 'https://swisscows.ch/api/suggest?{query}&itemsCount=5'
  72. resp = loads(get(url.format(query=urlencode({'query': query}))).text)
  73. return resp
  74. def qwant(query, lang):
  75. # qwant autocompleter (additional parameter : lang=en_en&count=xxx )
  76. url = 'https://api.qwant.com/api/suggest?{query}'
  77. resp = get(url.format(query=urlencode({'q': query, 'lang': lang})))
  78. results = []
  79. if resp.ok:
  80. data = loads(resp.text)
  81. if data['status'] == 'success':
  82. for item in data['data']['items']:
  83. results.append(item['value'])
  84. return results
  85. def wikipedia(query, lang):
  86. # wikipedia autocompleter
  87. url = 'https://' + lang + '.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json'
  88. resp = loads(get(url.format(urlencode(dict(search=query)))).text)
  89. if len(resp) > 1:
  90. return resp[1]
  91. return []
  92. backends = {
  93. 'dbpedia': dbpedia,
  94. 'duckduckgo': duckduckgo,
  95. 'google': google,
  96. 'startpage': startpage,
  97. 'swisscows': swisscows,
  98. 'qwant': qwant,
  99. 'wikipedia': wikipedia,
  100. 'brave': brave,
  101. }
  102. def search_autocomplete(backend_name, query, lang):
  103. backend = backends.get(backend_name)
  104. if backend is None:
  105. return []
  106. try:
  107. return backend(query, lang)
  108. except (HTTPError, SearxEngineResponseException):
  109. return []