.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
oa_doi_rewrite.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4from __future__ import annotations
5import re
6from urllib.parse import urlparse, parse_qsl
7
8from flask_babel import gettext
9
10from searx import settings
11
12
13regex = re.compile(r'10\.\d{4,9}/[^\s]+')
14
15name = gettext('Open Access DOI rewrite')
16description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
17default_on = False
18preference_section = 'general/doi_resolver'
19
20
21def extract_doi(url):
22 match = regex.search(url.path)
23 if match:
24 return match.group(0)
25 for _, v in parse_qsl(url.query):
26 match = regex.search(v)
27 if match:
28 return match.group(0)
29 return None
30
31
32def get_doi_resolver(preferences):
33 doi_resolvers = settings['doi_resolvers']
34 selected_resolver = preferences.get_value('doi_resolver')[0]
35 if selected_resolver not in doi_resolvers:
36 selected_resolver = settings['default_doi_resolver']
37 return doi_resolvers[selected_resolver]
38
39
40def on_result(request, _search, result) -> bool:
41
42 if not result.parsed_url:
43 return True
44
45 doi = extract_doi(result['parsed_url'])
46 if doi and len(doi) < 50:
47 for suffix in ('/', '.pdf', '.xml', '/full', '/meta', '/abstract'):
48 if doi.endswith(suffix):
49 doi = doi[: -len(suffix)]
50 result['url'] = get_doi_resolver(request.preferences) + doi
51 result['parsed_url'] = urlparse(result['url'])
52 if 'doi' not in result:
53 result['doi'] = doi
54 return True
bool on_result(request, _search, result)