on master help sign in
Merge pull request #1 from pointhi/news

News

commitfe35c86c947f503ae2e1e7633d3355204e48ae12 authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateTue 4 Mar 2014, 13:26 (13 years ago) parent4627788dcd1b99
4 files changed+152
searx/engines/bing_news.py+51 searx/engines/google_news.py+37 searx/engines/yahoo_news.py+51 searx/settings.yml+13
searx/engines/bing_news.pynew file+51
@@ -0,0 +1,51 @@
1+from urllib import urlencode
2+from cgi import escape
3+from lxml import html
4+
5+categories = ['news']
6+
7+base_url = 'http://www.bing.com/'
8+search_string = 'news/search?{query}&first={offset}'
9+paging = True
10+language_support = True
11+
12+
13+def request(query, params):
14+ offset = (params['pageno'] - 1) * 10 + 1
15+ if params['language'] == 'all':
16+ language = 'en-US'
17+ else:
18+ language = params['language'].replace('_', '-')
19+ search_path = search_string.format(
20+ query=urlencode({'q': query, 'setmkt': language}),
21+ offset=offset)
22+
23+ params['cookies']['SRCHHPGUSR'] = \
24+ 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
25+ #if params['category'] == 'images':
26+ # params['url'] = base_url + 'images/' + search_path
27+ params['url'] = base_url + search_path
28+ return params
29+
30+
31+def response(resp):
32+ global base_url
33+ results = []
34+ dom = html.fromstring(resp.content)
35+ for result in dom.xpath('//div[@class="sa_cc"]'):
36+ link = result.xpath('.//h3/a')[0]
37+ url = link.attrib.get('href')
38+ title = ' '.join(link.xpath('.//text()'))
39+ content = escape(' '.join(result.xpath('.//p//text()')))
40+ results.append({'url': url, 'title': title, 'content': content})
41+
42+ if results:
43+ return results
44+
45+ for result in dom.xpath('//li[@class="b_algo"]'):
46+ link = result.xpath('.//h2/a')[0]
47+ url = link.attrib.get('href')
48+ title = ' '.join(link.xpath('.//text()'))
49+ content = escape(' '.join(result.xpath('.//p//text()')))
50+ results.append({'url': url, 'title': title, 'content': content})
51+ return results
searx/engines/google_news.pynew file+37
@@ -0,0 +1,37 @@
1+#!/usr/bin/env python
2+
3+from urllib import urlencode
4+from json import loads
5+
6+categories = ['news']
7+
8+url = 'https://ajax.googleapis.com/'
9+search_url = url + 'ajax/services/search/news?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={language}' # noqa
10+
11+paging = True
12+language_support = True
13+
14+
15+def request(query, params):
16+ offset = (params['pageno'] - 1) * 8
17+ language = 'en-US'
18+ if params['language'] != 'all':
19+ language = params['language'].replace('_', '-')
20+ params['url'] = search_url.format(offset=offset,
21+ query=urlencode({'q': query}),
22+ language=language)
23+ return params
24+
25+
26+def response(resp):
27+ results = []
28+ search_res = loads(resp.text)
29+
30+ if not search_res.get('responseData', {}).get('results'):
31+ return []
32+
33+ for result in search_res['responseData']['results']:
34+ results.append({'url': result['unescapedUrl'],
35+ 'title': result['titleNoFormatting'],
36+ 'content': result['content']})
37+ return results
searx/engines/yahoo_news.pynew file+51
@@ -0,0 +1,51 @@
1+#!/usr/bin/env python
2+
3+from urllib import urlencode
4+from urlparse import unquote
5+from lxml import html
6+from searx.engines.xpath import extract_text, extract_url
7+
8+categories = ['news']
9+search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
10+results_xpath = '//div[@class="res"]'
11+url_xpath = './/h3/a/@href'
12+title_xpath = './/h3/a'
13+content_xpath = './/div[@class="abstr"]'
14+suggestion_xpath = '//div[@id="satat"]//a'
15+
16+paging = True
17+
18+
19+def request(query, params):
20+ offset = (params['pageno'] - 1) * 10 + 1
21+ if params['language'] == 'all':
22+ language = 'en'
23+ else:
24+ language = params['language'].split('_')[0]
25+ params['url'] = search_url.format(offset=offset,
26+ query=urlencode({'p': query}))
27+ params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
28+ .format(lang=language)
29+ return params
30+
31+
32+def response(resp):
33+ results = []
34+ dom = html.fromstring(resp.text)
35+
36+ for result in dom.xpath(results_xpath):
37+ url_string = extract_url(result.xpath(url_xpath), search_url)
38+ start = url_string.find('/RU=')+4
39+ end = url_string.rfind('/RS')
40+ url = unquote(url_string[start:end])
41+ title = extract_text(result.xpath(title_xpath)[0])
42+ content = extract_text(result.xpath(content_xpath)[0])
43+ results.append({'url': url, 'title': title, 'content': content})
44+
45+ if not suggestion_xpath:
46+ return results
47+
48+ for suggestion in dom.xpath(suggestion_xpath):
49+ results.append({'suggestion': extract_text(suggestion)})
50+
51+ return results
searx/settings.yml+13
@@ -17,6 +17,11 @@ engines:
1717 locale : en-US
1818 shortcut : bi
1919
20+ - name : bing news
21+ engine : bing_news
22+ locale : en-US
23+ shortcut : bin
24+
2025 - name : currency
2126 engine : currency_convert
2227 categories : general
@@ -61,6 +66,10 @@ engines:
6166 engine : google_images
6267 shortcut : goi
6368
69+ - name : google news
70+ engine : google_news
71+ shortcut : gon
72+
6473 - name : piratebay
6574 engine : piratebay
6675 categories : videos, music, files
@@ -112,6 +121,10 @@ engines:
112121 engine : yahoo
113122 shortcut : yh
114123
124+ - name : yahoo news
125+ engine : yahoo_news
126+ shortcut : yhn
127+
115128 - name : youtube
116129 engine : youtube
117130 categories : videos