@@ -10,9 +10,6 @@
1010 @stable no (HTML can change)
1111 @parse url, title, img_src
1212
13− @todo currently there are up to 35 images receive per page,
14− because bing does not parse count=10.
15− limited response to 10 images
1613"""
1714
1815from lxml import html
@@ -28,10 +25,15 @@ safesearch = True
2825time_range_support = True
2926language_support = True
3027supported_languages_url = 'https://www.bing.com/account/general'
28+number_of_results = 28
3129
3230# search-url
3331base_url = 'https://www.bing.com/'
34−search_string = 'images/search?{query}&count=10&first={offset}'
32+search_string = 'images/search'\
33+ '?{query}'\
34+ '&count={count}'\
35+ '&first={first}'\
36+ '&FORM=IBASEP'
3537time_range_string = '&qft=+filterui:age-lt{interval}'
3638time_range_dict = {'day': '1440',
3739 'week': '10080',
@@ -44,16 +46,14 @@ safesearch_types = {2: 'STRICT',
4446 0: 'OFF'}
4547
4648
47−_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
48−
49−
5049# do search-request
5150def request(query, params):
52− offset = (params['pageno'] - 1) * 10 + 1
51+ offset = ((params['pageno'] - 1) * number_of_results) + 1
5352
5453 search_path = search_string.format(
5554 query=urlencode({'q': query}),
56− offset=offset)
55+ count=number_of_results,
56+ first=offset)
5757
5858 language = match_language(params['language'], supported_languages, language_aliases).lower()
5959
@@ -77,32 +77,31 @@ def response(resp):
7777 dom = html.fromstring(resp.text)
7878
7979 # parse results
80− for result in dom.xpath('//div[@id="mmComponent_images_1"]/ul/li/div/div[@class="imgpt"]'):
81− link = result.xpath('./a')[0]
82−
83− # TODO find actual title
84− title = link.xpath('.//img/@alt')[0]
85−
86− # parse json-data (it is required to add a space, to make it parsable)
87− json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('m')))
88−
89− url = json_data.get('purl')
90− img_src = json_data.get('murl')
91− thumbnail = json_data.get('turl')
92−
93− # append result
94− results.append({'template': 'images.html',
95− 'url': url,
96− 'title': title,
97− 'content': '',
98− 'thumbnail_src': thumbnail,
99− 'img_src': img_src})
100−
101− # TODO stop parsing if 10 images are found
102− # if len(results) >= 10:
103− # break
80+ for result in dom.xpath('//div[@class="imgpt"]'):
81+
82+ img_format = result.xpath('./div[contains(@class, "img_info")]/span/text()')[0]
83+ # Microsoft seems to experiment with this code so don't make the path too specific,
84+ # just catch the text section for the first anchor in img_info assuming this to be
85+ # the originating site.
86+ source = result.xpath('./div[contains(@class, "img_info")]//a/text()')[0]
87+
88+ try:
89+ m = loads(result.xpath('./a/@m')[0])
90+
91+ # strip 'Unicode private use area' highlighting, they render to Tux
92+ # the Linux penguin and a standing diamond on my machine...
93+ title = m.get('t', '').replace(u'\ue000', '').replace(u'\ue001', '')
94+ results.append({'template': 'images.html',
95+ 'url': m['purl'],
96+ 'thumbnail_src': m['turl'],
97+ 'img_src': m['murl'],
98+ 'content': '',
99+ 'title': title,
100+ 'source': source,
101+ 'img_format': img_format})
102+ except:
103+ continue
104104
105− # return results
106105 return results
107106
108107