diff --git a/searx/plugins/__init__.py b/searx/plugins/__init__.py index 46c1f8918..4dbcbbd28 100644 --- a/searx/plugins/__init__.py +++ b/searx/plugins/__init__.py @@ -22,7 +22,7 @@ if version_info[0] == 3: logger = logger.getChild('plugins') -from searx.plugins import (doai_rewrite, +from searx.plugins import (oa_doi_rewrite, https_rewrite, infinite_scroll, open_results_on_new_tab, @@ -78,7 +78,7 @@ class PluginStore(): plugins = PluginStore() -plugins.register(doai_rewrite) +plugins.register(oa_doi_rewrite) plugins.register(https_rewrite) plugins.register(infinite_scroll) plugins.register(open_results_on_new_tab) diff --git a/searx/plugins/doai_rewrite.py b/searx/plugins/oa_doi_rewrite.py similarity index 63% rename from searx/plugins/doai_rewrite.py rename to searx/plugins/oa_doi_rewrite.py index b7b814fac..e952c86f5 100644 --- a/searx/plugins/doai_rewrite.py +++ b/searx/plugins/oa_doi_rewrite.py @@ -1,14 +1,19 @@ from flask_babel import gettext import re from searx.url_utils import urlparse, parse_qsl +from flask import request +from searx import settings + regex = re.compile(r'10\.\d{4,9}/[^\s]+') -name = gettext('DOAI rewrite') +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 = 'privacy' +doi_resolvers = settings['doi_resolvers'] + def extract_doi(url): match = regex.search(url.path) @@ -21,12 +26,20 @@ def extract_doi(url): return None +def get_doi_resolver(): + doi_resolvers = settings['doi_resolvers'] + doi_resolver = request.args.get('doi_resolver', request.preferences.get_value('doi_resolver'))[0] + if doi_resolver not in doi_resolvers: + doi_resolvers = settings['default_doi_resolver'] + return doi_resolvers[doi_resolver] + + def on_result(request, search, result): doi = extract_doi(result['parsed_url']) if doi and len(doi) < 50: for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'): if doi.endswith(suffix): doi = doi[:-len(suffix)] - result['url'] = 'http://doai.io/' + doi + result['url'] = get_doi_resolver() + doi result['parsed_url'] = urlparse(result['url']) return True diff --git a/searx/preferences.py b/searx/preferences.py index c2c649eea..00a3fd683 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -15,6 +15,7 @@ LANGUAGE_CODES = [l[0] for l in languages] LANGUAGE_CODES.append('all') DISABLED = 0 ENABLED = 1 +DOI_RESOLVERS = [r for r in settings['doi_resolvers'].keys()] class MissingArgumentException(Exception): @@ -266,7 +267,9 @@ class Preferences(object): 'results_on_new_tab': MapSetting(False, map={'0': False, '1': True, 'False': False, - 'True': True})} + 'True': True}), + 'doi_resolver': MultipleChoiceSetting(['oadoi.org'], choices=DOI_RESOLVERS), + } self.engines = EnginesSetting('engines', choices=engines) self.plugins = PluginsSetting('plugins', choices=plugins) diff --git a/searx/settings.yml b/searx/settings.yml index 18b6a5c59..ec3e6b469 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -712,3 +712,10 @@ locales: tr : Türkçe (Turkish) uk : українська мова (Ukrainian) zh : 中文 (Chinese) + +doi_resolvers : + oadoi.org : 'https://oadoi.org/' + doi.org : 'https://doi.org/' + doai.io : 'http://doai.io/' + +default_doi_resolver : 'oadoi.org' diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index f4b2c63ea..dcca3cba7 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -223,6 +223,23 @@ {% endfor %} +
+
+

{{ _('OA DOI rewrite') }}

+
+
+
{{ _('Avoid paywalls by redirecting to open-access versions of publications when available') }}
+
+ +
+
+
diff --git a/searx/webapp.py b/searx/webapp.py index dd93395ee..9542b7814 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -164,6 +164,14 @@ def get_locale(): return locale +def get_doi_resolver(): + doi_resolvers = settings['doi_resolvers'] + doi_resolver = request.args.get('doi_resolver', request.preferences.get_value('doi_resolver'))[0] + if doi_resolver not in doi_resolvers: + doi_resolvers = settings['default_doi_resolver'] + return doi_resolver + + # code-highlighter @app.template_filter('code_highlighter') def code_highlighter(codelines, language=None): @@ -695,6 +703,8 @@ def preferences(): shortcuts={y: x for x, y in engine_shortcuts.items()}, themes=themes, plugins=plugins, + doi_resolvers=settings['doi_resolvers'], + current_doi_resolver=get_doi_resolver(), allowed_plugins=allowed_plugins, theme=get_current_theme_name(), preferences_url_params=request.preferences.get_as_url_params(), @@ -839,7 +849,10 @@ def config(): 'autocomplete': settings['search']['autocomplete'], 'safe_search': settings['search']['safe_search'], 'default_theme': settings['ui']['default_theme'], - 'version': VERSION_STRING}) + 'version': VERSION_STRING, + 'doi_resolvers': [r for r in search['doi_resolvers']], + 'default_doi_resolver': settings['default_doi_resolver'], + }) @app.errorhandler(404)