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