2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import urlparse, parse_qsl
|
2016-07-16 11:26:29 +02:00
|
|
|
from flask_babel import gettext
|
|
|
|
import re
|
2017-09-22 23:43:05 +02:00
|
|
|
from searx import settings
|
|
|
|
|
2016-07-16 11:26:29 +02:00
|
|
|
|
|
|
|
regex = re.compile(r'10\.\d{4,9}/[^\s]+')
|
|
|
|
|
2017-09-22 23:43:05 +02:00
|
|
|
name = gettext('Open Access DOI rewrite')
|
2016-07-16 11:26:29 +02:00
|
|
|
description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
|
|
|
|
default_on = False
|
2018-08-09 16:13:50 +02:00
|
|
|
preference_section = 'general'
|
2016-07-16 11:26:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-09-06 08:47:11 +02:00
|
|
|
def get_doi_resolver(preferences):
|
2017-09-22 23:43:05 +02:00
|
|
|
doi_resolvers = settings['doi_resolvers']
|
2021-09-06 08:47:11 +02:00
|
|
|
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]
|
2017-09-22 23:43:05 +02:00
|
|
|
|
|
|
|
|
2016-10-22 14:01:53 +02:00
|
|
|
def on_result(request, search, result):
|
2019-09-23 17:14:32 +02:00
|
|
|
if 'parsed_url' not in result:
|
|
|
|
return True
|
|
|
|
|
2016-10-22 14:01:53 +02:00
|
|
|
doi = extract_doi(result['parsed_url'])
|
2016-07-16 11:26:29 +02:00
|
|
|
if doi and len(doi) < 50:
|
2021-04-04 13:36:33 +02:00
|
|
|
for suffix in ('/', '.pdf', '.xml', '/full', '/meta', '/abstract'):
|
2016-07-16 11:26:29 +02:00
|
|
|
if doi.endswith(suffix):
|
2021-12-27 09:26:22 +01:00
|
|
|
doi = doi[: -len(suffix)]
|
2021-09-06 08:47:11 +02:00
|
|
|
result['url'] = get_doi_resolver(request.preferences) + doi
|
2016-11-14 17:25:21 +01:00
|
|
|
result['parsed_url'] = urlparse(result['url'])
|
2022-08-26 18:07:38 +02:00
|
|
|
if 'doi' not in result:
|
|
|
|
result['doi'] = doi
|
2016-07-16 11:26:29 +02:00
|
|
|
return True
|