.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
astrophysics_data_system.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2""".. sidebar:: info
3
4The Astrophysics Data System (ADS) is a digital library portal for researchers in astronomy and physics,
5operated by the Smithsonian Astrophysical Observatory (SAO) under a NASA grant.
6The engine is adapted from the solr engine.
7
8"""
9
10# pylint: disable=global-statement
11
12from datetime import datetime
13from json import loads
14from urllib.parse import urlencode
15from searx.exceptions import SearxEngineAPIException
16
17about = {
18 "website": 'https://ui.adsabs.harvard.edu/',
19 "wikidata_id": 'Q752099',
20 "official_api_documentation": 'https://ui.adsabs.harvard.edu/help/api/api-docs.html',
21 "use_official_api": True,
22 "require_api_key": True,
23 "results": 'JSON',
24}
25
26base_url = 'https://api.adsabs.harvard.edu/v1/search'
27result_base_url = 'https://ui.adsabs.harvard.edu/abs/'
28rows = 10
29sort = '' # sorting: asc or desc
30field_list = ['bibcode', 'author', 'title', 'abstract', 'doi', 'date'] # list of field names to display on the UI
31default_fields = '' # default field to query
32query_fields = '' # query fields
33paging = True
34api_key = 'unset'
35
36
37def init(_):
38 if api_key == 'unset':
39 raise SearxEngineAPIException('missing ADS API key')
40
41
42def request(query, params):
43 query_params = {'q': query, 'rows': rows}
44 if field_list:
45 query_params['fl'] = ','.join(field_list)
46 if query_fields:
47 query_params['qf'] = ','.join(query_fields)
48 if default_fields:
49 query_params['df'] = default_fields
50 if sort:
51 query_params['sort'] = sort
52
53 query_params['start'] = rows * (params['pageno'] - 1)
54
55 params['headers']['Authorization'] = f'Bearer {api_key}'
56 params['url'] = f"{base_url}/query?{urlencode(query_params)}"
57
58 return params
59
60
61def response(resp):
62 try:
63 resp_json = loads(resp.text)
64 except Exception as e:
65 raise SearxEngineAPIException("failed to parse response") from e
66
67 if 'error' in resp_json:
68 raise SearxEngineAPIException(resp_json['error']['msg'])
69
70 resp_json = resp_json["response"]
71 result_len = resp_json["numFound"]
72 results = []
73
74 for res in resp_json["docs"]:
75 author = res.get("author")
76
77 if author:
78 author = author[0] + ' et al.'
79
80 results.append(
81 {
82 'url': result_base_url + res.get("bibcode") + "/",
83 'title': res.get("title")[0],
84 'author': author,
85 'content': res.get("abstract"),
86 'doi': res.get("doi"),
87 'publishedDate': datetime.fromisoformat(res.get("date")),
88 }
89 )
90
91 results.append({'number_of_results': result_len})
92
93 return results