Last active
September 6, 2018 06:01
-
-
Save hadisfr/f1582d22d470e9cbb31676eec312e86f to your computer and use it in GitHub Desktop.
DOI to BibTeX https://scipython.com/blog/doi-to-bibtex/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import urllib.request | |
from urllib.error import HTTPError | |
BASE_URL = 'http://dx.doi.org/' | |
try: | |
doi = sys.argv[1] | |
except IndexError: | |
print('Usage:\n{} <doi>'.format(sys.argv[0])) | |
sys.exit(1) | |
url = BASE_URL + doi | |
req = urllib.request.Request(url) | |
req.add_header('Accept', 'application/x-bibtex') | |
try: | |
with urllib.request.urlopen(req) as f: | |
bibtex = f.read().decode() | |
print(bibtex) | |
except HTTPError as e: | |
if e.code == 404: | |
print('DOI not found.') | |
else: | |
print('Service unavailable.') | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment