Created
April 5, 2020 21:46
-
-
Save MusikAnimal/608d5f568ca69cc0e8c55aa915a42fc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Node script to import COVID-19 data from nyc.gov's official dataset. | |
* The raw JSON should be pasted into https://commons.wikimedia.org/wiki/Data:COVID-19_cases_in_New_York_City.tab | |
* The wikitext is for the table at https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_New_York_City | |
*/ | |
const getScript = (url) => { | |
return new Promise((resolve, reject) => { | |
const http = require('http'), | |
https = require('https'); | |
let client = http; | |
if (url.toString().indexOf("https") === 0) { | |
client = https; | |
} | |
client.get(url, (resp) => { | |
let data = ''; | |
// A chunk of data has been recieved. | |
resp.on('data', (chunk) => { | |
data += chunk; | |
}); | |
// The whole response has been received. Print out the result. | |
resp.on('end', () => { | |
resolve(data); | |
}); | |
}).on("error", (err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
(async (url) => { | |
const contents = await getScript(url); | |
const lines = contents.split('\n'); | |
let total_cases = 0; | |
let total_deaths = 0; | |
console.log(' "data": ['); | |
console.log(' ['); | |
lines.slice(1).forEach(line => { | |
const [ date, new_cases, _hosp_cases, deaths ] = line.split(','); | |
const date_str = new Date(Date.parse(date)).toISOString().slice(0, 10); | |
total_cases += parseInt(new_cases, 10); | |
total_deaths += parseInt(deaths, 10) || 0; | |
console.log(`[ "${date_str}", ${total_cases}, ${new_cases}, ${total_deaths}, ${parseInt(deaths, 10) || 0} ],`); | |
}); | |
let last_total_cases; | |
let last_total_deaths; | |
total_deaths = 0; | |
total_cases = 0; | |
lines.slice(1).forEach(line => { | |
const [ date, new_cases, _hosp_cases, deaths ] = line.split(','); | |
const date_str = new Date(Date.parse(date)).toLocaleString('en', {month: 'long', day: 'numeric', year: 'numeric'}); | |
total_cases += parseInt(new_cases, 10); | |
total_deaths += parseInt(deaths, 10) || 0; | |
cases_pct_change = !last_total_cases | |
? 'N/A' | |
: parseFloat((((total_cases - last_total_cases) / last_total_cases) * 100).toFixed(2)); | |
deaths_pct_change = !last_total_deaths | |
? 'N/A' | |
: parseFloat((((total_deaths - last_total_deaths) / last_total_deaths) * 100).toFixed(2)); | |
console.log('|-'); | |
console.log(`|${date_str}`); | |
console.log(`|${new Number(total_cases).toLocaleString()}`); | |
console.log(`|${cases_pct_change}`); | |
console.log(`|${new Number(total_deaths).toLocaleString()}`); | |
console.log(`|${deaths_pct_change}`); | |
last_total_cases = total_cases; | |
last_total_deaths = total_deaths; | |
}); | |
})('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/case-hosp-death.csv'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment