Last active
May 19, 2022 13:31
-
-
Save mirekys/39cfa96bafe826f37aa1e0ea97f3f89e to your computer and use it in GitHub Desktop.
Resolves a DOI identifier and returns record metadata in JSON format
This file contains 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 | |
# pip install crossrefapi | |
from crossref.restful import Works | |
import requests | |
""" | |
Simple client for CrossRef DOI content negotiation | |
Tries to resolve a given DOI with CrossRef API. | |
If it fails (e.g. DOI is not minted by the CrossRef agency), | |
it tries to resolve it using http://dx.doi.org site. | |
http://www.crosscite.org/cn/ | |
""" | |
class CrossRefClient(object): | |
def __init__(self, accept='text/x-bibliography; style=apa', timeout=3): | |
""" | |
# Defaults to APA biblio style | |
# Usage: | |
s = CrossRefClient() | |
print s.doi2apa("10.1038/nature10414") | |
""" | |
self.headers = {'accept': accept} | |
self.timeout = timeout | |
def query(self, doi, q={}): | |
if doi.startswith("http://"): | |
url = doi | |
else: | |
url = "http://dx.doi.org/" + doi | |
r = requests.get(url, headers=self.headers) | |
print(r) | |
return r | |
def doi2apa(self, doi): | |
self.headers['accept'] = 'text/x-bibliography; style=apa' | |
return self.query(doi).text | |
def doi2turtle(self, doi): | |
self.headers['accept'] = 'text/turtle' | |
return self.query(doi).text | |
def doi2json(self, doi): | |
self.headers['accept'] = 'application/vnd.citationstyles.csl+json' | |
return self.query(doi).json() | |
def getMetafataFromDOI(id): | |
works = Works() | |
metadata = works.doi(id) | |
if metadata is None: | |
s = CrossRefClient() | |
metadata = s.doi2json(id) | |
return metadata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment