@@ -10,9 +10,6 @@
10 @stable no (HTML can change)
10 @stable no (HTML can change)
11 @parse url, title, img_src
11 @parse url, title, img_src
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
18from lxml import html
15from lxml import html
@@ -28,10 +25,15 @@ safesearch = True
28time_range_support = True
25time_range_support = True
29language_support = True
26language_support = True
30supported_languages_url = 'https://www.bing.com/account/general'
27supported_languages_url = 'https://www.bing.com/account/general'
28+number_of_results = 28
32# search-url
30# search-url
33base_url = 'https://www.bing.com/'
31base_url = 'https://www.bing.com/'
34−search_string = 'images/search?{query}&count=10&first={offset}'
32+search_string = 'images/search'\
35time_range_string = '&qft=+filterui:age-lt{interval}'
37time_range_string = '&qft=+filterui:age-lt{interval}'
36time_range_dict = {'day': '1440',
38time_range_dict = {'day': '1440',
37 'week': '10080',
39 'week': '10080',
@@ -44,16 +46,14 @@ safesearch_types = {2: 'STRICT',
47−_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
50# do search-request
49# do search-request
51def request(query, params):
50def request(query, params):
52− offset = (params['pageno'] - 1) * 10 + 1
51+ offset = ((params['pageno'] - 1) * number_of_results) + 1
54 search_path = search_string.format(
53 search_path = search_string.format(
55 query=urlencode({'q': query}),
54 query=urlencode({'q': query}),
55+ count=number_of_results,
56− offset=offset)
56+ first=offset)
58 language = match_language(params['language'], supported_languages, language_aliases).lower()
58 language = match_language(params['language'], supported_languages, language_aliases).lower()
@@ -77,32 +77,31 @@ def response(resp):
77 dom = html.fromstring(resp.text)
77 dom = html.fromstring(resp.text)
79 # parse results
79 # parse results
80− for result in dom.xpath('//div[@id="mmComponent_images_1"]/ul/li/div/div[@class="imgpt"]'):
80+ for result in dom.xpath('//div[@class="imgpt"]'):
81− link = result.xpath('./a')[0]
81+
82−
82+ img_format = result.xpath('./div[contains(@class, "img_info")]/span/text()')[0]
83− # TODO find actual title
83+ # Microsoft seems to experiment with this code so don't make the path too specific,
84− title = link.xpath('.//img/@alt')[0]
84+ # just catch the text section for the first anchor in img_info assuming this to be
85−
85+ # the originating site.
86− # parse json-data (it is required to add a space, to make it parsable)
86+ source = result.xpath('./div[contains(@class, "img_info")]//a/text()')[0]
87− json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('m')))
87+
89− url = json_data.get('purl')
89+ m = loads(result.xpath('./a/@m')[0])
90− img_src = json_data.get('murl')
90+
91− thumbnail = json_data.get('turl')
91+ # strip 'Unicode private use area' highlighting, they render to Tux
92−
92+ # the Linux penguin and a standing diamond on my machine...
93− # append result
93+ title = m.get('t', '').replace(u'\ue000', '').replace(u'\ue001', '')
94− results.append({'template': 'images.html',
94+ results.append({'template': 'images.html',
95− 'url': url,
95+ 'url': m['purl'],
98− 'thumbnail_src': thumbnail,
96+ 'thumbnail_src': m['turl'],
99− 'img_src': img_src})
97+ 'img_src': m['murl'],
101− # TODO stop parsing if 10 images are found
99+ 'title': title,
102− # if len(results) >= 10:
100+ 'source': source,
103− # break
101+ 'img_format': img_format})
106 return results
105 return results