on master help sign in
update bing engines and fix bing_news
commit4b1e0423a03cacf9cc8663aba1a1e0585865afa1 authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateMon 1 Sep 2014, 14:38 (12 years ago) parent55dfb30
2 files changed+92−23
searx/engines/bing.py+39−6 searx/engines/bing_news.py+53−17
searx/engines/bing.py+39−6
@@ -1,48 +1,81 @@
1+## Bing (Web)
2+#
3+# @website https://www.bing.com
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
10+#
11+# @todo publishedDate
12+
113from urllib import urlencode
214from cgi import escape
315from lxml import html
416
5−base_url = 'http://www.bing.com/'
6−search_string = 'search?{query}&first={offset}'
17+# engine dependent config
18+categories = ['general']
719paging = True
820language_support = True
921
22+# search-url
23+base_url = 'https://www.bing.com/'
24+search_string = 'search?{query}&first={offset}'
1025
26+# do search-request
1127def request(query, params):
1228 offset = (params['pageno'] - 1) * 10 + 1
29+
1330 if params['language'] == 'all':
1431 language = 'en-US'
1532 else:
1633 language = params['language'].replace('_', '-')
34+
1735 search_path = search_string.format(
1836 query=urlencode({'q': query, 'setmkt': language}),
1937 offset=offset)
2038
2139 params['cookies']['SRCHHPGUSR'] = \
2240 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
23− #if params['category'] == 'images':
24− # params['url'] = base_url + 'images/' + search_path
41+
2542 params['url'] = base_url + search_path
2643 return params
2744
2845
46+# get response from search-request
2947def response(resp):
3048 results = []
49+
3150 dom = html.fromstring(resp.content)
51+
52+ # parse results
3253 for result in dom.xpath('//div[@class="sa_cc"]'):
3354 link = result.xpath('.//h3/a')[0]
3455 url = link.attrib.get('href')
3556 title = ' '.join(link.xpath('.//text()'))
3657 content = escape(' '.join(result.xpath('.//p//text()')))
37− results.append({'url': url, 'title': title, 'content': content})
3858
59+ # append result
60+ results.append({'url': url,
61+ 'title': title,
62+ 'content': content})
63+
64+ # return results if something is found
3965 if results:
4066 return results
4167
68+ # parse results again if nothing is found yet
4269 for result in dom.xpath('//li[@class="b_algo"]'):
4370 link = result.xpath('.//h2/a')[0]
4471 url = link.attrib.get('href')
4572 title = ' '.join(link.xpath('.//text()'))
4673 content = escape(' '.join(result.xpath('.//p//text()')))
47− results.append({'url': url, 'title': title, 'content': content})
74+
75+ # append result
76+ results.append({'url': url,
77+ 'title': title,
78+ 'content': content})
79+
80+ # return results
4881 return results
searx/engines/bing_news.py+53−17
@@ -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