bing_images.py 2.9KB

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