Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active January 8, 2021 02:58
Show Gist options
  • Save donpdonp/d34eca66111b9f00d25b1c7317c4d73d to your computer and use it in GitHub Desktop.
Save donpdonp/d34eca66111b9f00d25b1c7317c4d73d to your computer and use it in GitHub Desktop.
gluon covid vaccine
(function() {
setup()
return {name:"vaccine"}
})
var alert_channel = "#portlandor"
var vacdata = {}
function setup() {
vacload(function(data){
vacdata = data
bot.say(bot.admin_channel, "vaccine data "+JSON.stringify(data))
})
}
function vacload(cb) {
db.get("vaccine", function(json){
if (json) {
cb(JSON.parse(json))
}
})
}
function vacsave(data) {
db.set("vaccine", JSON.stringify(data))
}
function go(msg) {
if (msg.method == "irc.privmsg") {
var match = /^!(vaccine)(\s+(.*))?/.exec(msg.params.message)
if(match) {
var vaccine = read_tableau()
var parts = ["OHA vaccine doses", vaccine.last, "given", vaccine.last_date+".", "Total", vaccine.total, '"'+vaccine.total_date+'"']
bot.say(msg.params.channel, parts.join(' '))
}
}
if (msg.method == "clocktower") {
var now = new Date(Date.parse(msg.params.time))
if (now.getMinutes() == 0) {
var vaccine = read_tableau()
if (vaccine.last_date != vacdata.last_date) {
bot.say(bot.admin_channel, "new: "+vaccine.last_date+" (was "+vacdata.last_date+"). saving.")
vacdata = vaccine
vacsave(vaccine)
bot.say(alert_channel, "OHA vaccine doeses for "+vaccine.last_date+" "+vaccine.last)
}
}
}
}
function read_tableau() {
var session_url = 'https://public.tableau.com/views/OregonCOVID-19VaccinationTrends/OregonStatewideVaccinationTrends?%3AshowVizHome=no'
var resp = http.get({url: session_url})
var session_id = resp.headers['X-Session-Id'][0]
url = 'https://public.tableau.com/vizql/w/OregonCOVID-19VaccinationTrends/v/OregonStatewideVaccinationTrends/bootstrapSession/sessions/' + session_id
var params = {url: url, headers: {"Content-Type": "application/x-www-form-urlencoded"}}
var body = 'sheet_id=Oregon%2520Statewide%2520Vaccination%2520Trends'
var resp = http.post(params, body)
var jsons = dice(resp)
bot.say(bot.admin_channel, "tableau Oregon COVID-19 "+resp.length+" bytes. "+jsons.length+" parts")
var vacpart = JSON.parse(jsons[1])
/*
tableau can DIAF
"secondaryInfo": {
"presModelMap": {
"dataDictionary": {
"presModelHolder": {
"genDataDictionaryPresModel": {
"dataSegments": {
"0": {
"dataColumns": [
{
"dataType": "integer",
"dataValues": [
1084,
1046,
1657,
434,
254,
1,
4475,
*/
var data = vacpart['secondaryInfo']['presModelMap']['dataDictionary']['presModelHolder']['genDataDictionaryPresModel']['dataSegments']['0']['dataColumns']
var values = data[0]['dataValues']
var labels = data[1]['dataValues']
var dates = data[2]['dataValues']
//bot.say(bot.admin_channel, "values "+JSON.stringify(values))
//bot.say(bot.admin_channel, "dates "+JSON.stringify(dates))
return {last: values[0], last_date: labels[4], total_date: labels[3], total: values[dates.length+1]}
}
function dice(body) {
var bodies = []
var more = true
while (more) {
bot.say(bot.admin_channel, "*dice of "+body.length+" "+body.slice(0,10)+" ... "+body.slice(body.length-10, body.length))
var semi = body.indexOf(';')
if (semi > -1) {
var len_str = body.slice(0, semi)
var len = parseInt(len_str)
var json_first = semi + 1
var json_bad_last = semi + len
var utf_makeup = 0
var body_utfzone = body.slice(json_bad_last, body.length)
bot.say(bot.admin_channel, "*dice utfzone "+body_utfzone.length+" "+body_utfzone.slice(0,10)+" ... "+body_utfzone.slice(body.length-10, body.length))
var match = /(.*)\d+;/.exec(body_utfzone)
if(match) {
utf_makeup = match[1].length
}
var json_last = json_bad_last + utf_makeup
var part = body.slice(json_first, json_last)
bodies.push(part)
body = body.slice(json_last+1, body.length)
} else {
more = false
}
}
return bodies
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment