Spaces:
No application file
No application file
File size: 2,616 Bytes
4475814 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import requests
import re
import xml.etree.ElementTree as ET
# Documentation to follow: https://dailymed.nlm.nih.gov/dailymed/webservices-help/v2/spls_setid_api.cfm
# Set up the drug object
class Drug:
def __init__(self, name):
self.name = name
self.spl = self.request_spls(name_type='both', return_format='json')
self.spl_xml = self.request_spl_xml()
# Helper
def extract_text_in_parentheses(self, text):
pattern = r'\((.*?)\)' # Regular expression pattern to match text within parentheses
match = re.search(pattern, text)
if match:
return match.group(1)
return None
def request_spls(self, name_type='both', return_format='json'):
base_url = 'https://dailymed.nlm.nih.gov/dailymed/services/v2/spls'
endpoint = f'{base_url}.{return_format}'
params = {
'drug_name': self.extract_text_in_parentheses(self.name),
'name_type': name_type
}
response = requests.get(endpoint, params=params)
if response.status_code == 200:
return response.json() if return_format == 'json' else response.text
else:
print(f'Request failed with status code {response.status_code}.')
return None
def request_spl_xml(self):
if self.spl is None:
return None
set_id = self.spl['data'][0]['setid']
endpoint = f'https://dailymed.nlm.nih.gov/dailymed/services/v2/spls/{set_id}.xml'
response = requests.get(endpoint)
if response.status_code == 200:
return response.text
else:
print(f'Request failed with status code {response.status_code}.')
return None
def traverse_xml(self):
if self.spl_xml is None:
return None
root = ET.fromstring(self.request_spl_xml())
namespaces = {'ns': 'urn:hl7-org:v3'}
ingredients = root.findall('.//ns:ingredient', namespaces=namespaces)
ingredient_data = []
for ingredient in ingredients:
ingredient_data.append({
'name': ingredient.find('.//ns:quantity/ns:ingredientSubstance', namespaces=namespaces),
#'strength': ingredient.find('.//ns:strength', namespaces=namespaces).text,
#'code': ingredient.find('.//ns:code', namespaces=namespaces).attrib['code']
})
return ingredient_data
###### Testing the function outputs ######
# drug = Drug('drospirenone-ethinyl estradiol (YAZ) 3-0.02 MG per tablet')
# n = Drug.traverse_xml(drug)
# print(n) |