mirror of
https://github.com/searxng/searxng.git
synced 2024-12-23 17:50:29 +00:00
542f7d0d7b
In the past, some files were tested with the standard profile, others with a profile in which most of the messages were switched off ... some files were not checked at all. - ``PYLINT_SEARXNG_DISABLE_OPTION`` has been abolished - the distinction ``# lint: pylint`` is no longer necessary - the pylint tasks have been reduced from three to two 1. ./searx/engines -> lint engines with additional builtins 2. ./searx ./searxng_extra ./tests -> lint all other python files Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
import re
|
|
from urllib.parse import urlparse, parse_qsl
|
|
|
|
from flask_babel import gettext
|
|
from searx import settings
|
|
|
|
regex = re.compile(r'10\.\d{4,9}/[^\s]+')
|
|
|
|
name = gettext('Open Access DOI rewrite')
|
|
description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
|
|
default_on = False
|
|
preference_section = 'general'
|
|
|
|
|
|
def extract_doi(url):
|
|
match = regex.search(url.path)
|
|
if match:
|
|
return match.group(0)
|
|
for _, v in parse_qsl(url.query):
|
|
match = regex.search(v)
|
|
if match:
|
|
return match.group(0)
|
|
return None
|
|
|
|
|
|
def get_doi_resolver(preferences):
|
|
doi_resolvers = settings['doi_resolvers']
|
|
selected_resolver = preferences.get_value('doi_resolver')[0]
|
|
if selected_resolver not in doi_resolvers:
|
|
selected_resolver = settings['default_doi_resolver']
|
|
return doi_resolvers[selected_resolver]
|
|
|
|
|
|
def on_result(request, _search, result):
|
|
if 'parsed_url' not in result:
|
|
return True
|
|
|
|
doi = extract_doi(result['parsed_url'])
|
|
if doi and len(doi) < 50:
|
|
for suffix in ('/', '.pdf', '.xml', '/full', '/meta', '/abstract'):
|
|
if doi.endswith(suffix):
|
|
doi = doi[: -len(suffix)]
|
|
result['url'] = get_doi_resolver(request.preferences) + doi
|
|
result['parsed_url'] = urlparse(result['url'])
|
|
if 'doi' not in result:
|
|
result['doi'] = doi
|
|
return True
|