summaryrefslogtreecommitdiff
path: root/searx/engines/scanr_structures.py
blob: 51c925247d424849102d492640254471e89f8e0c (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
69
70
71
72
73
74
75
76
77
78
79
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
 ScanR Structures (Science)
"""

from json import loads, dumps
from searx.utils import html_to_text

# about
about = {
    "website": 'https://scanr.enseignementsup-recherche.gouv.fr',
    "wikidata_id": 'Q44105684',
    "official_api_documentation": 'https://scanr.enseignementsup-recherche.gouv.fr/opendata',
    "use_official_api": True,
    "require_api_key": False,
    "results": 'JSON',
}

# engine dependent config
categories = ['science']
paging = True
page_size = 20

# search-url
url = 'https://scanr.enseignementsup-recherche.gouv.fr/'
search_url = url + 'api/structures/search'


# do search-request
def request(query, params):

    params['url'] = search_url
    params['method'] = 'POST'
    params['headers']['Content-type'] = "application/json"
    params['data'] = dumps({"query": query,
                            "searchField": "ALL",
                            "sortDirection": "ASC",
                            "sortOrder": "RELEVANCY",
                            "page": params['pageno'],
                            "pageSize": page_size})

    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 search_res.get('total', 0) < 1:
        return []

    # parse results
    for result in search_res['results']:
        if 'id' not in result:
            continue

        # is it thumbnail or img_src??
        thumbnail = None
        if 'logo' in result:
            thumbnail = result['logo']
            if thumbnail[0] == '/':
                thumbnail = url + thumbnail

        content = None
        if 'highlights' in result:
            content = result['highlights'][0]['value']

        # append result
        results.append({'url': url + 'structure/' + result['id'],
                        'title': result['label'],
                        # 'thumbnail': thumbnail,
                        'img_src': thumbnail,
                        'content': html_to_text(content)})

    # return results
    return results