summaryrefslogtreecommitdiff
path: root/searx/engines/google_news.py
blob: 95d15cfb997e0b2f1d9d877d90b7df92639f30a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
 Google (News)

 @website     https://www.google.com
 @provide-api yes (https://developers.google.com/web-search/docs/),
              deprecated!

 @using-api   yes
 @results     JSON
 @stable      yes (but deprecated)
 @parse       url, title, content, publishedDate
"""

from urllib import urlencode
from json import loads
from dateutil import parser

# search-url
categories = ['news']
paging = True
language_support = True

# engine dependent config
url = 'https://ajax.googleapis.com/'
search_url = url + 'ajax/services/search/news?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={lang}'


# do search-request
def request(query, params):
    offset = (params['pageno'] - 1) * 8

    language = 'en-US'
    if params['language'] != 'all':
        language = params['language'].replace('_', '-')

    params['url'] = search_url.format(offset=offset,
                                      query=urlencode({'q': query}),
                                      lang=language)

    return params


# get response from search-request
def response(resp):
    results = []

    search_res = loads(resp.text)

    # return empty array if there are no results
    if not search_res.get('responseData', {}).get('results'):
        return []

    # parse results
    for result in search_res['responseData']['results']:
        # parse publishedDate
        publishedDate = parser.parse(result['publishedDate'])
        if 'url' not in result:
            continue

        # append result
        results.append({'url': result['unescapedUrl'],
                        'title': result['titleNoFormatting'],
                        'publishedDate': publishedDate,
                        'content': result['content']})

    # return results
    return results