oa_doi_rewrite.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from flask_babel import gettext
  2. import re
  3. from searx.url_utils import urlparse, parse_qsl
  4. from searx import settings
  5. regex = re.compile(r'10\.\d{4,9}/[^\s]+')
  6. name = gettext('Open Access DOI rewrite')
  7. description = gettext('Avoid paywalls by redirecting to open-access versions of publications when available')
  8. default_on = False
  9. preference_section = 'privacy'
  10. doi_resolvers = settings['doi_resolvers']
  11. def extract_doi(url):
  12. match = regex.search(url.path)
  13. if match:
  14. return match.group(0)
  15. for _, v in parse_qsl(url.query):
  16. match = regex.search(v)
  17. if match:
  18. return match.group(0)
  19. return None
  20. def get_doi_resolver(args, preference_doi_resolver):
  21. doi_resolvers = settings['doi_resolvers']
  22. doi_resolver = args.get('doi_resolver', preference_doi_resolver)[0]
  23. if doi_resolver not in doi_resolvers:
  24. doi_resolvers = settings['default_doi_resolver']
  25. return doi_resolver
  26. def on_result(request, search, result):
  27. doi = extract_doi(result['parsed_url'])
  28. if doi and len(doi) < 50:
  29. for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
  30. if doi.endswith(suffix):
  31. doi = doi[:-len(suffix)]
  32. result['url'] = get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')) + doi
  33. result['parsed_url'] = urlparse(result['url'])
  34. return True