.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
marginalia.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""`Marginalia Search`_ is an independent open source Internet search engine
3operating out of Sweden. It is principally developed and operated by Viktor
4Lofgren .
5
6.. _Marginalia Search:
7 https://about.marginalia-search.com/
8
9Configuration
10=============
11
12The engine has the following required settings:
13
14- :py:obj:`api_key`
15
16You can configure a Marginalia engine by:
17
18.. code:: yaml
19
20 - name: marginalia
21 engine: marginalia
22 shortcut: mar
23 api_key: ...
24
25Implementations
26===============
27
28"""
29
30import typing as t
31from urllib.parse import urlencode, quote_plus
32from searx.utils import searxng_useragent
33from searx.result_types import EngineResults
34from searx.extended_types import SXNG_Response
35
36about = {
37 "website": "https://marginalia.nu",
38 "wikidata_id": None,
39 "official_api_documentation": "https://about.marginalia-search.com/article/api/",
40 "use_official_api": True,
41 "require_api_key": True,
42 "results": "JSON",
43}
44
45base_url = "https://api.marginalia.nu"
46safesearch = True
47categories = ["general"]
48paging = False
49results_per_page = 20
50api_key = None
51"""To get an API key, please follow the instructions from `Key and license`_
52
53.. _Key and license:
54 https://about.marginalia-search.com/article/api/
55
56"""
57
58
59class ApiSearchResult(t.TypedDict):
60 """Marginalia's ApiSearchResult_ class definition.
61
62 .. _ApiSearchResult:
63 https://github.com/MarginaliaSearch/MarginaliaSearch/blob/master/code/services-application/api-service/java/nu/marginalia/api/model/ApiSearchResult.java
64 """
65
66 url: str
67 title: str
68 description: str
69 quality: float
70 format: str
71 details: str
72
73
74class ApiSearchResults(t.TypedDict):
75 """Marginalia's ApiSearchResults_ class definition.
76
77 .. _ApiSearchResults:
78 https://github.com/MarginaliaSearch/MarginaliaSearch/blob/master/code/services-application/api-service/java/nu/marginalia/api/model/ApiSearchResults.java
79 """
80
81 license: str
82 query: str
83 results: list[ApiSearchResult]
84
85
86def request(query: str, params: dict[str, t.Any]):
87
88 query_params = {
89 "count": results_per_page,
90 "nsfw": min(params["safesearch"], 1),
91 }
92
93 params["url"] = f"{base_url}/{api_key}/search/{quote_plus(query)}?{urlencode(query_params)}"
94 params["headers"]["User-Agent"] = searxng_useragent()
95
96
97def response(resp: SXNG_Response):
98
99 res = EngineResults()
100 resp_json: ApiSearchResults = resp.json() # type: ignore
101
102 for item in resp_json.get("results", []):
103 res.add(
104 res.types.MainResult(
105 title=item["title"],
106 url=item["url"],
107 content=item.get("description", ""),
108 )
109 )
110
111 return res
112
113
114def init(engine_settings: dict[str, t.Any]):
115
116 _api_key = engine_settings.get("api_key")
117 if not _api_key:
118 logger.error("missing api_key: see https://about.marginalia-search.com/article/api")
119 return False
120
121 if _api_key == "public":
122 logger.error("invalid api_key (%s): see https://about.marginalia-search.com/article/api", api_key)
123
124 return True
init(dict[str, t.Any] engine_settings)
request(str query, dict[str, t.Any] params)
Definition marginalia.py:86
response(SXNG_Response resp)
Definition marginalia.py:97