forked from dportnoy/health-insurance-marketplace-analytics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine_readable_puf_crawler.py
239 lines (177 loc) · 6.9 KB
/
machine_readable_puf_crawler.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import shutil
import zipfile
import pandas as pd
# import xlrd # library for reading xlsx, but can use pandas.read_excel()
import json
import requests
from urllib import parse
import os
global debug
def get_unique_urls():
ps = df['URL Submitted'].value_counts()
for i, v in ps.iteritems():
print(i)
print(v)
def read_json_url(url):
global global_error
global_error = ''
try:
r = requests.get(url, allow_redirects=True)
except requests.exceptions.RequestException as error: # This is the correct syntax
print (error)
global_error = str(error)
return None, None
if r.status_code == 200:
# Note: As of 2/17/2016 Content-type is sometimes not json
if not 'json' in r.headers.get('content-type',''):
global_error += 'content-type should not be: '+ r.headers.get('content-type','missing')+"'\n"
if debug: print("Good!")
json_data = json.loads(r.text)
metadata = save_response_metadata(url, r) # Need elapsed.total_seconds()
return (json_data, metadata)
else:
if debug: print("Not good! Status code:" + str(r.status_code) + " Content-type: " + r.headers.get('content-type',''))
return None, None
return
def save_response_metadata(url, response):
metadata = {}
metadata['status_code'] = response.status_code
metadata['history'] = response.history
metadata['url'] = response.url
metadata['_diff_url'] = (response.url == url)
metadata['headers'] = response.headers
metadata['elapsed'] = response.elapsed
metadata['total_secs'] = response.elapsed.total_seconds()
metadata['file_name' ] = os.path.basename(parse.urlparse(url).path)
return metadata
def load_zip_to_df(full_dst):
zfile = zipfile.ZipFile(full_dst)
zfile.extractall()
global df
# Read into Pandas DF
for file_name in zfile.namelist():
print("Loading: "+file_name)
df = pd.read_excel(file_name)
df.as_matrix()
print("Records loaded: "+str(len(df)))
break
return
def count_url_items(json_url, max_depth=1, response_times = [], parent_key = 'index_url_items'):
if debug: print("\n\n\n--------------------------")
if debug: print("index = "+str(index))
if debug: print("json_url = "+str(json_url))
if debug: print("max_depth = "+str(max_depth))
if max_depth == 0:
if debug: print("Exiting")
return 0
max_depth -= 1
json_data, metadata = read_json_url(json_url)
if json_data == None:
if debug: print("--- Nonetype!")
return 0
response_times.append(metadata['total_secs']) # List of times to calculate
df.loc[index, 'avg_response_time'] = str(response_times) # Update when it changes; Can't assign dict to df value
if debug: print("type(json_data) = "+str(type(json_data)))
if isinstance(json_data, dict):
for dict_key, dict_value in json_data.items():
if isinstance(dict_value, list):
count = len(dict_value)
if debug: print("# of items for "+ dict_key+" : " + str(count))
df.loc[index, dict_key] = count
if max_depth >= 1:
for list_key, list_value in enumerate(dict_value):
count = count_url_items(list_value, max_depth, response_times, dict_key+'_items')
#if debug: print(list_value)
else:
count = 1
if debug: print("# of items for "+ dict_key+" : 1")
df.loc[index, dict_key] = count
if max_depth >= 1:
count = count_url_items(dict_value, max_depth, response_times, dict_key+'_items')
return count
#if debug: print(dict_value)
elif isinstance(json_data, list):
count = len(json_data)
if debug: print("# of items for "+ str(parent_key) +" : " + str(count))
df.loc[index, str(parent_key)] = count
else:
if debug: print("Error: json is not dictionary")
return 0
return
# This is a custom alternative to use of parse.urlparse(url)
# because sometimes it doesn't work well
# Simple custom rules for checking validity
def validate_url_custom(url):
url = url.strip()
returned_url = ''
if url.find(' ')==-1 and url.find('.')>0 and url.find('.')<len(url)-1:
if not url.lower().find('http')==0:
# Protocol not specified, but no spaces and with periods. Try adding http://"
url = 'http://' + url
else:
# Bad! Spaces or no periods"
url = ''
return url
def save_to_excel():
writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')
df.to_excel(writer,'Sheet1')
writer.save()
return
from IPython.display import display, clear_output
import sys
def print_same_line(print_str):
'''
#=== stdout method ===
sys.stdout.write('\r'+print_str)
sys.stdout.flush()
'''
#=== iPython method ===
if not debug:
clear_output(wait=True)
print(print_str+"\n")
sys.stdout.flush()
else:
print(print_str+"\n")
return
def main(start_row=0 , end_row=9, max_depth = 1):
global df
global index
global global_error
global_error = ''
# Test
src = '../DDOD-HealthData.gov/snapshots/'
src += 'machine-readable-url-puf_2016-02-11.zip'
dst = '.'
print(src)
print(dst)
full_dst = shutil.copy2(src, dst) # copy2 preserves metadata
load_zip_to_df(full_dst)
#:=== Initialize columns
df['Status'] = '' # Add column now to prevent errors
df['avg_response_time'] = '' # Add column now to prevent errors
#:=== If dictionaries are better
#url_dict = pd.DataFrame.to_dict(df,'records')
# Loop through all rows
#===== Dataframe version ======
for index, row in df.loc[start_row:end_row].iterrows():
print_same_line("Reading row: "+str(index)+"\n")
#print(index)
#print(row)
url_submit = row['URL Submitted']
url_clean = validate_url_custom(url_submit)
if url_clean:
row_status = count_url_items(url_clean, max_depth, [])
df.loc[index, 'Status'] = 'Read' if (global_error == '') else global_error # Can't assign dict
else:
df.loc[index, 'Status'] = "Missing"
if df.loc[index, 'avg_response_time']:
time_list = json.loads(df.loc[index, 'avg_response_time'])
time_average = format(pd.np.mean(time_list),'.3f')
df.loc[index, 'avg_response_time'] = time_average
if debug:
print('\n\n----- Row -----')
print(df.iloc[[index]])
if index >= end_row: break
save_to_excel()
print("\nDone!")
return