Skip to content

Instantly share code, notes, and snippets.

@caodac
Created December 28, 2020 20:35
Show Gist options
  • Save caodac/b6d48420af8f1435fda8b05e5732242d to your computer and use it in GitHub Desktop.
Save caodac/b6d48420af8f1435fda8b05e5732242d to your computer and use it in GitHub Desktop.
Get list of U.S. approved drugs from https://drugs.ncats.io
import requests
def get_additional_data(id):
r = requests.get('https://drugs.ncats.io/api/v1/substances(%s)/@additional' % id)
if 200 == r.status_code:
return r.json()
return None
# experiment with the filters at https://drugs.ncats.io
params = {
'facet': ['Substance Form/Principal Form',
'Development Status/US Approved Rx',
'Development Status/US Approved OTC'],
'skip': 0,
'top': 100
}
print('UNII\tName\tYear\tConditions\tStatus\tSource')
total = 0
while True:
r = requests.get('https://drugs.ncats.io/api/v1/substances/search', params)
if 200 != r.status_code:
print('%s returns status code %d!' % (r.url, r.status_code))
break
json = r.json()
count = json['count']
total = total + count
#print('fetched %d/%d' % (total, json['total']))
for r in json['content']:
#print('...%s' % r['uuid'])
data = get_additional_data(r['uuid'])
if data != None:
status = None
year = None
conds = []
for d in data:
if (d['name'] == 'Highest Development Event'
or (d['name'] == 'Earliest Approved Event' and status == None)):
status = d['value']
elif d['name'] == 'Approval Year':
year = d['value']
elif d['name'] == 'Conditions':
conds.append(d['value']['label'])
if status != None and not status['withdrawn']:
print('%s\t%s\t%s\t%s\t%s\t%s' % (
r['approvalID'], r['_name'], year, ';'.join(conds), status['status'], status['sourceURL']))
if count < json['top']:
# we're done
break
params['skip'] = total
#print('%d records processed!' % total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment