forked from amazingshellyyy/covid19-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCountyData.js
57 lines (52 loc) · 1.46 KB
/
getCountyData.js
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
const request = require("request");
const cheerio = require("cheerio");
const covidHistory = require('./docs/countyTimeseries.json');
const fs = require('fs');
getCurrentTime = () => {
let time = new Date().getTime();
return time;
}
let covidData = covidHistory;
request({
method: 'GET',
url: "https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_California"
}, (err, res, body) => {
if (err) return console.error(err);
console.log('res', res && res.statusCode)
if (res.statusCode == 200) {
let data = []
let $ = cheerio.load(body);
$('.tp-container tbody tr').each((index, el) => {
// console.log($(el).text())
if (index < 41 && index > 2) {
data.push($(el).text());
}
})
let newData = [];
data.forEach(county => {
const datastr = JSON.stringify(county).split('\\n');
if (datastr[1] && parseInt(datastr[3])) {
const datas = {
"county": datastr[1],
"case": parseInt(datastr[3]),
"death": parseInt(datastr[5])
}
newData.push(datas);
}
})
let timeseriesData = {
timeStamp: getCurrentTime(),
data: newData
}
covidData.push(timeseriesData);
fs.writeFile(`./docs/countyTimeseries.json`, JSON.stringify(covidData, null, 2), function (err) {
if (err) {
console.log(err);
} else {
console.log('finish writing file')
}
})
} else {
console.log('failed',res.statusCode)
}
});