-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_emergency_manager_docs.py
executable file
·159 lines (100 loc) · 4.03 KB
/
parse_emergency_manager_docs.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
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
import os
import csv
import json
from json import JSONEncoder
from json import JSONDecoder
import re
import sys
import pdb
class EMDocsParser():
DATA_TAGS = ('a', 'div')
def __init__(self):
self.category = 'Emergency Manager Orders'
self.sub_category = ''
self.grab_category = False
def get_first_tag(line):
"""
Returns the tag for this line.
"""
match = re.search('<', line)
# In case line has no tags.
if not match:
return '', ''
line = line[match.start() + 1 : ]
# find end of tag
match = re.search('[ >]', line)
if not match:
return '', ''
return line[0 : match.start()], line
def parse_value(line, str_begin, str_end='\"'):
begin = line.find(str_begin)
if begin < 0:
return line, None
begin = begin + len(str_begin)
end = line.find(str_end, begin)
if end < 0:
return line, None
value = line[begin : end].strip()
line = line[end + len(str_end) : ]
return line, value
def parse_line(self, tag, line):
"""
Retrieve relevant content from line.
"""
if tag == 'a':
new_line, title = EMDocsParser.parse_value(line=line, str_begin=' title=\"')
new_line, url = EMDocsParser.parse_value(line=line, str_begin=' href=\"')
url = url.replace("/Portals/0/docs/EM/", "")
line = new_line
if not title or not url:
return None
return { title : url }
elif tag == 'div':
line, class_val = EMDocsParser.parse_value(line=line, str_begin=' class=\"')
if class_val and "dt-faq-item" in class_val:
self.grab_category = True
return None
def get_sub_category(self, line):
begin = line.find('<h3>') + 4
end = line.find('</h3>', begin)
self.sub_category = line[begin : end].strip()
def parse(self, filename):
filename = sys.argv[1]
with open(filename, newline='', encoding="utf8") as input_file:
content = {}
for line in input_file:
if self.category == 'Emergency Manager Reports' and '<h3>' in line:
self.get_sub_category(line)
line = ''
while line:
if self.grab_category:
self.category = line.strip()
self.grab_category = False
tag, line = EMDocsParser.get_first_tag(line)
if tag in EMDocsParser.DATA_TAGS:
line_info = self.parse_line(tag, line)
if line_info:
if self.category == 'Emergency Manager Reports':
if not content.get(self.category):
content[self.category] = {}
if not content[self.category].get(self.sub_category):
content[self.category][self.sub_category] = []
content[self.category][self.sub_category].append(line_info)
else:
if not content.get(self.category):
content[self.category] = []
content[self.category].append(line_info)
elif self.grab_category:
line = ''
data = {
"base_path": "/sites/detroitmi.localhost/files/migrated_docs/emergency-manager-reports/",
"reports": content
}
# print(json.dumps(data))
pdb.set_trace()
if __name__ == "__main__":
if len(sys.argv) < 2:
msg = "usage: {} input_file\n(e.g., {} website_data/emergency_manager_orders/content.html".format(sys.argv[0], sys.argv[0])
raise Exception(msg)
EMDocsParser().parse(sys.argv[1])