summaryrefslogtreecommitdiff
path: root/searx/engines/crossref.py
blob: d61318146b84c9239275ed573e2ae6aaf67e4872 (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
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""Semantic Scholar (Science)
"""

from urllib.parse import urlencode
from searx.utils import html_to_text

about = {
    "website": 'https://www.crossref.org/',
    "wikidata_id": 'Q5188229',
    "official_api_documentation": 'https://github.com/CrossRef/rest-api-doc',
    "use_official_api": False,
    "require_api_key": False,
    "results": 'JSON',
}

categories = ['science', 'scientific publications']
paging = True
search_url = 'https://api.crossref.org/works'


def request(query, params):
    params['url'] = search_url + '?' + urlencode(dict(query=query, offset=20 * (params['pageno'] - 1)))
    return params


def response(resp):
    res = resp.json()
    results = []
    for record in res['message']['items']:
        record_type = record['type']
        if record_type == 'book-chapter':
            title = record['container-title'][0]
            if record['title'][0].lower().strip() != title.lower().strip():
                title = title + ' (' + record['title'][0] + ')'
            journal = None
        else:
            title = record['title'][0]
            journal = record.get('container-title', [None])[0]
        url = record.get('resource', {}).get('primary', {}).get('URL') or record['URL']
        authors = [author.get('given', '') + ' ' + author.get('family', '') for author in record.get('author', [])]
        isbn = record.get('isbn') or [i['value'] for i in record.get('isbn-type', [])]
        results.append(
            {
                'template': 'paper.html',
                'url': url,
                'title': title,
                'journal': journal,
                'volume': record.get('volume'),
                'type': record['type'],
                'content': html_to_text(record.get('abstract', '')),
                'publisher': record.get('publisher'),
                'authors': authors,
                'doi': record['DOI'],
                'isbn': isbn,
            }
        )
    return results