.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
core.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""CORE_ (COnnecting REpositories) provides a comprehensive bibliographic
3database of the world’s scholarly literature, collecting and indexing
4research from repositories and journals.
5
6.. _CORE: https://core.ac.uk/about
7
8.. note::
9
10 The CORE engine requires an :py:obj:`API key <api_key>`.
11
12.. _core engine config:
13
14Configuration
15=============
16
17The engine has the following additional settings:
18
19- :py:obj:`api_key`
20
21.. code:: yaml
22
23 - name: core.ac.uk
24 api_key: "..."
25 inactive: false
26
27Implementations
28===============
29
30"""
31
32import typing as t
33
34from datetime import datetime
35from urllib.parse import urlencode
36
37from searx.result_types import EngineResults
38
39if t.TYPE_CHECKING:
40 from searx.extended_types import SXNG_Response
41 from searx.search.processors import OnlineParams
42
43
44about = {
45 "website": "https://core.ac.uk",
46 "wikidata_id": "Q22661180",
47 "official_api_documentation": "https://api.core.ac.uk/docs/v3",
48 "use_official_api": True,
49 "require_api_key": True,
50 "results": "JSON",
51}
52
53api_key = ""
54"""For an API key register at https://core.ac.uk/services/api and insert
55the API key in the engine :ref:`core engine config`."""
56
57categories = ["science", "scientific publications"]
58paging = True
59nb_per_page = 10
60base_url = "https://api.core.ac.uk/v3/search/works/"
61
62
63def setup(engine_settings: dict[str, t.Any]) -> bool:
64 """Initialization of the CORE_ engine, checks whether the :py:obj:`api_key`
65 is set, otherwise the engine is inactive.
66 """
67
68 key: str = engine_settings.get("api_key", "")
69 if key and key not in ("unset", "unknown", "..."):
70 return True
71 logger.error("CORE's API key is not set or invalid.")
72 return False
73
74
75def request(query: str, params: "OnlineParams") -> None:
76
77 # API v3 uses different parameters
78 search_params = {
79 "q": query,
80 "offset": (params["pageno"] - 1) * nb_per_page,
81 "limit": nb_per_page,
82 "sort": "relevance",
83 }
84
85 params["url"] = base_url + "?" + urlencode(search_params)
86 params["headers"] = {"Authorization": f"Bearer {api_key}"}
87
88
89def response(resp: "SXNG_Response") -> EngineResults:
90 # pylint: disable=too-many-branches
91 res = EngineResults()
92 json_data = resp.json()
93
94 for result in json_data.get("results", []):
95 # Get title
96 if not result.get("title"):
97 continue
98
99 # Get URL - try different options
100 url: str | None = None
101
102 # Try DOI first
103 doi: str = result.get("doi")
104 if doi:
105 url = f"https://doi.org/{doi}"
106
107 if url is None and result.get("doi"):
108 # use the DOI reference
109 url = "https://doi.org/" + str(result["doi"])
110 elif result.get("id"):
111 url = "https://core.ac.uk/works/" + str(result["id"])
112 elif result.get("downloadUrl"):
113 url = result["downloadUrl"]
114 elif result.get("sourceFulltextUrls"):
115 url = result["sourceFulltextUrls"]
116 else:
117 continue
118
119 # Published date
120 published_date = None
121
122 raw_date = result.get("publishedDate") or result.get("depositedDate")
123 if raw_date:
124 try:
125 published_date = datetime.fromisoformat(result["publishedDate"].replace("Z", "+00:00"))
126 except (ValueError, AttributeError):
127 pass
128
129 # Handle journals
130 journals = []
131 if result.get("journals"):
132 journals = [j.get("title") for j in result["journals"] if j.get("title")]
133
134 # Handle publisher
135 publisher = result.get("publisher", "").strip("'")
136
137 # Handle authors
138 authors: set[str] = set()
139 for i in result.get("authors", []):
140 name: str | None = i.get("name")
141 if name:
142 authors.add(name)
143
144 res.add(
145 res.types.Paper(
146 title=result.get("title"),
147 url=url,
148 content=result.get("fullText", "") or "",
149 tags=result.get("fieldOfStudy", []),
150 publishedDate=published_date,
151 type=result.get("documentType", "") or "",
152 authors=authors,
153 editor=", ".join(result.get("contributors", [])),
154 publisher=publisher,
155 journal=", ".join(journals),
156 doi=result.get("doi"),
157 pdf_url=result.get("downloadUrl", {}) or result.get("sourceFulltextUrls", {}),
158 )
159 )
160
161 return res
None request(str query, "OnlineParams" params)
Definition core.py:75
EngineResults response("SXNG_Response" resp)
Definition core.py:89
bool setup(dict[str, t.Any] engine_settings)
Definition core.py:63