summaryrefslogtreecommitdiff
path: root/searx/engines/bpb.py
blob: 5d6359aaeeb83847163664d92333ef131b4cf36a (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
68
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""BPB refers to ``Bundeszentrale für poltische Bildung``, which is a German
governmental institution aiming to reduce misinformation by providing resources
about politics and history.
"""

from datetime import datetime
from urllib.parse import urlencode

about = {
    'website': "https://www.bpb.de",
    'official_api_documentation': None,
    'use_official_api': False,
    'require_api_key': False,
    'results': 'JSON',
    'language': 'de',
}

paging = True
categories = ['general']


base_url = "https://www.bpb.de"


def request(query, params):
    args = {
        'query[term]': query,
        'page': params['pageno'] - 1,
        'sort[direction]': 'descending',
        'payload[nid]': 65350,
    }
    params['url'] = f"{base_url}/bpbapi/filter/search?{urlencode(args)}"
    return params


def response(resp):
    results = []

    json_resp = resp.json()

    for result in json_resp['teaser']:
        img_src = None
        if result['teaser']['image']:
            img_src = base_url + result['teaser']['image']['sources'][-1]['url']

        metadata = result['extension']['overline']
        authors = ', '.join(author['name'] for author in result['extension'].get('authors', []))
        if authors:
            metadata += f" | {authors}"

        publishedDate = None
        if result['extension'].get('publishingDate'):
            publishedDate = datetime.utcfromtimestamp(result['extension']['publishingDate'])

        results.append(
            {
                'url': base_url + result['teaser']['link']['url'],
                'title': result['teaser']['title'],
                'content': result['teaser']['text'],
                'img_src': img_src,
                'publishedDate': publishedDate,
                'metadata': metadata,
            }
        )

    return results