duckduckgo.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """
  2. DuckDuckGo (Web)
  3. @website https://duckduckgo.com/
  4. @provide-api yes (https://duckduckgo.com/api),
  5. but not all results from search-site
  6. @using-api no
  7. @results HTML (using search portal)
  8. @stable no (HTML can change)
  9. @parse url, title, content
  10. @todo rewrite to api
  11. """
  12. from lxml.html import fromstring
  13. from json import loads
  14. from searx.utils import extract_text, match_language, eval_xpath
  15. # engine dependent config
  16. categories = ['general']
  17. paging = False
  18. language_support = True
  19. supported_languages_url = 'https://duckduckgo.com/util/u172.js'
  20. time_range_support = True
  21. language_aliases = {
  22. 'ar-SA': 'ar-XA',
  23. 'es-419': 'es-XL',
  24. 'ja': 'jp-JP',
  25. 'ko': 'kr-KR',
  26. 'sl-SI': 'sl-SL',
  27. 'zh-TW': 'tzh-TW',
  28. 'zh-HK': 'tzh-HK'
  29. }
  30. # search-url
  31. url = 'https://html.duckduckgo.com/html'
  32. time_range_dict = {'day': 'd',
  33. 'week': 'w',
  34. 'month': 'm'}
  35. # specific xpath variables
  36. result_xpath = '//div[@class="result results_links results_links_deep web-result "]' # noqa
  37. url_xpath = './/a[@class="result__a"]/@href'
  38. title_xpath = './/a[@class="result__a"]'
  39. content_xpath = './/a[@class="result__snippet"]'
  40. correction_xpath = '//div[@id="did_you_mean"]//a'
  41. # match query's language to a region code that duckduckgo will accept
  42. def get_region_code(lang, lang_list=None):
  43. if lang == 'all':
  44. return None
  45. lang_code = match_language(lang, lang_list or [], language_aliases, 'wt-WT')
  46. lang_parts = lang_code.split('-')
  47. # country code goes first
  48. return lang_parts[1].lower() + '-' + lang_parts[0].lower()
  49. def request(query, params):
  50. if params['time_range'] is not None and params['time_range'] not in time_range_dict:
  51. return params
  52. params['url'] = url
  53. params['method'] = 'POST'
  54. params['data']['b'] = ''
  55. params['data']['q'] = query
  56. params['data']['df'] = ''
  57. region_code = get_region_code(params['language'], supported_languages)
  58. if region_code:
  59. params['data']['kl'] = region_code
  60. params['cookies']['kl'] = region_code
  61. if params['time_range'] in time_range_dict:
  62. params['data']['df'] = time_range_dict[params['time_range']]
  63. return params
  64. # get response from search-request
  65. def response(resp):
  66. results = []
  67. doc = fromstring(resp.text)
  68. # parse results
  69. for i, r in enumerate(eval_xpath(doc, result_xpath)):
  70. if i >= 30:
  71. break
  72. try:
  73. res_url = eval_xpath(r, url_xpath)[-1]
  74. except:
  75. continue
  76. if not res_url:
  77. continue
  78. title = extract_text(eval_xpath(r, title_xpath))
  79. content = extract_text(eval_xpath(r, content_xpath))
  80. # append result
  81. results.append({'title': title,
  82. 'content': content,
  83. 'url': res_url})
  84. # parse correction
  85. for correction in eval_xpath(doc, correction_xpath):
  86. # append correction
  87. results.append({'correction': extract_text(correction)})
  88. # return results
  89. return results
  90. # get supported languages from their site
  91. def _fetch_supported_languages(resp):
  92. # response is a js file with regions as an embedded object
  93. response_page = resp.text
  94. response_page = response_page[response_page.find('regions:{') + 8:]
  95. response_page = response_page[:response_page.find('}') + 1]
  96. regions_json = loads(response_page)
  97. supported_languages = map((lambda x: x[3:] + '-' + x[:2].upper()), regions_json.keys())
  98. return list(supported_languages)