Skip to content

Instantly share code, notes, and snippets.

@eteq
Last active August 29, 2015 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eteq/4a63b5f8924dbd764d15 to your computer and use it in GitHub Desktop.
Save eteq/4a63b5f8924dbd764d15 to your computer and use it in GitHub Desktop.
A script to grab ADS private libraries and send them to ORCID
from __future__ import division, print_function
"""
This script takes in the URL to the JSON version of an ADS private library, and outputs a bibtex file suitable for importing into ORCiD
"""
import json
import urllib2
def extract_bibcodes(urloflibrary):
u = urllib2.urlopen(urloflibrary)
try:
jsitems = json.loads(u.read())['items']
finally:
u.close()
adsnames = []
for i in jsitems:
if i['itemtype'] == 'ads/itemtype:pub':
adsnames.append(i['basic']['name'])
else:
print('The item type is "{0}" not pub, so I don\'t know what to do '
'with it'.format(i['itemtype']))
return adsnames
BIBCODE_TEMPLATE = 'http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode={bibcode}&data_type=BIBTEX&db_key=AST&nocookieset=1'
def bibcodes_to_bibtex(adsnames):
bibtexs = []
for name in adsnames:
url = BIBCODE_TEMPLATE.format(bibcode=name)
u = urllib2.urlopen(url)
try:
response = u.read()
finally:
u.close()
bibtexs.append('@' + '@'.join(response.split('@')[1:]))
return _hack_to_make_orcid_parse_the_bibtex_damnit(bibtexs)
def _hack_to_make_orcid_parse_the_bibtex_damnit(bibtexs):
newbibtexs = []
for b in bibtexs:
bi = b.replace('month = jan', 'month = Jan')
lines = []
for l in bi.split('\n'):
if l.lstrip().startswith('@ARTICLE'):
lines.append(l.replace('&', ''))
else:
lines.append(l)
newbibtexs.append('\n'.join(lines))
return newbibtexs
if __name__ == '__main__':
import argparse
p = argparse.ArgumentParser()
p.add_argument('jsonlibraryurl')
p.add_argument('-o', '--outfile', default=None)
ops = p.parse_args()
adsnames = extract_bibcodes(ops.jsonlibraryurl)
bibtexs = bibcodes_to_bibtex(adsnames)
if ops.outfile is None:
for l in bibtexs:
print(l)
else:
with open(ops.outfile, 'w') as f:
for l in bibtexs:
f.write(l)
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment