Skip to content

Instantly share code, notes, and snippets.

@egh

egh/api-access-example.py

Last active Aug 29, 2015
Embed
What would you like to do?
python rich citations api access example
#!/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