on master help sign in
Merge pull request #88 from pointhi/engines

update and fix search engines

commitf36d1e28fae212b8b8640324d2e787b73305e2d2 authorAdam Tauber <asciimoo@gmail.com> dateMon 1 Sep 2014, 18:30 (12 years ago) parent55dfb3058a443b
11 files changed+401−101
searx/engines/bing.py+39−6 searx/engines/bing_images.py+81 searx/engines/bing_news.py+53−17 searx/engines/dailymotion.py+35−19 searx/engines/google.py+24−4 searx/engines/google_images.py+26−5 searx/engines/google_news.py+25−6 searx/engines/vimeo.py+40−22 searx/engines/yahoo.py+41−6 searx/engines/yahoo_news.py+31−11 searx/settings.yml+6−5
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_images.pynew file+81
@@ -0,0 +1,81 @@
1+## Bing (Images)
2+#
3+# @website https://www.bing.com/images
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, img_src
10+#
11+# @todo currently there are up to 35 images receive per page, because bing does not parse count=10. limited response to 10 images
12+
13+from urllib import urlencode
14+from cgi import escape
15+from lxml import html
16+from yaml import load
17+import re
18+
19+# engine dependent config
20+categories = ['images']
21+paging = True
22+
23+# search-url
24+base_url = 'https://www.bing.com/'
25+search_string = 'images/search?{query}&count=10&first={offset}'
26+
27+# do search-request
28+def request(query, params):
29+ offset = (params['pageno'] - 1) * 10 + 1
30+
31+ # required for cookie
32+ language = 'en-US'
33+
34+ search_path = search_string.format(
35+ query=urlencode({'q': query}),
36+ offset=offset)
37+
38+ params['cookies']['SRCHHPGUSR'] = \
39+ 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
40+
41+ params['url'] = base_url + search_path
42+
43+ print(params['url'])
44+
45+ return params
46+
47+
48+# get response from search-request
49+def response(resp):
50+ results = []
51+
52+ dom = html.fromstring(resp.content)
53+
54+ # init regex for yaml-parsing
55+ p = re.compile( '({|,)([a-z]+):(")')
56+
57+ # parse results
58+ for result in dom.xpath('//div[@class="dg_u"]'):
59+ link = result.xpath('./a')[0]
60+
61+ # parse yaml-data (it is required to add a space, to make it parsable)
62+ yaml_data = load(p.sub( r'\1\2: \3', link.attrib.get('m')))
63+
64+ title = link.attrib.get('t1')
65+ #url = 'http://' + link.attrib.get('t3')
66+ url = yaml_data.get('surl')
67+ img_src = yaml_data.get('imgurl')
68+
69+ # append result
70+ results.append({'template': 'images.html',
71+ 'url': url,
72+ 'title': title,
73+ 'content': '',
74+ 'img_src': img_src})
75+
76+ # TODO stop parsing if 10 images are found
77+ if len(results) >= 10:
78+ break
79+
80+ # return results
81+ 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
searx/engines/dailymotion.py+35−19
@@ -1,45 +1,61 @@
1+## Dailymotion (Videos)
2+#
3+# @website https://www.dailymotion.com
4+# @provide-api yes (http://www.dailymotion.com/developer)
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes
9+# @parse url, title, thumbnail
10+#
11+# @todo set content-parameter with correct data
12+
113from urllib import urlencode
214from json import loads
315from lxml import html
416
17+# engine dependent config
518categories = ['videos']
619locale = 'en_US'
20+paging = True
721
22+# search-url
823# see http://www.dailymotion.com/doc/api/obj-video.html
9−search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=25&page={pageno}&{query}' # noqa
10−
11−# TODO use video result template
12−content_tpl = '<a href="{0}" title="{0}" ><img src="{1}" /></a><br />'
13−
14−paging = True
24+search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=5&page={pageno}&{query}' # noqa
1525
1626
27+# do search-request
1728def request(query, params):
1829 params['url'] = search_url.format(
1930 query=urlencode({'search': query, 'localization': locale}),
2031 pageno=params['pageno'])
32+
2133 return params
2234
2335
36+# get response from search-request
2437def response(resp):
2538 results = []
39+
2640 search_res = loads(resp.text)
41+
42+ # return empty array if there are no results
2743 if not 'list' in search_res:
28− return results
44+ return []
45+
46+ # parse results
2947 for res in search_res['list']:
3048 title = res['title']
3149 url = res['url']
32− if res['thumbnail_360_url']:
33− content = content_tpl.format(url, res['thumbnail_360_url'])
34− else:
35− content = ''
36− if res['description']:
37− description = text_content_from_html(res['description'])
38− content += description[:500]
39− results.append({'url': url, 'title': title, 'content': content})
40− return results
50+ #content = res['description']
51+ content = ''
52+ thumbnail = res['thumbnail_360_url']
4153
54+ results.append({'template': 'videos.html',
55+ 'url': url,
56+ 'title': title,
57+ 'content': content,
58+ 'thumbnail': thumbnail})
4259
43−def text_content_from_html(html_string):
44− desc_html = html.fragment_fromstring(html_string, create_parent=True)
45− return desc_html.text_content()
60+ # return results
61+ return results
searx/engines/google.py+24−4
@@ -1,37 +1,57 @@
1−#!/usr/bin/env python
1+## Google (Web)
2+#
3+# @website https://www.google.com
4+# @provide-api yes (https://developers.google.com/web-search/docs/), deprecated!
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes (but deprecated)
9+# @parse url, title, content
210
311from urllib import urlencode
412from json import loads
513
14+# engine dependent config
615categories = ['general']
16+paging = True
17+language_support = True
718
19+# search-url
820url = 'https://ajax.googleapis.com/'
921search_url = url + 'ajax/services/search/web?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={language}' # noqa
1022
11−paging = True
12−language_support = True
13−
1423
24+# do search-request
1525def request(query, params):
1626 offset = (params['pageno'] - 1) * 8
27+
1728 language = 'en-US'
1829 if params['language'] != 'all':
1930 language = params['language'].replace('_', '-')
31+
2032 params['url'] = search_url.format(offset=offset,
2133 query=urlencode({'q': query}),
2234 language=language)
35+
2336 return params
2437
2538
39+# get response from search-request
2640def response(resp):
2741 results = []
42+
2843 search_res = loads(resp.text)
2944
45+ # return empty array if there are no results
3046 if not search_res.get('responseData', {}).get('results'):
3147 return []
3248
49+ # parse results
3350 for result in search_res['responseData']['results']:
51+ # append result
3452 results.append({'url': result['unescapedUrl'],
3553 'title': result['titleNoFormatting'],
3654 'content': result['content']})
55+
56+ # return results
3757 return results
searx/engines/google_images.py+26−5
@@ -1,37 +1,58 @@
1−#!/usr/bin/env python
1+## Google (Images)
2+#
3+# @website https://www.google.com
4+# @provide-api yes (https://developers.google.com/web-search/docs/), deprecated!
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes (but deprecated)
9+# @parse url, title, img_src
210
311from urllib import urlencode
412from json import loads
513
14+# engine dependent config
615categories = ['images']
16+paging = True
717
18+# search-url
819url = 'https://ajax.googleapis.com/'
920search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe=off&filter=off&{query}' # noqa
1021
11−paging = True
1222
23+# do search-request
1324def request(query, params):
1425 offset = (params['pageno'] - 1) * 8
26+
1527 params['url'] = search_url.format(query=urlencode({'q': query}),
1628 offset=offset)
29+
1730 return params
1831
1932
33+# get response from search-request
2034def response(resp):
2135 results = []
36+
2237 search_res = loads(resp.text)
23− if not search_res.get('responseData'):
24− return []
25− if not search_res['responseData'].get('results'):
38+
39+ # return empty array if there are no results
40+ if not search_res.get('responseData', {}).get('results'):
2641 return []
42+
43+ # parse results
2744 for result in search_res['responseData']['results']:
2845 href = result['originalContextUrl']
2946 title = result['title']
3047 if not result['url']:
3148 continue
49+
50+ # append result
3251 results.append({'url': href,
3352 'title': title,
3453 'content': '',
3554 'img_src': result['url'],
3655 'template': 'images.html'})
56+
57+ # return results
3758 return results
searx/engines/google_news.py+25−6
@@ -1,43 +1,62 @@
1−#!/usr/bin/env python
1+## Google (News)
2+#
3+# @website https://www.google.com
4+# @provide-api yes (https://developers.google.com/web-search/docs/), deprecated!
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes (but deprecated)
9+# @parse url, title, content, publishedDate
210
311from urllib import urlencode
412from json import loads
513from dateutil import parser
614
15+# search-url
716categories = ['news']
17+paging = True
18+language_support = True
819
20+# engine dependent config
921url = 'https://ajax.googleapis.com/'
1022search_url = url + 'ajax/services/search/news?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={language}' # noqa
1123
12−paging = True
13−language_support = True
14−
1524
25+# do search-request
1626def request(query, params):
1727 offset = (params['pageno'] - 1) * 8
28+
1829 language = 'en-US'
1930 if params['language'] != 'all':
2031 language = params['language'].replace('_', '-')
32+
2133 params['url'] = search_url.format(offset=offset,
2234 query=urlencode({'q': query}),
2335 language=language)
36+
2437 return params
2538
2639
40+# get response from search-request
2741def response(resp):
2842 results = []
43+
2944 search_res = loads(resp.text)
3045
46+ # return empty array if there are no results
3147 if not search_res.get('responseData', {}).get('results'):
3248 return []
3349
50+ # parse results
3451 for result in search_res['responseData']['results']:
35−
36−# Mon, 10 Mar 2014 16:26:15 -0700
52+ # parse publishedDate
3753 publishedDate = parser.parse(result['publishedDate'])
3854
55+ # append result
3956 results.append({'url': result['unescapedUrl'],
4057 'title': result['titleNoFormatting'],
4158 'publishedDate': publishedDate,
4259 'content': result['content']})
60+
61+ # return results
4362 return results
searx/engines/vimeo.py+40−22
@@ -1,43 +1,58 @@
1+## Vimeo (Videos)
2+#
3+# @website https://vimeo.com/
4+# @provide-api yes (http://developer.vimeo.com/api), they have a maximum count of queries/hour
5+#
6+# @using-api no (TODO, rewrite to api)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, publishedDate, thumbnail
10+#
11+# @todo rewrite to api
12+# @todo set content-parameter with correct data
13+
114from urllib import urlencode
215from HTMLParser import HTMLParser
316from lxml import html
417from searx.engines.xpath import extract_text
518from dateutil import parser
619
7−base_url = 'http://vimeo.com'
8−search_url = base_url + '/search?{query}'
9−url_xpath = None
10−content_xpath = None
11−title_xpath = None
12−results_xpath = ''
13−content_tpl = '<a href="{0}"> <img src="{2}"/> </a>'
14−publishedDate_xpath = './/p[@class="meta"]//attribute::datetime'
20+# engine dependent config
21+categories = ['videos']
22+paging = True
1523
16−# the cookie set by vimeo contains all the following values,
17−# but only __utma seems to be requiered
18−cookie = {
19− #'vuid':'918282893.1027205400'
20− # 'ab_bs':'%7B%223%22%3A279%7D'
21− '__utma': '00000000.000#0000000.0000000000.0000000000.0000000000.0'
22− # '__utmb':'18302654.1.10.1388942090'
23− #, '__utmc':'18302654'
24− #, '__utmz':'18#302654.1388942090.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)' # noqa
25− #, '__utml':'search'
26−}
24+# search-url
25+base_url = 'https://vimeo.com'
26+search_url = base_url + '/search/page:{pageno}?{query}'
27+
28+# specific xpath variables
29+url_xpath = './a/@href'
30+content_xpath = './a/img/@src'
31+title_xpath = './a/div[@class="data"]/p[@class="title"]/text()'
32+results_xpath = '//div[@id="browse_content"]/ol/li'
33+publishedDate_xpath = './/p[@class="meta"]//attribute::datetime'
2734
2835
36+# do search-request
2937def request(query, params):
30− params['url'] = search_url.format(query=urlencode({'q': query}))
31− params['cookies'] = cookie
38+ params['url'] = search_url.format(pageno=params['pageno'] ,
39+ query=urlencode({'q': query}))
40+
41+ # TODO required?
42+ params['cookies']['__utma'] = '00000000.000#0000000.0000000000.0000000000.0000000000.0'
43+
3244 return params
3345
3446
47+# get response from search-request
3548def response(resp):
3649 results = []
50+
3751 dom = html.fromstring(resp.text)
3852
3953 p = HTMLParser()
4054
55+ # parse results
4156 for result in dom.xpath(results_xpath):
4257 url = base_url + result.xpath(url_xpath)[0]
4358 title = p.unescape(extract_text(result.xpath(title_xpath)))
@@ -45,10 +60,13 @@ def response(resp):
4560 publishedDate = parser.parse(extract_text(
4661 result.xpath(publishedDate_xpath)[0]))
4762
63+ # append result
4864 results.append({'url': url,
4965 'title': title,
50− 'content': content_tpl.format(url, title, thumbnail),
66+ 'content': '',
5167 'template': 'videos.html',
5268 'publishedDate': publishedDate,
5369 'thumbnail': thumbnail})
70+
71+ # return results
5472 return results
searx/engines/yahoo.py+41−6
@@ -1,64 +1,99 @@
1−#!/usr/bin/env python
1+## Yahoo (Web)
2+#
3+# @website https://search.yahoo.com/web
4+# @provide-api yes (https://developer.yahoo.com/boss/search/), $0.80/1000 queries
5+#
6+# @using-api no (because pricing)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, suggestion
210
311from urllib import urlencode
412from urlparse import unquote
513from lxml import html
614from searx.engines.xpath import extract_text, extract_url
715
16+# engine dependent config
817categories = ['general']
9−search_url = 'http://search.yahoo.com/search?{query}&b={offset}'
18+paging = True
19+language_support = True
20+
21+# search-url
22+search_url = 'https://search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}'
23+
24+# specific xpath variables
1025results_xpath = '//div[@class="res"]'
1126url_xpath = './/h3/a/@href'
1227title_xpath = './/h3/a'
1328content_xpath = './/div[@class="abstr"]'
1429suggestion_xpath = '//div[@id="satat"]//a'
1530
16−paging = True
17−
1831
32+# remove yahoo-specific tracking-url
1933def parse_url(url_string):
2034 endings = ['/RS', '/RK']
2135 endpositions = []
2236 start = url_string.find('http', url_string.find('/RU=')+1)
37+
2338 for ending in endings:
2439 endpos = url_string.rfind(ending)
2540 if endpos > -1:
2641 endpositions.append(endpos)
2742
2843 end = min(endpositions)
44+
2945 return unquote(url_string[start:end])
3046
3147
48+# do search-request
3249def request(query, params):
3350 offset = (params['pageno'] - 1) * 10 + 1
51+
3452 if params['language'] == 'all':
3553 language = 'en'
3654 else:
3755 language = params['language'].split('_')[0]
56+
3857 params['url'] = search_url.format(offset=offset,
39− query=urlencode({'p': query}))
58+ query=urlencode({'p': query}),
59+ lang=language)
60+
61+ # TODO required?
4062 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
4163 .format(lang=language)
64+
4265 return params
4366
4467
68+# get response from search-request
4569def response(resp):
4670 results = []
71+
4772 dom = html.fromstring(resp.text)
4873
74+ # parse results
4975 for result in dom.xpath(results_xpath):
5076 try:
5177 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
5278 title = extract_text(result.xpath(title_xpath)[0])
5379 except:
5480 continue
81+
5582 content = extract_text(result.xpath(content_xpath)[0])
56− results.append({'url': url, 'title': title, 'content': content})
5783
84+ # append result
85+ results.append({'url': url,
86+ 'title': title,
87+ 'content': content})
88+
89+ # if no suggestion found, return results
5890 if not suggestion_xpath:
5991 return results
6092
93+ # parse suggestion
6194 for suggestion in dom.xpath(suggestion_xpath):
95+ # append suggestion
6296 results.append({'suggestion': extract_text(suggestion)})
6397
98+ # return results
6499 return results
searx/engines/yahoo_news.py+31−11
@@ -1,4 +1,12 @@
1−#!/usr/bin/env python
1+## Yahoo (News)
2+#
3+# @website https://news.yahoo.com
4+# @provide-api yes (https://developer.yahoo.com/boss/search/), $0.80/1000 queries
5+#
6+# @using-api no (because pricing)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, publishedDate
210
311from urllib import urlencode
412from lxml import html
@@ -8,8 +16,15 @@ from datetime import datetime, timedelta
816import re
917from dateutil import parser
1018
19+# engine dependent config
1120categories = ['news']
12−search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
21+paging = True
22+language_support = True
23+
24+# search-url
25+search_url = 'https://news.search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}'
26+
27+# specific xpath variables
1328results_xpath = '//div[@class="res"]'
1429url_xpath = './/h3/a/@href'
1530title_xpath = './/h3/a'
@@ -17,30 +32,39 @@ content_xpath = './/div[@class="abstr"]'
1732publishedDate_xpath = './/span[@class="timestamp"]'
1833suggestion_xpath = '//div[@id="satat"]//a'
1934
20−paging = True
21−
2235
36+# do search-request
2337def request(query, params):
2438 offset = (params['pageno'] - 1) * 10 + 1
39+
2540 if params['language'] == 'all':
2641 language = 'en'
2742 else:
2843 language = params['language'].split('_')[0]
44+
2945 params['url'] = search_url.format(offset=offset,
30− query=urlencode({'p': query}))
46+ query=urlencode({'p': query}),
47+ lang=language)
48+
49+ # TODO required?
3150 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
3251 .format(lang=language)
3352 return params
3453
3554
55+# get response from search-request
3656def response(resp):
3757 results = []
58+
3859 dom = html.fromstring(resp.text)
3960
61+ # parse results
4062 for result in dom.xpath(results_xpath):
4163 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
4264 title = extract_text(result.xpath(title_xpath)[0])
4365 content = extract_text(result.xpath(content_xpath)[0])
66+
67+ # parse publishedDate
4468 publishedDate = extract_text(result.xpath(publishedDate_xpath)[0])
4569
4670 if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
@@ -58,15 +82,11 @@ def response(resp):
5882 if publishedDate.year == 1900:
5983 publishedDate = publishedDate.replace(year=datetime.now().year)
6084
85+ # append result
6186 results.append({'url': url,
6287 'title': title,
6388 'content': content,
6489 'publishedDate': publishedDate})
6590
66− if not suggestion_xpath:
67− return results
68−
69− for suggestion in dom.xpath(suggestion_xpath):
70− results.append({'suggestion': extract_text(suggestion)})
71−
91+ # return results
7292 return results
searx/settings.yml+6−5
@@ -20,6 +20,11 @@ engines:
2020 locale : en-US
2121 shortcut : bi
2222
23+ - name : bing images
24+ engine : bing_images
25+ locale : en-US
26+ shortcut : bii
27+
2328 - name : bing news
2429 engine : bing_news
2530 locale : en-US
@@ -148,11 +153,7 @@ engines:
148153
149154 - name : vimeo
150155 engine : vimeo
151− categories : videos
152− results_xpath : //div[@id="browse_content"]/ol/li
153− url_xpath : ./a/@href
154− title_xpath : ./a/div[@class="data"]/p[@class="title"]/text()
155− content_xpath : ./a/img/@src
156+ locale : en-US
156157 shortcut : vm
157158
158159locales: