logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

duckduckgo.py (3314B)


  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.engines.xpath import extract_text
  15. from searx.poolrequests import get
  16. from searx.url_utils import urlencode
  17. from searx.utils import match_language
  18. # engine dependent config
  19. categories = ['general']
  20. paging = True
  21. language_support = True
  22. supported_languages_url = 'https://duckduckgo.com/util/u172.js'
  23. time_range_support = True
  24. language_aliases = {
  25. 'ar-SA': 'ar-XA',
  26. 'es-419': 'es-XL',
  27. 'ja': 'jp-JP',
  28. 'ko': 'kr-KR',
  29. 'sl-SI': 'sl-SL',
  30. 'zh-TW': 'tzh-TW',
  31. 'zh-HK': 'tzh-HK'
  32. }
  33. # search-url
  34. url = 'https://duckduckgo.com/html?{query}&s={offset}&dc={dc_param}'
  35. time_range_url = '&df={range}'
  36. time_range_dict = {'day': 'd',
  37. 'week': 'w',
  38. 'month': 'm'}
  39. # specific xpath variables
  40. result_xpath = '//div[@class="result results_links results_links_deep web-result "]' # noqa
  41. url_xpath = './/a[@class="result__a"]/@href'
  42. title_xpath = './/a[@class="result__a"]'
  43. content_xpath = './/a[@class="result__snippet"]'
  44. # match query's language to a region code that duckduckgo will accept
  45. def get_region_code(lang, lang_list=[]):
  46. lang_code = match_language(lang, lang_list, language_aliases, 'wt-WT')
  47. lang_parts = lang_code.split('-')
  48. # country code goes first
  49. return lang_parts[1].lower() + '-' + lang_parts[0].lower()
  50. # do search-request
  51. def request(query, params):
  52. if params['time_range'] and params['time_range'] not in time_range_dict:
  53. return params
  54. offset = (params['pageno'] - 1) * 30
  55. region_code = get_region_code(params['language'], supported_languages)
  56. params['url'] = url.format(
  57. query=urlencode({'q': query, 'kl': region_code}), offset=offset, dc_param=offset)
  58. if params['time_range'] in time_range_dict:
  59. params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
  60. return params
  61. # get response from search-request
  62. def response(resp):
  63. results = []
  64. doc = fromstring(resp.text)
  65. # parse results
  66. for r in doc.xpath(result_xpath):
  67. try:
  68. res_url = r.xpath(url_xpath)[-1]
  69. except:
  70. continue
  71. if not res_url:
  72. continue
  73. title = extract_text(r.xpath(title_xpath))
  74. content = extract_text(r.xpath(content_xpath))
  75. # append result
  76. results.append({'title': title,
  77. 'content': content,
  78. 'url': res_url})
  79. # return results
  80. return results
  81. # get supported languages from their site
  82. def _fetch_supported_languages(resp):
  83. # response is a js file with regions as an embedded object
  84. response_page = resp.text
  85. response_page = response_page[response_page.find('regions:{') + 8:]
  86. response_page = response_page[:response_page.find('}') + 1]
  87. regions_json = loads(response_page)
  88. supported_languages = map((lambda x: x[3:] + '-' + x[:2].upper()), regions_json.keys())
  89. return list(supported_languages)