Browse Source

Create google_news.py

Thomas Pointhuber 11 years ago
parent
commit
0549f8c7c8
1 changed files with 37 additions and 0 deletions
  1. 37
    0
      searx/engines/google_news.py

+ 37
- 0
searx/engines/google_news.py View File

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