oa_doi_rewrite.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from urllib.parse import urlparse, parse_qsl
  2. from flask_babel import gettext
  3. import re
  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 = 'general'
  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. doi_resolver_url = doi_resolvers[doi_resolver]
  26. return doi_resolver_url
  27. def on_result(request, search, result):
  28. if 'parsed_url' not in result:
  29. return True
  30. doi = extract_doi(result['parsed_url'])
  31. if doi and len(doi) < 50:
  32. for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
  33. if doi.endswith(suffix):
  34. doi = doi[:-len(suffix)]
  35. result['url'] = get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')) + doi
  36. result['parsed_url'] = urlparse(result['url'])
  37. return True