.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"""The Astrophysics Data System (ADS_) is a digital library portal for
3researchers in astronomy and physics, operated by the Smithsonian Astrophysical
4Observatory (SAO) under a NASA grant. The ADS_ is a solr instance, but not with
5the standard API paths.
6
7.. note::
8
9 The ADS_ engine requires an :py:obj:`API key <api_key>`.
10
11This engine uses the `search/query`_ API endpoint. Since the user's search term
12is passed through, the `search syntax`_ of ADS can be used (at least to some
13extent).
14
15.. _ADS: https://ui.adsabs.harvard.edu
16.. _search/query: https://ui.adsabs.harvard.edu/help/api/api-docs.html#get-/search/query
17.. _search syntax: https://ui.adsabs.harvard.edu/help/search/search-syntax
18
19
20Configuration
21=============
22
23The engine has the following additional settings:
24
25- :py:obj:`api_key`
26- :py:obj:`ads_sort`
27
28.. code:: yaml
29
30 - name: astrophysics data system
31 api_key: "..."
32 inactive: false
33
34
35Implementations
36===============
37"""
38
39import typing as t
40
41from datetime import datetime
42from urllib.parse import urlencode
43
44from searx.utils import html_to_text
45from searx.exceptions import SearxEngineAPIException
46from searx.result_types import EngineResults
47
48if t.TYPE_CHECKING:
49 from searx.extended_types import SXNG_Response
50 from searx.search.processors import OnlineParams
51
52about = {
53 "website": "https://ui.adsabs.harvard.edu/",
54 "wikidata_id": "Q752099",
55 "official_api_documentation": "https://ui.adsabs.harvard.edu/help/api/api-docs.html",
56 "use_official_api": True,
57 "require_api_key": True,
58 "results": "JSON",
59}
60
61categories = ["science", "scientific publications"]
62paging = True
63base_url = "https://api.adsabs.harvard.edu/v1/search/query"
64
65api_key = "unset"
66"""Get an API token as described in https://ui.adsabs.harvard.edu/help/api"""
67
68ads_field_list = [
69 "abstract",
70 "author",
71 "bibcode",
72 "comment",
73 "date",
74 "doi",
75 "isbn",
76 "issn",
77 "keyword",
78 "page",
79 "page_count",
80 "page_range",
81 "pub",
82 "pubdate",
83 "pubnote",
84 "read_count",
85 "title",
86 "volume",
87 "year",
88]
89"""Set of fields to return in the response from ADS."""
90
91ads_rows = 10
92"""How many records to return for the ADS request."""
93
94ads_sort = "read_count desc"
95"""The format is 'field' + 'direction' where direction is one of 'asc' or 'desc'
96and field is any of the valid indexes."""
97
98
99def setup(engine_settings: dict[str, t.Any]) -> bool:
100 """Initialization of the ADS_ engine, checks whether the :py:obj:`api_key`
101 is set, otherwise the engine is inactive.
102 """
103 key: str = engine_settings.get("api_key", "")
104 if key and key not in ("unset", "unknown", "..."):
105 return True
106 logger.error("Astrophysics Data System (ADS) API key is not set or invalid.")
107 return False
108
109
110def request(query: str, params: "OnlineParams") -> None:
111
112 args: dict[str, str | int] = {
113 "q": query,
114 "fl": ",".join(ads_field_list),
115 "rows": ads_rows,
116 "start": ads_rows * (params["pageno"] - 1),
117 }
118 if ads_sort:
119 args["sort"] = ads_sort
120
121 params["headers"]["Authorization"] = f"Bearer {api_key}"
122 params["url"] = f"{base_url}?{urlencode(args)}"
123
124
125def response(resp: "SXNG_Response") -> EngineResults:
126
127 res = EngineResults()
128 json_data: dict[str, dict[str, t.Any]] = resp.json()
129
130 if "error" in json_data:
131 raise SearxEngineAPIException(json_data["error"]["msg"])
132
133 def _str(k: str) -> str:
134 return str(doc.get(k, ""))
135
136 def _list(k: str) -> list[str]:
137 return doc.get(k, [])
138
139 for doc in json_data["response"]["docs"]:
140 authors: list[str] = doc["author"]
141 if len(authors) > 15:
142 # There are articles with hundreds of authors
143 authors = authors[:15] + ["et al."]
144
145 paper = res.types.Paper(
146 url=f"https://ui.adsabs.harvard.edu/abs/{doc.get('bibcode')}/",
147 title=html_to_text(_list("title")[0]),
148 authors=authors,
149 content=html_to_text(_str("abstract")),
150 doi=_list("doi")[0],
151 issn=_list("issn"),
152 isbn=_list("isbn"),
153 tags=_list("keyword"),
154 pages=",".join(_list("page")),
155 publisher=_str("pub") + " " + _str("year"),
156 publishedDate=datetime.fromisoformat(_str("date")),
157 volume=_str("volume"),
158 views=_str("read_count"),
159 comments=" / ".join(_list("pubnote")),
160 )
161 res.add(paper)
162
163 return res
EngineResults response("SXNG_Response" resp)
None request(str query, "OnlineParams" params)
bool setup(dict[str, t.Any] engine_settings)