bing_images.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. from searx.engines.bing import _fetch_supported_languages, supported_languages_url, language_aliases
  17. # engine dependent config
  18. categories = ['images']
  19. paging = True
  20. safesearch = True
  21. time_range_support = True
  22. language_support = True
  23. supported_languages_url = 'https://www.bing.com/account/general'
  24. number_of_results = 28
  25. # search-url
  26. base_url = 'https://www.bing.com/'
  27. search_string = 'images/search'\
  28. '?{query}'\
  29. '&count={count}'\
  30. '&first={first}'\
  31. '&FORM=IBASEP'
  32. time_range_string = '&qft=+filterui:age-lt{interval}'
  33. time_range_dict = {'day': '1440',
  34. 'week': '10080',
  35. 'month': '43200',
  36. 'year': '525600'}
  37. # safesearch definitions
  38. safesearch_types = {2: 'STRICT',
  39. 1: 'DEMOTE',
  40. 0: 'OFF'}
  41. # do search-request
  42. def request(query, params):
  43. offset = ((params['pageno'] - 1) * number_of_results) + 1
  44. search_path = search_string.format(
  45. query=urlencode({'q': query}),
  46. count=number_of_results,
  47. first=offset)
  48. language = match_language(params['language'], supported_languages, language_aliases).lower()
  49. params['cookies']['SRCHHPGUSR'] = \
  50. 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  51. params['cookies']['_EDGE_S'] = 'mkt=' + language +\
  52. '&ui=' + language + '&F=1'
  53. params['url'] = base_url + search_path
  54. if params['time_range'] in time_range_dict:
  55. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  56. return params
  57. # get response from search-request
  58. def response(resp):
  59. results = []
  60. dom = html.fromstring(resp.text)
  61. # parse results
  62. for result in dom.xpath('//div[@class="imgpt"]'):
  63. img_format = result.xpath('./div[contains(@class, "img_info")]/span/text()')[0]
  64. # Microsoft seems to experiment with this code so don't make the path too specific,
  65. # just catch the text section for the first anchor in img_info assuming this to be
  66. # the originating site.
  67. source = result.xpath('./div[contains(@class, "img_info")]//a/text()')[0]
  68. try:
  69. m = loads(result.xpath('./a/@m')[0])
  70. # strip 'Unicode private use area' highlighting, they render to Tux
  71. # the Linux penguin and a standing diamond on my machine...
  72. title = m.get('t', '').replace(u'\ue000', '').replace(u'\ue001', '')
  73. results.append({'template': 'images.html',
  74. 'url': m['purl'],
  75. 'thumbnail_src': m['turl'],
  76. 'img_src': m['murl'],
  77. 'content': '',
  78. 'title': title,
  79. 'source': source,
  80. 'img_format': img_format})
  81. except:
  82. continue
  83. return results