doai_rewrite.py 947 B

123456789101112131415161718192021222324252627282930313233
  1. from flask_babel import gettext
  2. import re
  3. from searx.url_utils 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. preference_section = 'privacy'
  9. def extract_doi(url):
  10. match = regex.search(url.path)
  11. if match:
  12. return match.group(0)
  13. for _, v in parse_qsl(url.query):
  14. match = regex.search(v)
  15. if match:
  16. return match.group(0)
  17. return None
  18. def on_result(request, search, result):
  19. doi = extract_doi(result['parsed_url'])
  20. if doi and len(doi) < 50:
  21. for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
  22. if doi.endswith(suffix):
  23. doi = doi[:-len(suffix)]
  24. result['url'] = 'http://doai.io/' + doi
  25. result['parsed_url'] = urlparse(result['url'])
  26. return True