forked from cmu-delphi/delphi-epidata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delphi_epidata.coffee
380 lines (346 loc) · 11.3 KB
/
delphi_epidata.coffee
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
###
A module for DELPHI's Epidata API.
https://github.com/cmu-delphi/delphi-epidata
Notes:
- If running in node.js (or using browserify), there are no external
dependencies. Otherwise, a global jQuery object named `$` must be available.
###
# Use built-in node.js modules unless jQuery is available
unless $?.getJSON?
https = require('https')
querystring = require('querystring')
# Because the API is stateless, the Epidata class only contains static methods
class Epidata
# API base url
BASE_URL = 'https://delphi.cmu.edu/epidata/api.php'
# Helper function to cast values and/or ranges to strings
_listitem = (value) ->
if value.hasOwnProperty('from') and value.hasOwnProperty('to')
return "#{value['from']}-#{value['to']}"
else
return "#{value}"
# Helper function to build a list of values and/or ranges
_list = (values) ->
if not Array.isArray(values)
values = [values]
return (_listitem(value) for value in values).join(',')
# Helper function to request and parse epidata
_request = (callback, params) ->
# Function to handle the API response
handler = (data) ->
if data?.result?
callback(data.result, data.message, data.epidata)
else
callback(0, 'unknown error', null)
# Request data from the server
if $?.getJSON?
# API call with jQuery
$.getJSON(BASE_URL, params, handler)
else
# Function to handle the HTTP response
reader = (response) ->
text = ''
response.setEncoding('utf8')
response.on('data', (chunk) -> text += chunk)
response.on('error', (e) -> error(e.message))
response.on('end', () -> handler(JSON.parse(text)))
# API call with Node
https.get("#{BASE_URL}?#{querystring.stringify(params)}", reader)
# Build a `range` object (ex: dates/epiweeks)
@range = (from, to) ->
if to <= from
[from, to] = [to, from]
return { from: from, to: to }
# Fetch FluView data
@fluview: (callback, regions, epiweeks, issues, lag, auth) ->
# Check parameters
unless regions? and epiweeks?
throw { msg: '`regions` and `epiweeks` are both required' }
if issues? and lag?
throw { msg: '`issues` and `lag` are mutually exclusive' }
# Set up request
params =
'source': 'fluview'
'regions': _list(regions)
'epiweeks': _list(epiweeks)
if issues?
params.issues = _list(issues)
if lag?
params.lag = lag
if auth?
params.auth = auth
# Make the API call
_request(callback, params)
# Fetch FluView metadata
@fluview_meta: (callback) ->
# Set up request
params =
'source': 'fluview_meta'
# Make the API call
_request(callback, params)
# Fetch FluView clinical data
@fluview_clinical: (callback, regions, epiweeks, issues, lag) ->
# Check parameters
unless regions? and epiweeks?
throw { msg: '`regions` and `epiweeks` are both required' }
if issues? and lag?
throw { msg: '`issues` and `lag` are mutually exclusive' }
# Set up request
params =
'source': 'fluview_clinical'
'regions': _list(regions)
'epiweeks': _list(epiweeks)
if issues?
params.issues = _list(issues)
if lag?
params.lag = lag
# Make the API call
_request(callback, params)
# Fetch FluSurv data
@flusurv: (callback, locations, epiweeks, issues, lag) ->
# Check parameters
unless locations? and epiweeks?
throw { msg: '`locations` and `epiweeks` are both required' }
if issues? and lag?
throw { msg: '`issues` and `lag` are mutually exclusive' }
# Set up request
params =
'source': 'flusurv'
'locations': _list(locations)
'epiweeks': _list(epiweeks)
if issues?
params.issues = _list(issues)
if lag?
params.lag = lag
# Make the API call
_request(callback, params)
# Fetch Google Flu Trends data
@gft: (callback, locations, epiweeks) ->
# Check parameters
unless locations? and epiweeks?
throw { msg: '`locations` and `epiweeks` are both required' }
# Set up request
params =
'source': 'gft'
'locations': _list(locations)
'epiweeks': _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch Google Health Trends data
@ght: (callback, auth, locations, epiweeks, query) ->
# Check parameters
unless auth? and locations? and epiweeks? and query?
throw { msg: '`auth`, `locations`, `epiweeks`, and `query` are all required' }
# Set up request
params =
'source': 'ght'
'auth': auth
'locations': _list(locations)
'epiweeks': _list(epiweeks)
'query': query
# Make the API call
_request(callback, params)
# Fetch HealthTweets data
@twitter: (callback, auth, locations, dates, epiweeks) ->
# Check parameters
unless auth? and locations?
throw { msg: '`auth` and `locations` are both required' }
unless dates? ^ epiweeks?
throw { msg: 'exactly one of `dates` and `epiweeks` is required' }
# Set up request
params =
'source': 'twitter'
'auth': auth
'locations': _list(locations)
if dates?
params.dates = _list(dates)
if epiweeks?
params.epiweeks = _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch Wikipedia access data
@wiki: (callback, articles, dates, epiweeks, hours) ->
# Check parameters
unless articles?
throw { msg: '`articles` is required' }
unless dates? ^ epiweeks?
throw { msg: 'exactly one of `dates` and `epiweeks` is required' }
# Set up request
params =
'source': 'wiki'
'articles': _list(articles)
if dates?
params.dates = _list(dates)
if epiweeks?
params.epiweeks = _list(epiweeks)
if hours?
params.hours = _list(hours)
# Make the API call
_request(callback, params)
# Fetch CDC page hits
@cdc: (callback, auth, epiweeks, locations) ->
# Check parameters
unless auth? and epiweeks? and locations?
throw { msg: '`auth`, `epiweeks`, and `locations` are all required' }
# Set up request
params =
'source': 'cdc'
'auth': auth
'epiweeks': _list(epiweeks)
'locations': _list(locations)
# Make the API call
_request(callback, params)
# Fetch Quidel data
@quidel: (callback, auth, epiweeks, locations) ->
# Check parameters
unless auth? and epiweeks? and locations?
throw { msg: '`auth`, `epiweeks`, and `locations` are all required' }
# Set up request
params =
'source': 'quidel'
'auth': auth
'epiweeks': _list(epiweeks)
'locations': _list(locations)
# Make the API call
_request(callback, params)
# Fetch NoroSTAT data (point data, no min/max)
@norostat: (callback, auth, location, epiweeks) ->
# Check parameters
unless auth? and location? and epiweeks?
throw { msg: '`auth`, `location`, and `epiweeks` are all required' }
# Set up request
params =
'source': 'norostat'
'auth': auth
'location': location
'epiweeks': _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch NoroSTAT metadata
@meta_norostat: (callback, auth) ->
# Check parameters
unless auth?
throw { msg: '`auth` is required' }
# Set up request
params =
'source': 'meta_norostat'
'auth': auth
# Make the API call
_request(callback, params)
# Fetch AFHSB data (point data, no min/max)
@afhsb: (callback, auth, locations, epiweeks, flu_types) ->
# Check parameters
unless auth? and locations? and epiweeks? and flu_types?
throw { msg: '`auth`, `locations`, `epiweeks` and `flu_types` are all required' }
# Set up request
params =
'source': 'afhsb'
'auth': auth
'locations': _list(locations)
'epiweeks': _list(epiweeks)
'flu_types': _list(flu_types)
# Make the API call
_request(callback, params)
# Fetch AFHSB metadata
@meta_afhsb: (callback, auth) ->
# Check parameters
unless auth?
throw { msg: '`auth` is required' }
# Set up request
params =
'source': 'meta_afhsb'
'auth': auth
# Make the API call
_request(callback, params)
# Fetch NIDSS flu data
@nidss_flu: (callback, regions, epiweeks, issues, lag) ->
# Check parameters
unless regions? and epiweeks?
throw { msg: '`regions` and `epiweeks` are both required' }
if issues? and lag?
throw { msg: '`issues` and `lag` are mutually exclusive' }
# Set up request
params =
'source': 'nidss_flu'
'regions': _list(regions)
'epiweeks': _list(epiweeks)
if issues?
params.issues = _list(issues)
if lag?
params.lag = lag
# Make the API call
_request(callback, params)
# Fetch NIDSS dengue data
@nidss_dengue: (callback, locations, epiweeks) ->
# Check parameters
unless locations? and epiweeks?
throw { msg: '`locations` and `epiweeks` are both required' }
# Set up request
params =
'source': 'nidss_dengue'
'locations': _list(locations)
'epiweeks': _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch Delphi's forecast
@delphi: (callback, system, epiweek) ->
# Check parameters
unless system? and epiweek?
throw { msg: '`system` and `epiweek` are both required' }
# Set up request
params =
'source': 'delphi'
'system': system
'epiweek': epiweek
# Make the API call
_request(callback, params)
# Fetch Delphi's digital surveillance sensors
@sensors: (callback, auth, names, locations, epiweeks) ->
# Check parameters
unless auth? and names? and locations? and epiweeks?
throw { msg: '`auth`, `names`, `locations`, and `epiweeks` are all required' }
# Set up request
params =
'source': 'sensors'
'auth': auth
'names': _list(names)
'locations': _list(locations)
'epiweeks': _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch Delphi's wILI nowcast
@nowcast: (callback, locations, epiweeks) ->
# Check parameters
unless locations? and epiweeks?
throw { msg: '`locations` and `epiweeks` are both required' }
# Set up request
params =
'source': 'nowcast'
'locations': _list(locations)
'epiweeks': _list(epiweeks)
# Make the API call
_request(callback, params)
# Fetch API metadata
@meta: (callback) ->
_request(callback, {'source': 'meta'})
# Fetch Delphi's COVID-19 Surveillance Streams
@covidcast: (callback, data_source, signal, time_type, geo_type, time_values, geo_value) ->
# Check parameters
unless data_source? and signal? and time_type? and geo_type? and time_values? and geo_values?
throw { msg: '`data_source`, `signal`, `time_type`, `geo_type`, `time_values`, and `geo_value` are all required' }
# Set up request
params =
'source': 'covidcast'
'data_source': data_source
'signal': signal
'time_type': time_type
'geo_type': geo_type
'time_values': _list(time_values)
'geo_value': geo_value
# Make the API call
_request(callback, params)
# Fetch Delphi's COVID-19 Surveillance Streams metadata
@covidcast_meta: (callback) ->
_request(callback, {'source': 'covidcast_meta'})
# Export the API to the global environment
(exports ? window).Epidata = Epidata