Last active
March 17, 2021 02:02
-
-
Save donpdonp/c7276d2dd685de67fc56dc63890a2b3b to your computer and use it in GitHub Desktop.
gluon covid tracker for Oregon
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
(function() { | |
setup() | |
return {name:"covid"} | |
}) | |
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) | |
var death_string = "" | |
//for(var i=0; i < diff_deaths; i++) { death_string += "⚰️ " } | |
//bot.say(bot.admin_channel, "corona debug "+JSON.stringify(new_oregon)+" - "+JSON.stringify(new_oregon)) | |
bot.say("#portlandor", "Oregon processed "+diff_total+" covid tests yesterday"+ | |
" with "+diff_pcent+"%/"+diff_pos+" new positives") | |
//" and "+death_string) | |
} | |
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 tests") | |
if(positive) { | |
oha.positive = positive | |
} | |
var negative = findint(html, "Negative tests") | |
if(negative) { | |
oha.negative = negative | |
} | |
var deaths = findint(html, "Total 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