doi2bib 742 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env nix-shell
  2. #! nix-shell -i python3 -p python3 python3Packages.urllib3
  3. # From https://scipython.com/blog/doi-to-bibtex/
  4. import sys
  5. import urllib.request
  6. from urllib.error import HTTPError
  7. BASE_URL = 'http://dx.doi.org/'
  8. try:
  9. doi = sys.argv[1]
  10. except IndexError:
  11. print('Usage:\n{} <doi>'.format(sys.argv[0]))
  12. sys.exit(1)
  13. url = BASE_URL + doi
  14. req = urllib.request.Request(url)
  15. req.add_header('Accept', 'application/x-bibtex')
  16. try:
  17. with urllib.request.urlopen(req) as f:
  18. bibtex = f.read().decode()
  19. print(bibtex)
  20. except HTTPError as e:
  21. if e.code == 404:
  22. print('DOI not found.')
  23. else:
  24. print('Service unavailable.')
  25. sys.exit(1)
  26. ### Local Variables:
  27. ### mode: python
  28. ### End: