bing_images.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """
  2. Bing (Images)
  3. @website https://www.bing.com/images
  4. @provide-api yes (http://datamarket.azure.com/dataset/bing/search),
  5. max. 5000 query/month
  6. @using-api no (because of query limit)
  7. @results HTML (using search portal)
  8. @stable no (HTML can change)
  9. @parse url, title, img_src
  10. """
  11. from lxml import html
  12. from json import loads
  13. import re
  14. from searx.url_utils import urlencode
  15. from searx.utils import match_language
  16. # engine dependent config
  17. categories = ['images']
  18. paging = True
  19. safesearch = True
  20. time_range_support = True
  21. language_support = True
  22. supported_languages_url = 'https://www.bing.com/account/general'
  23. number_of_results = 28
  24. # search-url
  25. base_url = 'https://www.bing.com/'
  26. search_string = 'images/search'\
  27. '?{query}'\
  28. '&count={count}'\
  29. '&first={first}'\
  30. '&FORM=IBASEP'
  31. time_range_string = '&qft=+filterui:age-lt{interval}'
  32. time_range_dict = {'day': '1440',
  33. 'week': '10080',
  34. 'month': '43200',
  35. 'year': '525600'}
  36. # safesearch definitions
  37. safesearch_types = {2: 'STRICT',
  38. 1: 'DEMOTE',
  39. 0: 'OFF'}
  40. # do search-request
  41. def request(query, params):
  42. offset = ((params['pageno'] - 1) * number_of_results) + 1
  43. search_path = search_string.format(
  44. query=urlencode({'q': query}),
  45. count=number_of_results,
  46. first=offset)
  47. language = match_language(params['language'], supported_languages, language_aliases).lower()
  48. params['cookies']['SRCHHPGUSR'] = \
  49. 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  50. params['cookies']['_EDGE_S'] = 'mkt=' + language +\
  51. '&ui=' + language + '&F=1'
  52. params['url'] = base_url + search_path
  53. if params['time_range'] in time_range_dict:
  54. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  55. return params
  56. # get response from search-request
  57. def response(resp):
  58. results = []
  59. dom = html.fromstring(resp.text)
  60. # parse results
  61. for result in dom.xpath('//div[@class="imgpt"]'):
  62. img_format = result.xpath('./div[contains(@class, "img_info")]/span/text()')[0]
  63. # Microsoft seems to experiment with this code so don't make the path too specific,
  64. # just catch the text section for the first anchor in img_info assuming this to be
  65. # the originating site.
  66. source = result.xpath('./div[contains(@class, "img_info")]//a/text()')[0]
  67. try:
  68. m = loads(result.xpath('./a/@m')[0])
  69. # strip 'Unicode private use area' highlighting, they render to Tux
  70. # the Linux penguin and a standing diamond on my machine...
  71. title = m.get('t', '').replace(u'\ue000', '').replace(u'\ue001', '')
  72. results.append({'template': 'images.html',
  73. 'url': m['purl'],
  74. 'thumbnail_src': m['turl'],
  75. 'img_src': m['murl'],
  76. 'content': '',
  77. 'title': title,
  78. 'source': source,
  79. 'img_format': img_format})
  80. except:
  81. continue
  82. return results
  83. # get supported languages from their site
  84. def _fetch_supported_languages(resp):
  85. supported_languages = []
  86. dom = html.fromstring(resp.text)
  87. regions_xpath = '//div[@id="region-section-content"]' \
  88. + '//ul[@class="b_vList"]/li/a/@href'
  89. regions = dom.xpath(regions_xpath)
  90. for region in regions:
  91. code = re.search('setmkt=[^\&]+', region).group()[7:]
  92. if code == 'nb-NO':
  93. code = 'no-NO'
  94. supported_languages.append(code)
  95. return supported_languages