-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid19tokyo.js
65 lines (59 loc) · 1.91 KB
/
covid19tokyo.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
57
58
59
60
61
62
63
64
65
const util = require('./util.js')
const CACHE_TIME = 1 * 60 * 60 * 1000 // 1hour
//const CACHE_TIME = 1 * 60 * 1000 // 1min
const PATH = 'data/covid19tokyo/'
const URL = 'https://raw.githubusercontent.com/tokyo-metropolitan-gov/covid19/master/data/data.json'
const getCovid19Data = async function() {
return await util.getWebWithCache(URL, PATH)
}
const getLastUpdate = function(fn) {
return util.getLastUpdateOfCache(URL, PATH)
}
const startUpdate = function() {
setInterval(async function() {
await util.getWebWithCache(URL, PATH, CACHE_TIME)
}, CACHE_TIME)
}
const getCovid19DataJSON = async function() {
return JSON.parse(await getCovid19Data())
}
const getCovid19DataSummary = async function() {
const json = await getCovid19DataJSON()
return {
'n_inspections': json.main_summary.value,
'n_patients': json.main_summary.children[0].value,
'n_light': json.main_summary.children[0].children[0].children[0].value,
'n_heavy': json.main_summary.children[0].children[0].children[1].value,
'n_exit': json.main_summary.children[0].children[1].value,
'n_death': json.main_summary.children[0].children[2].value,
's_lastUpdate': json.lastUpdate,
}
}
const simplejson2txt = function(json) {
const res = []
for (const name in json) {
res.push(name)
res.push(json[name])
}
res.splice(0, 0, res.length / 2)
res.push('')
return res.join('\r\n')
}
const getCovid19DataSummaryForIchigoJam = async function() {
const json = await getCovid19DataSummary()
return simplejson2txt(json)
}
const main = async function() {
const data = await getCovid19Data()
console.log(data)
const d = await getCovid19DataSummaryForIchigoJam()
console.log(d)
}
if (require.main === module) {
main()
} else {
startUpdate()
}
exports.getCovid19DataJSON = getCovid19DataJSON
exports.getCovid19DataSummary = getCovid19DataSummary
exports.getCovid19DataSummaryForIchigoJam = getCovid19DataSummaryForIchigoJam