Skip to content

Instantly share code, notes, and snippets.

@hadisfr
Last active September 6, 2018 06:01
Show Gist options
  • Save hadisfr/f1582d22d470e9cbb31676eec312e86f to your computer and use it in GitHub Desktop.
Save hadisfr/f1582d22d470e9cbb31676eec312e86f to your computer and use it in GitHub Desktop.
#!/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