doai_rewrite.py 916 B

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