@@ -1,50 +1,86 @@
1+## Bing (News)
2+#
3+# @website https://www.bing.com/news
4+# @provide-api yes (http://datamarket.azure.com/dataset/bing/search), max. 5000 query/month
5+#
6+# @using-api no (because of query limit)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, publishedDate
10+
111from urllib import urlencode
212from cgi import escape
313from lxml import html
14+from datetime import datetime, timedelta
15+from dateutil import parser
16+import re
417
18+# engine dependent config
519categories = ['news']
6−
7−base_url = 'http://www.bing.com/'
8−search_string = 'news/search?{query}&first={offset}'
920paging = True
1021language_support = True
1122
23+# search-url
24+base_url = 'https://www.bing.com/'
25+search_string = 'news/search?{query}&first={offset}'
1226
27+# do search-request
1328def request(query, params):
1429 offset = (params['pageno'] - 1) * 10 + 1
30+
1531 if params['language'] == 'all':
1632 language = 'en-US'
1733 else:
1834 language = params['language'].replace('_', '-')
35+
1936 search_path = search_string.format(
2037 query=urlencode({'q': query, 'setmkt': language}),
2138 offset=offset)
2239
2340 params['cookies']['SRCHHPGUSR'] = \
2441 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
25− #if params['category'] == 'images':
26− # params['url'] = base_url + 'images/' + search_path
42+
2743 params['url'] = base_url + search_path
2844 return params
2945
3046
47+# get response from search-request
3148def response(resp):
3249 results = []
50+
3351 dom = html.fromstring(resp.content)
34− for result in dom.xpath('//div[@class="sa_cc"]'):
35− link = result.xpath('.//h3/a')[0]
52+
53+ # parse results
54+ for result in dom.xpath('//div[@class="sn_r"]'):
55+ link = result.xpath('.//div[@class="newstitle"]/a')[0]
3656 url = link.attrib.get('href')
3757 title = ' '.join(link.xpath('.//text()'))
38− content = escape(' '.join(result.xpath('.//p//text()')))
39− results.append({'url': url, 'title': title, 'content': content})
58+ content = escape(' '.join(result.xpath('.//div[@class="sn_txt"]/div//span[@class="sn_snip"]//text()')))
59+
60+ # parse publishedDate
61+ publishedDate = escape(' '.join(result.xpath('.//div[@class="sn_txt"]/div//span[@class="sn_ST"]//span[@class="sn_tm"]//text()')))
4062
41− if results:
42− return results
63+ if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
64+ timeNumbers = re.findall(r'\d+', publishedDate)
65+ publishedDate = datetime.now()\
66+ - timedelta(minutes=int(timeNumbers[0]))
67+ elif re.match("^[0-9]+ hour(s|) ago$", publishedDate):
68+ timeNumbers = re.findall(r'\d+', publishedDate)
69+ publishedDate = datetime.now()\
70+ - timedelta(hours=int(timeNumbers[0]))
71+ elif re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate):
72+ timeNumbers = re.findall(r'\d+', publishedDate)
73+ publishedDate = datetime.now()\
74+ - timedelta(hours=int(timeNumbers[0]))\
75+ - timedelta(minutes=int(timeNumbers[1]))
76+ else:
77+ publishedDate = parser.parse(publishedDate)
4378
44− for result in dom.xpath('//li[@class="b_algo"]'):
45− link = result.xpath('.//h2/a')[0]
46− url = link.attrib.get('href')
47− title = ' '.join(link.xpath('.//text()'))
48− content = escape(' '.join(result.xpath('.//p//text()')))
49− results.append({'url': url, 'title': title, 'content': content})
79+ # append result
80+ results.append({'url': url,
81+ 'title': title,
82+ 'publishedDate': publishedDate,
83+ 'content': content})
84+
85+ # return results
5086 return results