|  | @@ -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
 |