소스 검색

Merge pull request #43 from pointhi/news

Adding News-sites
Adam Tauber 11 년 전
부모
커밋
2d42208e83
4개의 변경된 파일152개의 추가작업 그리고 0개의 파일을 삭제
  1. 51
    0
      searx/engines/bing_news.py
  2. 37
    0
      searx/engines/google_news.py
  3. 51
    0
      searx/engines/yahoo_news.py
  4. 13
    0
      searx/settings.yml

+ 51
- 0
searx/engines/bing_news.py 파일 보기

@@ -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

+ 37
- 0
searx/engines/google_news.py 파일 보기

@@ -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

+ 51
- 0
searx/engines/yahoo_news.py 파일 보기

@@ -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

+ 13
- 0
searx/settings.yml 파일 보기

@@ -17,6 +17,11 @@ engines:
17 17
     locale : en-US
18 18
     shortcut : bi
19 19
 
20
+  - name : bing news
21
+    engine : bing_news
22
+    locale : en-US
23
+    shortcut : bin
24
+    
20 25
   - name : currency
21 26
     engine : currency_convert
22 27
     categories : general
@@ -61,6 +66,10 @@ engines:
61 66
     engine : google_images
62 67
     shortcut : goi
63 68
 
69
+  - name : google news
70
+    engine : google_news
71
+    shortcut : gon
72
+    
64 73
   - name : piratebay
65 74
     engine : piratebay
66 75
     categories : videos, music, files
@@ -112,6 +121,10 @@ engines:
112 121
     engine : yahoo
113 122
     shortcut : yh
114 123
 
124
+  - name : yahoo news
125
+    engine : yahoo_news
126
+    shortcut : yhn
127
+    
115 128
   - name : youtube
116 129
     engine : youtube
117 130
     categories : videos