summaryrefslogtreecommitdiff
path: root/searx/plugins/doai_rewrite.py
blob: fc5998b14b08ef73b9d795df307dd7f3e683527d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from flask_babel import gettext
import re
from urlparse import urlparse, parse_qsl

regex = re.compile(r'10\.\d{4,9}/[^\s]+')

name = gettext('DOAI rewrite')
description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
default_on = False


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


def on_result(request, ctx):
    doi = extract_doi(ctx['result']['parsed_url'])
    if doi and len(doi) < 50:
        for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
            if doi.endswith(suffix):
                doi = doi[:-len(suffix)]
        ctx['result']['url'] = 'http://doai.io/' + doi
        ctx['result']['parsed_url'] = urlparse(ctx['result']['url'])
    return True