-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_download.py
125 lines (104 loc) · 3.41 KB
/
data_download.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
from time import sleep
import requests
from datetime import datetime
import os
from data_validate import return_good_zips
from data_validate import get_url
DEBUG = True
# A list of DE ZIP codes from data-validate.py
ZIP_CODES_DE = return_good_zips()
def get_per_zip_data():
"""
Saves the data for every DE zip code from URLs in the form
https://myhealthycommunity.dhss.delaware.gov/locations/zip-code-19734/download_covid_19_data.
"""
url_start = 'https://myhealthycommunity.dhss.delaware.gov/locations/zip-code-'
url_end = '/download_covid_19_data'
if DEBUG:
print('Running get_per_zip_data')
for code in ZIP_CODES_DE:
sleep(1)
code = str(code)
if DEBUG:
print('On ZIP code: ' + code)
url = url_start + code + url_end
current_time = get_datetime()
file_title = current_time + '.csv'
partial_path = os.path.join(get_path(), 'data', 'DE-by-ZIP', code)
final_path = os.path.join(partial_path, file_title)
url_data = get_url(url).content
if DEBUG:
print('ZIP data final path: ' + final_path)
# Check if the directory we wants exist,
# if it doesn't then make it
if not os.path.exists(partial_path):
if DEBUG:
print('Dir does not exist, making dir: ' + partial_path)
os.makedirs(partial_path)
else:
if DEBUG:
print('Dir exists: ' + partial_path)
save_data(final_path, url_data, 'wb+')
def get_all_de_data():
"""
Saves the CSV with all DE data from
https://myhealthycommunity.dhss.delaware.gov/locations/state/download_covid_19_data.
"""
current_time = get_datetime()
file_title = current_time + '_ALL.csv'
final_path = os.path.join(get_path(), 'data', 'DE-all', file_title)
url = 'https://myhealthycommunity.dhss.delaware.gov/locations/state/download_covid_19_data'
url_data = get_url(url).content
if DEBUG:
print('All data final path: ' + final_path)
save_data(final_path, url_data, 'wb+')
def get_path():
"""
Returns the proper OS independent relative path.
"""
current_folder = os.path.dirname(os.path.abspath(__file__))
if DEBUG:
print('Current folder: ' + current_folder)
return current_folder
def get_datetime():
"""
Returns the current time as a string.
Used to create file titles.
"""
time = str(datetime.today().strftime('%Y%m%d-%H%M%S'))
if DEBUG:
print('Current time: ' + time)
return time
def save_data(save_path, file_data, write_mode):
"""
Saves data to a given path.
Write mode should be 'wb+' for binary files
and 'w+' for text data.
"""
try:
with open(save_path, write_mode) as p:
if DEBUG:
print('Trying to write to: ' + save_path)
print('Write mode: ' + write_mode)
p.write(file_data)
except Exception as e:
print('Error in save_data')
print(e)
def main():
"""
Starts the program.
"""
print('Options...')
print('1: Save data for all of DE')
print('2: Save ZIP code level data')
choice = input('Enter what you want to do: 1, 2 ')
if choice == '1':
get_all_de_data()
elif choice == '2':
get_per_zip_data()
else:
print('Invalid input...')
print('End of program')
print()
if __name__ == '__main__':
main()