Ver código fonte

[enh] oa_doi_rewrite plugin broadens doai_rewrite

jibe-b 7 anos atrás
pai
commit
575159b194

+ 2
- 2
searx/plugins/__init__.py Ver arquivo

@@ -22,7 +22,7 @@ if version_info[0] == 3:
22 22
 
23 23
 logger = logger.getChild('plugins')
24 24
 
25
-from searx.plugins import (doai_rewrite,
25
+from searx.plugins import (oa_doi_rewrite,
26 26
                            https_rewrite,
27 27
                            infinite_scroll,
28 28
                            open_results_on_new_tab,
@@ -78,7 +78,7 @@ class PluginStore():
78 78
 
79 79
 
80 80
 plugins = PluginStore()
81
-plugins.register(doai_rewrite)
81
+plugins.register(oa_doi_rewrite)
82 82
 plugins.register(https_rewrite)
83 83
 plugins.register(infinite_scroll)
84 84
 plugins.register(open_results_on_new_tab)

searx/plugins/doai_rewrite.py → searx/plugins/oa_doi_rewrite.py Ver arquivo

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

+ 4
- 1
searx/preferences.py Ver arquivo

@@ -15,6 +15,7 @@ LANGUAGE_CODES = [l[0] for l in languages]
15 15
 LANGUAGE_CODES.append('all')
16 16
 DISABLED = 0
17 17
 ENABLED = 1
18
+DOI_RESOLVERS = [r for r in settings['doi_resolvers'].keys()]
18 19
 
19 20
 
20 21
 class MissingArgumentException(Exception):
@@ -266,7 +267,9 @@ class Preferences(object):
266 267
                                    'results_on_new_tab': MapSetting(False, map={'0': False,
267 268
                                                                                 '1': True,
268 269
                                                                                 'False': False,
269
-                                                                                'True': True})}
270
+                                                                                'True': True}),
271
+                                   'doi_resolver': MultipleChoiceSetting(['oadoi.org'], choices=DOI_RESOLVERS),
272
+                                   }
270 273
 
271 274
         self.engines = EnginesSetting('engines', choices=engines)
272 275
         self.plugins = PluginsSetting('plugins', choices=plugins)

+ 7
- 0
searx/settings.yml Ver arquivo

@@ -712,3 +712,10 @@ locales:
712 712
     tr : Türkçe (Turkish)
713 713
     uk : українська мова (Ukrainian)
714 714
     zh : 中文 (Chinese)
715
+
716
+doi_resolvers :
717
+  oadoi.org : 'https://oadoi.org/'
718
+  doi.org : 'https://doi.org/'
719
+  doai.io  : 'http://doai.io/'
720
+
721
+default_doi_resolver : 'oadoi.org'

+ 17
- 0
searx/templates/oscar/preferences.html Ver arquivo

@@ -223,6 +223,23 @@
223 223
                             </div>
224 224
                         </div>
225 225
                         {% endfor %}
226
+                        <div class="panel panel-default">
227
+                            <div class="panel-heading">
228
+                                <h3 class="panel-title">{{ _('OA DOI rewrite') }}</h3>
229
+                            </div>
230
+                            <div class="panel-body">
231
+                                <div class="col-xs-6 col-sm-4 col-md-6">{{ _('Avoid paywalls by redirecting to open-access versions of publications when available') }}</div>
232
+                                <div class="col-xs-6 col-sm-4 col-md-6">
233
+                                    <select class="form-control" id='doi_resolver' name='doi_resolver'>
234
+                                        {% for doi_resolver_name,doi_resolver_url in doi_resolvers.items() %}
235
+                                        <option value="{{ doi_resolver_name }}" {% if doi_resolver_name == current_doi_resolver %}selected="selected"{% endif %}>
236
+                                                {{ doi_resolver_name }} - {{ doi_resolver_url }}
237
+                                        </option>
238
+                                         {% endfor %}
239
+                                     </select>
240
+                                </div>
241
+                            </div>
242
+                        </div>
226 243
                     </div>
227 244
                 </fieldset>
228 245
             </div>

+ 14
- 1
searx/webapp.py Ver arquivo

@@ -164,6 +164,14 @@ def get_locale():
164 164
     return locale
165 165
 
166 166
 
167
+def get_doi_resolver():
168
+    doi_resolvers = settings['doi_resolvers']
169
+    doi_resolver = request.args.get('doi_resolver', request.preferences.get_value('doi_resolver'))[0]
170
+    if doi_resolver not in doi_resolvers:
171
+        doi_resolvers = settings['default_doi_resolver']
172
+    return doi_resolver
173
+
174
+
167 175
 # code-highlighter
168 176
 @app.template_filter('code_highlighter')
169 177
 def code_highlighter(codelines, language=None):
@@ -695,6 +703,8 @@ def preferences():
695 703
                   shortcuts={y: x for x, y in engine_shortcuts.items()},
696 704
                   themes=themes,
697 705
                   plugins=plugins,
706
+                  doi_resolvers=settings['doi_resolvers'],
707
+                  current_doi_resolver=get_doi_resolver(),
698 708
                   allowed_plugins=allowed_plugins,
699 709
                   theme=get_current_theme_name(),
700 710
                   preferences_url_params=request.preferences.get_as_url_params(),
@@ -839,7 +849,10 @@ def config():
839 849
                     'autocomplete': settings['search']['autocomplete'],
840 850
                     'safe_search': settings['search']['safe_search'],
841 851
                     'default_theme': settings['ui']['default_theme'],
842
-                    'version': VERSION_STRING})
852
+                    'version': VERSION_STRING,
853
+                    'doi_resolvers': [r for r in search['doi_resolvers']],
854
+                    'default_doi_resolver': settings['default_doi_resolver'],
855
+                    })
843 856
 
844 857
 
845 858
 @app.errorhandler(404)