.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
4import re
5from urllib.parse import urlparse, parse_qsl
6
7from flask_babel import gettext
8from searx import settings
9
10regex = re.compile(r'10\.\d{4,9}/[^\s]+')
11
12name = gettext('Open Access DOI rewrite')
13description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
14default_on = False
15preference_section = 'general'
16
17
18def extract_doi(url):
19 match = regex.search(url.path)
20 if match:
21 return match.group(0)
22 for _, v in parse_qsl(url.query):
23 match = regex.search(v)
24 if match:
25 return match.group(0)
26 return None
27
28
29def get_doi_resolver(preferences):
30 doi_resolvers = settings['doi_resolvers']
31 selected_resolver = preferences.get_value('doi_resolver')[0]
32 if selected_resolver not in doi_resolvers:
33 selected_resolver = settings['default_doi_resolver']
34 return doi_resolvers[selected_resolver]
35
36
37def on_result(request, _search, result):
38 if 'parsed_url' not in result:
39 return True
40
41 doi = extract_doi(result['parsed_url'])
42 if doi and len(doi) < 50:
43 for suffix in ('/', '.pdf', '.xml', '/full', '/meta', '/abstract'):
44 if doi.endswith(suffix):
45 doi = doi[: -len(suffix)]
46 result['url'] = get_doi_resolver(request.preferences) + doi
47 result['parsed_url'] = urlparse(result['url'])
48 if 'doi' not in result:
49 result['doi'] = doi
50 return True
on_result(request, _search, result)