Skip to content

Instantly share code, notes, and snippets.

@donpdonp donpdonp/corona.js

Last active Jun 22, 2020
Embed
What would you like to do?
gluon corona tracker
(function() {
setup()
return {name:"corona"}
})
var alert_channel = '#pdxbots'
var locations
var data_date
var oregon
function setup() {
jhu_csse(bot.admin_channel)
oregon = oregon_oha()
bot.say(bot.admin_channel, "corona oregon count set to pos "+oregon.positive+" neg "+oregon.negative)
}
function go(msg) {
if (msg.method == "clocktower") {
var now = new Date(Date.parse(msg.params.time))
if (now.getMinutes() == 0) {
var new_oregon = oregon_oha()
var total = oregon.positive + oregon.negative
var new_total = new_oregon.positive + new_oregon.negative
if (new_total > total) {
var diff_total = new_total - total
var diff_pos = new_oregon.positive - oregon.positive
var diff_pcent = ((diff_pos / diff_total) * 100).toFixed(1)
var diff_deaths = new_oregon.deaths - oregon.deaths
var hosp_pcent =(oregon.hosp/new_oregon.positive * 100).toFixed(1)
//bot.say(bot.admin_channel, "corona debug "+JSON.stringify(new_oregon)+" - "+JSON.stringify(new_oregon))
bot.say("#portlandor", "Oregon processed "+diff_total+" corona tests yesterday (from "+total+" to "+new_total+")"+
" with "+diff_pcent+"%/"+diff_pos+" new positives (from "+oregon.positive+" to "+new_oregon.positive+
" and "+hosp_pcent+"%/"+oregon.hosp+" hospitalized)"+
" and "+diff_deaths+" more deaths")
}
oregon = new_oregon
}
}
if (msg.method == "irc.privmsg") {
var match = /^!(corona|covid|covid19)(\s+(.*))?/.exec(msg.params.message)
if(match) {
if (match[3]) {
jhu_csse(bot.admin_channel)
bot.say(msg.params.channel, "searching corona data "+locations.length+" locations. last fetched "+data_date)
var region = match[2].trim()
var report = location_search(region)
if(report) {
bot.say(msg.params.channel, [report.country, report.province].join(', ')+": "+JSON.stringify(report.latest))
} else {
bot.say(msg.params.channel, region+" not found")
}
} else {
var oha = oregon_oha()
bot.say(msg.params.channel, "OregonHealthAuthority "+JSON.stringify(oha))
}
}
}
}
function oregon_oha() {
var oha = {}
var url = 'https://govstatus.egov.com/OR-OHA-COVID-19'
var html = http.get(url).replace(/\n/g,"").replace(/\r/g,"")
var match = / of (\d+\/\d+\/\d+[, ]+\d+:\d+\s+....)/.exec(html)
if (match) {
oha.date = match[1]
}
var positive = findint(html, "Positive")
if(positive) {
oha.positive = positive
}
var negative = findint(html, "Negative")
if(negative) {
oha.negative = negative
}
var deaths = findint(html, "deaths")
if(deaths) {
oha.deaths = deaths
}
var hosp = findint(html, "Yes")
if(hosp) {
oha.hosp = hosp
}
return oha
}
function findint(html, word) {
var exp = word + '.*?>((,?\\d)+)[ *]?<'
var re = new RegExp(exp, "i")
var match = re.exec(html)
if (match) {
var value = match[1].replace(/,/g,"")
return parseInt(value)
}
}
function location_search(region) {
var winner
locations.forEach(function(l){
var term = region.toLowerCase()
if (l.country.toLowerCase() == term ||
l.country_code.toLowerCase() == term ||
l.province.toLowerCase() == term
) {
winner = l
}
})
return winner
}
function jhu_csse(channel) {
var url = 'https://coronavirus-tracker-api.herokuapp.com/v2/locations'
bot.say(channel, "loading "+url)
var json = http.get(url)
if(json.length == 0) {
bot.say(channel, "warning: empty response from coronavirus tracker api")
}
locations = JSON.parse(json).locations
data_date = new Date()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.