Last active
August 29, 2015 14:06
-
-
Save egh/69718961c7e1627a1e0b to your computer and use it in GitHub Desktop.
python rich citations api access example
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 python | |
# Get the authors from a paper via api.richcitations.org | |
# call as api-access-example.py 10.1371/journal.pone.0106530 | |
import json | |
import requests | |
import sys | |
BASE_URL='http://api.richcitations.org/v0/' | |
PAPER_URL='%spaper'%(BASE_URL) | |
def format_author(author): | |
if author.has_key('literal'): | |
return author['literal'] | |
elif (author.has_key('given') and author.has_key('family')): | |
return "%s, %s"%(author['family'], author['given']) | |
elif author.has_key('family'): | |
return author.family | |
else: | |
return "[unknown]" | |
def main(raw_doi): | |
doi = "http://dx.doi.org/%s"%(raw_doi) | |
response = requests.get(PAPER_URL, params={'id': doi}) | |
if response.status_code == 202: | |
print "Rich citations still processing..." | |
elif response.status_code == 200: | |
parsed = json.loads(response.text) | |
authors = set([]) | |
for key, value in parsed['references'].iteritems(): | |
if value.has_key('bibliographic'): | |
if value['bibliographic'].has_key('author'): | |
for author in value['bibliographic']['author']: | |
authors.add(format_author(author)) | |
for author in sorted(authors): | |
print author | |
else: | |
print response | |
if __name__ == "__main__": | |
if (len(sys.argv) == 1): | |
print "Must supply a DOI!" | |
else: | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment