瀏覽代碼

[enh] oa_doi_rewrite plugin broadens doai_rewrite

jibe-b 7 年之前
父節點
當前提交
575159b194
共有 6 個檔案被更改,包括 59 行新增6 行删除
  1. 2
    2
      searx/plugins/__init__.py
  2. 15
    2
      searx/plugins/oa_doi_rewrite.py
  3. 4
    1
      searx/preferences.py
  4. 7
    0
      searx/settings.yml
  5. 17
    0
      searx/templates/oscar/preferences.html
  6. 14
    1
      searx/webapp.py

+ 2
- 2
searx/plugins/__init__.py 查看文件

22
 
22
 
23
 logger = logger.getChild('plugins')
23
 logger = logger.getChild('plugins')
24
 
24
 
25
-from searx.plugins import (doai_rewrite,
25
+from searx.plugins import (oa_doi_rewrite,
26
                            https_rewrite,
26
                            https_rewrite,
27
                            infinite_scroll,
27
                            infinite_scroll,
28
                            open_results_on_new_tab,
28
                            open_results_on_new_tab,
78
 
78
 
79
 
79
 
80
 plugins = PluginStore()
80
 plugins = PluginStore()
81
-plugins.register(doai_rewrite)
81
+plugins.register(oa_doi_rewrite)
82
 plugins.register(https_rewrite)
82
 plugins.register(https_rewrite)
83
 plugins.register(infinite_scroll)
83
 plugins.register(infinite_scroll)
84
 plugins.register(open_results_on_new_tab)
84
 plugins.register(open_results_on_new_tab)

searx/plugins/doai_rewrite.py → searx/plugins/oa_doi_rewrite.py 查看文件

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

+ 4
- 1
searx/preferences.py 查看文件

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

+ 7
- 0
searx/settings.yml 查看文件

712
     tr : Türkçe (Turkish)
712
     tr : Türkçe (Turkish)
713
     uk : українська мова (Ukrainian)
713
     uk : українська мова (Ukrainian)
714
     zh : 中文 (Chinese)
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 查看文件

223
                             </div>
223
                             </div>
224
                         </div>
224
                         </div>
225
                         {% endfor %}
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
                     </div>
243
                     </div>
227
                 </fieldset>
244
                 </fieldset>
228
             </div>
245
             </div>

+ 14
- 1
searx/webapp.py 查看文件

164
     return locale
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
 # code-highlighter
175
 # code-highlighter
168
 @app.template_filter('code_highlighter')
176
 @app.template_filter('code_highlighter')
169
 def code_highlighter(codelines, language=None):
177
 def code_highlighter(codelines, language=None):
695
                   shortcuts={y: x for x, y in engine_shortcuts.items()},
703
                   shortcuts={y: x for x, y in engine_shortcuts.items()},
696
                   themes=themes,
704
                   themes=themes,
697
                   plugins=plugins,
705
                   plugins=plugins,
706
+                  doi_resolvers=settings['doi_resolvers'],
707
+                  current_doi_resolver=get_doi_resolver(),
698
                   allowed_plugins=allowed_plugins,
708
                   allowed_plugins=allowed_plugins,
699
                   theme=get_current_theme_name(),
709
                   theme=get_current_theme_name(),
700
                   preferences_url_params=request.preferences.get_as_url_params(),
710
                   preferences_url_params=request.preferences.get_as_url_params(),
839
                     'autocomplete': settings['search']['autocomplete'],
849
                     'autocomplete': settings['search']['autocomplete'],
840
                     'safe_search': settings['search']['safe_search'],
850
                     'safe_search': settings['search']['safe_search'],
841
                     'default_theme': settings['ui']['default_theme'],
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
 @app.errorhandler(404)
858
 @app.errorhandler(404)