Browse Source

Merge pull request #630 from davidar/doi

Add Crossref search engine and DOAI rewrite plugin
Adam Tauber 8 years ago
parent
commit
96d623436c
4 changed files with 59 additions and 2 deletions
  1. 15
    1
      searx/engines/json_engine.py
  2. 3
    1
      searx/plugins/__init__.py
  3. 31
    0
      searx/plugins/doai_rewrite.py
  4. 10
    0
      searx/settings.yml

+ 15
- 1
searx/engines/json_engine.py View File

@@ -8,6 +8,14 @@ content_query = None
8 8
 title_query = None
9 9
 # suggestion_xpath = ''
10 10
 
11
+# parameters for engines with paging support
12
+#
13
+# number of results on each page
14
+# (only needed if the site requires not a page number, but an offset)
15
+page_size = 1
16
+# number of the first page (usually 0 or 1)
17
+first_page_num = 1
18
+
11 19
 
12 20
 def iterate(iterable):
13 21
     if type(iterable) == dict:
@@ -69,8 +77,14 @@ def query(data, query_string):
69 77
 
70 78
 def request(query, params):
71 79
     query = urlencode({'q': query})[2:]
72
-    params['url'] = search_url.format(query=query)
80
+
81
+    fp = {'query': query}
82
+    if paging and search_url.find('{pageno}') >= 0:
83
+        fp['pageno'] = (params['pageno'] + first_page_num - 1) * page_size
84
+
85
+    params['url'] = search_url.format(**fp)
73 86
     params['query'] = query
87
+
74 88
     return params
75 89
 
76 90
 

+ 3
- 1
searx/plugins/__init__.py View File

@@ -19,7 +19,8 @@ from searx import logger
19 19
 
20 20
 logger = logger.getChild('plugins')
21 21
 
22
-from searx.plugins import (https_rewrite,
22
+from searx.plugins import (doai_rewrite,
23
+                           https_rewrite,
23 24
                            open_results_on_new_tab,
24 25
                            self_info,
25 26
                            search_on_category_select,
@@ -73,6 +74,7 @@ class PluginStore():
73 74
 
74 75
 
75 76
 plugins = PluginStore()
77
+plugins.register(doai_rewrite)
76 78
 plugins.register(https_rewrite)
77 79
 plugins.register(open_results_on_new_tab)
78 80
 plugins.register(self_info)

+ 31
- 0
searx/plugins/doai_rewrite.py View File

@@ -0,0 +1,31 @@
1
+from flask_babel import gettext
2
+import re
3
+from urlparse import urlparse, parse_qsl
4
+
5
+regex = re.compile(r'10\.\d{4,9}/[^\s]+')
6
+
7
+name = gettext('DOAI rewrite')
8
+description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
9
+default_on = False
10
+
11
+
12
+def extract_doi(url):
13
+    match = regex.search(url.path)
14
+    if match:
15
+        return match.group(0)
16
+    for _, v in parse_qsl(url.query):
17
+        match = regex.search(v)
18
+        if match:
19
+            return match.group(0)
20
+    return None
21
+
22
+
23
+def on_result(request, ctx):
24
+    doi = extract_doi(ctx['result']['parsed_url'])
25
+    if doi and len(doi) < 50:
26
+        for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
27
+            if doi.endswith(suffix):
28
+                doi = doi[:-len(suffix)]
29
+        ctx['result']['url'] = 'http://doai.io/' + doi
30
+        ctx['result']['parsed_url'] = urlparse(ctx['result']['url'])
31
+    return True

+ 10
- 0
searx/settings.yml View File

@@ -87,6 +87,16 @@ engines:
87 87
   - name : btdigg
88 88
     engine : btdigg
89 89
     shortcut : bt
90
+    
91
+  - name : crossref
92
+    engine : json_engine
93
+    paging : True
94
+    search_url : http://search.crossref.org/dois?q={query}&page={pageno}
95
+    url_query : doi
96
+    title_query : title
97
+    content_query : fullCitation
98
+    categories : science
99
+    shortcut : cr
90 100
 
91 101
   - name : currency
92 102
     engine : currency_convert