3from __future__
import annotations
7from urllib.parse
import parse_qsl
9from flask_babel
import gettext
10from searx
import get_setting
16if typing.TYPE_CHECKING:
23ahmia_blacklist: list = []
26def filter_url_field(result:
"Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
27 """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
28 If URL should be modified, the returned string is the new URL to use."""
30 if field_name !=
"url":
34 if doi
and len(doi) < 50:
35 for suffix
in (
"/",
".pdf",
".xml",
"/full",
"/meta",
"/abstract"):
36 doi = doi.removesuffix(suffix)
38 if "doi" not in result:
40 log.debug(
"oa_doi_rewrite: [URL field: %s] %s -> %s", field_name, url_src, new_url)
47 """Avoid paywalls by redirecting to open-access."""
51 def __init__(self, plg_cfg:
"PluginCfg") ->
None:
55 name=gettext(
"Open Access DOI rewrite"),
56 description=gettext(
"Avoid paywalls by redirecting to open-access versions of publications when available"),
57 preference_section=
"general",
62 request:
"SXNG_Request",
63 search:
"SearchWithPlugins",
67 result.filter_urls(filter_url_field)
71regex = re.compile(
r'10\.\d{4,9}/[^\s]+')
75 m = regex.search(url.path)
78 for _, v
in parse_qsl(url.query):
87 selected_resolver = sxng_request.preferences.get_value(
'doi_resolver')[0]
88 if selected_resolver
not in doi_resolvers:
89 selected_resolver =
get_setting(
"default_doi_resolver")
90 return doi_resolvers[selected_resolver]
None __init__(self, "PluginCfg" plg_cfg)
bool on_result(self, "SXNG_Request" request, "SearchWithPlugins" search, "Result" result)
bool|str filter_url_field("Result|LegacyResult" result, str field_name, str url_src)
get_setting(name, default=_unset)