bing_images.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. @todo currently there are up to 35 images receive per page,
  11. because bing does not parse count=10.
  12. limited response to 10 images
  13. """
  14. from lxml import html
  15. from json import loads
  16. import re
  17. from searx.engines.bing import _fetch_supported_languages, supported_languages_url
  18. from searx.url_utils import urlencode
  19. # engine dependent config
  20. categories = ['images']
  21. paging = True
  22. safesearch = True
  23. time_range_support = True
  24. # search-url
  25. base_url = 'https://www.bing.com/'
  26. search_string = 'images/search?{query}&count=10&first={offset}'
  27. time_range_string = '&qft=+filterui:age-lt{interval}'
  28. time_range_dict = {'day': '1440',
  29. 'week': '10080',
  30. 'month': '43200',
  31. 'year': '525600'}
  32. # safesearch definitions
  33. safesearch_types = {2: 'STRICT',
  34. 1: 'DEMOTE',
  35. 0: 'OFF'}
  36. _quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
  37. # do search-request
  38. def request(query, params):
  39. offset = (params['pageno'] - 1) * 10 + 1
  40. # required for cookie
  41. if params['language'] == 'all':
  42. language = 'en-US'
  43. else:
  44. language = params['language']
  45. search_path = search_string.format(
  46. query=urlencode({'q': query}),
  47. offset=offset)
  48. params['cookies']['SRCHHPGUSR'] = \
  49. 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0] +\
  50. '&ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  51. params['url'] = base_url + search_path
  52. if params['time_range'] in time_range_dict:
  53. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  54. return params
  55. # get response from search-request
  56. def response(resp):
  57. results = []
  58. dom = html.fromstring(resp.text)
  59. # parse results
  60. for result in dom.xpath('//div[@id="mmComponent_images_1"]/ul/li/div/div[@class="imgpt"]'):
  61. link = result.xpath('./a')[0]
  62. # TODO find actual title
  63. title = link.xpath('.//img/@alt')[0]
  64. # parse json-data (it is required to add a space, to make it parsable)
  65. json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('m')))
  66. url = json_data.get('purl')
  67. img_src = json_data.get('murl')
  68. thumb_json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('mad')))
  69. thumbnail = thumb_json_data.get('turl')
  70. # append result
  71. results.append({'template': 'images.html',
  72. 'url': url,
  73. 'title': title,
  74. 'content': '',
  75. 'thumbnail_src': thumbnail,
  76. 'img_src': img_src})
  77. # TODO stop parsing if 10 images are found
  78. # if len(results) >= 10:
  79. # break
  80. # return results
  81. return results