42
42
from delphi .utils .epiweek import delta_epiweeks , range_epiweeks , add_epiweeks
43
43
from delphi .utils .epidate import EpiDate
44
44
45
+
45
46
def ensure_tables_exist ():
46
- (u ,p ) = secrets .db .epi
47
- cnx = mysql .connector .connect (user = u ,password = p ,database = ' epidata' )
47
+ (u , p ) = secrets .db .epi
48
+ cnx = mysql .connector .connect (user = u , password = p , database = " epidata" )
48
49
try :
49
50
cursor = cnx .cursor ()
50
- cursor .execute ('''
51
+ cursor .execute (
52
+ """
51
53
CREATE TABLE IF NOT EXISTS `kcdc_ili` (
52
54
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
53
55
`release_date` DATE NOT NULL,
@@ -58,69 +60,76 @@ def ensure_tables_exist():
58
60
`ili` DOUBLE NOT NULL,
59
61
UNIQUE KEY (`issue`, `epiweek`, `region`)
60
62
);
61
- ''' );
63
+ """
64
+ )
62
65
cnx .commit ()
63
66
finally :
64
67
cnx .close ()
65
68
69
+
66
70
def safe_float (f ):
67
71
try :
68
- return float (f .replace (',' , '' ))
72
+ return float (f .replace ("," , "" ))
69
73
except :
70
74
return 0
71
75
76
+
72
77
def safe_int (i ):
73
78
try :
74
- return int (i .replace (',' , '' ))
79
+ return int (i .replace ("," , "" ))
75
80
except :
76
81
return 0
77
82
78
- def get_rows (cnx , table = 'kcdc_ili' ):
79
- # Count and return the number of rows in the `kcdc_ili` table.
80
- select = cnx .cursor ()
81
- select .execute ('SELECT count(1) num FROM %s' % table )
82
- for (num ,) in select :
83
- pass
84
- select .close ()
85
- return num
83
+
84
+ def get_rows (cnx , table = "kcdc_ili" ):
85
+ # Count and return the number of rows in the `kcdc_ili` table.
86
+ select = cnx .cursor ()
87
+ select .execute ("SELECT count(1) num FROM %s" % table )
88
+ for (num ,) in select :
89
+ pass
90
+ select .close ()
91
+ return num
92
+
86
93
87
94
def get_kcdc_data ():
88
95
issue = EpiDate .today ().get_ew ()
89
- last_season = issue // 100 + (1 if issue % 100 > 35 else 0 )
90
- url = 'http://www.cdc.go.kr/npt/biz/npp/iss/influenzaListAjax.do'
96
+ last_season = issue // 100 + (1 if issue % 100 > 35 else 0 )
97
+ url = "https://www.cdc.go.kr/npt/biz/npp/iss/influenzaListAjax.do"
98
+ # Started in 2004
91
99
params = {
92
- ' icdNm' : ' influenza' ,
93
- 'startYear' : '2004' , # Started in 2004
94
- ' endYear' : str (last_season )
100
+ " icdNm" : " influenza" ,
101
+ "startYear" : " 2004" ,
102
+ " endYear" : str (last_season ),
95
103
}
96
104
response = requests .post (url , params )
97
105
datas = response .json ()
98
- data = datas [' data' ]
106
+ data = datas [" data" ]
99
107
ews = []
100
108
ilis = []
101
109
ew1 = 200436
102
- for year in range (2004 ,last_season ):
103
- year_data = data [year - 2004 ]
110
+ for year in range (2004 , last_season ):
111
+ year_data = data [year - 2004 ]
104
112
if year > 2004 :
105
113
ew1 = ews [- 1 ] + 1
106
- ili_yr = year_data ["VALUE" ].split ('`' )
107
- ili_yr = [float (f ) for f in ili_yr if f != '' ]
108
- ew2 = add_epiweeks (ew1 ,len (ili_yr ))
109
- new_ews = list (range_epiweeks (ew1 ,ew2 ))
114
+ ili_yr = year_data ["VALUE" ].split ("`" )
115
+ ili_yr = [float (f ) for f in ili_yr if f != "" ]
116
+ ew2 = add_epiweeks (ew1 , len (ili_yr ))
117
+ new_ews = list (range_epiweeks (ew1 , ew2 ))
110
118
for i in range (len (new_ews )):
111
119
j = float (ili_yr [i ])
112
120
ilis .append (j )
113
121
ews .append (new_ews [i ])
114
122
return ews , ilis
115
123
124
+
116
125
def update_from_data (ews , ilis , date , issue , test_mode = False ):
117
126
u , p = secrets .db .epi
118
- cnx = mysql .connector .connect (user = u , password = p , database = ' epidata' )
127
+ cnx = mysql .connector .connect (user = u , password = p , database = " epidata" )
119
128
rows1 = get_rows (cnx )
120
- print (' rows before: %d' % (rows1 ))
129
+ print (" rows before: %d" % (rows1 ))
121
130
insert = cnx .cursor ()
122
131
123
- sql = '''
132
+ sql = """
124
133
INSERT INTO
125
134
`kcdc_ili` (`release_date`, `issue`, `epiweek`, `region`, `lag`,
126
135
`ili`)
@@ -129,15 +138,15 @@ def update_from_data(ews, ilis, date, issue, test_mode=False):
129
138
ON DUPLICATE KEY UPDATE
130
139
`release_date` = least(`release_date`, '%s'),
131
140
`ili` = %s
132
- '''
141
+ """
133
142
134
143
for i in range (len (ews )):
135
144
ew = ews [i ]
136
145
ili = ilis [i ]
137
146
lag = delta_epiweeks (ews [i ], issue )
138
147
139
- insert_args = [date ,issue ,ew ,' ROK' , lag ,ili ]
140
- update_args = [date ,ili ]
148
+ insert_args = [date , issue , ew , " ROK" , lag , ili ]
149
+ update_args = [date , ili ]
141
150
try :
142
151
insert .execute (sql % tuple (insert_args + update_args ))
143
152
except Exception :
@@ -146,34 +155,33 @@ def update_from_data(ews, ilis, date, issue, test_mode=False):
146
155
# cleanup
147
156
insert .close ()
148
157
if test_mode :
149
- print (' test mode, not committing' )
158
+ print (" test mode, not committing" )
150
159
rows2 = rows1
151
160
else :
152
161
cnx .commit ()
153
162
rows2 = get_rows (cnx )
154
- print (' rows after: %d (added %d)' % (rows2 ,rows2 - rows1 ))
163
+ print (" rows after: %d (added %d)" % (rows2 , rows2 - rows1 ))
155
164
cnx .close ()
156
165
166
+
157
167
def main ():
158
168
# args and usage
159
169
parser = argparse .ArgumentParser ()
160
170
parser .add_argument (
161
- '--test' ,
162
- action = 'store_true' ,
163
- help = 'do dry run only, do not update the database'
171
+ "--test" , action = "store_true" , help = "do dry run only, do not update the database"
164
172
)
165
173
args = parser .parse_args ()
166
174
167
- date = datetime .datetime .now ().strftime (' %Y-%m-%d' )
168
- print (' assuming release date is today, %s' % date )
175
+ date = datetime .datetime .now ().strftime (" %Y-%m-%d" )
176
+ print (" assuming release date is today, %s" % date )
169
177
issue = EpiDate .today ().get_ew ()
170
178
171
179
ensure_tables_exist ()
172
180
173
- ews ,ilis = get_kcdc_data ()
181
+ ews , ilis = get_kcdc_data ()
174
182
175
183
update_from_data (ews , ilis , date , issue , test_mode = args .test )
176
184
177
185
178
- if __name__ == ' __main__' :
186
+ if __name__ == " __main__" :
179
187
main ()
0 commit comments