-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvest_atc_ddd_substances.py
executable file
·146 lines (127 loc) · 5.08 KB
/
harvest_atc_ddd_substances.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Drugs-Harvesters -- Harvesters to collect data on medications
# By: Emmanuel Raviart <emmanuel@raviart.com>
#
# Copyright (C) 2014 Etalab
# https://github.com/AlexisEidelman/medicam
#
# This file is part of Drugs-Harvesters.
#
# Drugs-Harvesters is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Drugs-Harvesters is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Download ATC/DDD substances pages from http://www.whocc.no/"""
import argparse
import logging
import os
import re
import shutil
import sys
import urllib
import urllib2
import urlparse
import lxml.html
app_name = os.path.splitext(os.path.basename(__file__))[0]
log = logging.getLogger(app_name)
substance_url_re = re.compile(r'\./\?code=(?P<code>.+?)(&|$)')
def main():
parser = argparse.ArgumentParser(description = __doc__)
parser.add_argument('-d', '--download-dir', default = 'atc-ddd',
help = 'directory where to store downloaded pages')
parser.add_argument('-e', '--erase', action = 'store_true', help = 'erase pages already downloaded')
parser.add_argument('-v', '--verbose', action = 'store_true', help = 'increase output verbosity')
args = parser.parse_args()
logging.basicConfig(level = logging.INFO if args.verbose else logging.WARNING, stream = sys.stdout)
if not os.path.exists(args.download_dir):
os.makedirs(args.download_dir)
codes = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
form_data = dict(
code = None,
showdescription = 'yes',
)
substance_by_code = {}
while codes:
code = codes.pop()
form_data['code'] = code
page_path = os.path.join(args.download_dir, '{}.html'.format(code))
if not args.erase and os.path.exists(page_path):
log.info(u'Reusing page for code {} (remaining: {})'.format(code, len(codes)))
with open(page_path) as page_file:
page = page_file.read()
else:
log.info(u'Harvesting page for code {} (remaining: {})'.format(code, len(codes)))
response = urllib2.urlopen('http://www.whocc.no/atc_ddd_index/?{}'.format(urllib.urlencode(form_data)))
page = response.read()
html_element = lxml.html.fromstring(page)
a_element_list = html_element.xpath('//div[@id="content"]/b/a')
if not a_element_list:
log.info('No substance with code {}'.format(code))
continue
hierarchy = [
(
substance_url_re.match(a_element.get('href')).group('code'),
lxml.html.tostring(a_element, encoding = unicode, method = 'text').strip()
)
for a_element in a_element_list
]
current_code, name = hierarchy[-1]
assert current_code == code
# print hierarchy
hierarchy_names = ([
item[1]
for item in hierarchy
] + [''] * 4)[:4]
a_element_list = html_element.xpath('//div[@id="content"]/p/b/a')
children = [
(
substance_url_re.match(a_element.get('href')).group('code'),
lxml.html.tostring(a_element, encoding = unicode, method = 'text').strip()
)
for a_element in a_element_list
]
# print children
for child_code, child_name in children:
codes.add(child_code)
with open(page_path, 'w') as page_file:
page_file.write(page)
tr_element_list = html_element.xpath('//div[@id="content"]/ul/table/tr')
if tr_element_list:
labels = [
td_element.text.strip()
for td_element in tr_element_list[0].xpath('td')
]
assert labels == [u'ATC code', u'Name', u'DDD', u'U', u'Adm.R', u'Note'], labels
for tr_element in tr_element_list[1:]:
values = [
lxml.html.tostring(td_element, encoding = unicode, method = 'text').strip()
for td_element in tr_element.xpath('td')
]
values[1:1] = hierarchy_names
substance_by_code[values[0]] = values
print u';'.join([
u'ATC code',
u'Anatomical Main Group',
u'Therapeutic Subgroup',
u'Pharmacological Subgroup',
u'Chemical Subgroup',
u'Chemical Substance',
u'DDD',
u'U',
u'Adm.R',
u'Note',
]).encode('utf-8')
for code, values in sorted(substance_by_code.iteritems()):
print u';'.join(values).encode('utf-8')
return 0
if __name__ == "__main__":
sys.exit(main())