Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active August 6, 2017 18:34
Show Gist options
  • Save donpdonp/69341ca5be0be81e537f to your computer and use it in GitHub Desktop.
Save donpdonp/69341ca5be0be81e537f to your computer and use it in GitHub Desktop.
gluon multco bridges
(function() {
setup()
// descriptor
return {name:"multco-bridges"}
})
var bridges = {}
var cache_date
function setup() {
db.get('multco:bridges:token', function(thekey){
if(thekey) {
token = thekey
bot.say('#zrobo', 'multco bridge token loaded. v4')
} else {
bot.say('#zrobo', 'multco bridge token missing')
}
})
}
function go(msg) {
var go = false
if(msg.method == "clocktower") {
var now = new Date(msg.params.time)
//bot.say('#zrobo', 'bridge clocktower')
refresh()
}
if(msg.method == "irc.privmsg") {
var temp_match = /^bridges?(\s+(\w+))?$/.exec(msg.params.message)
if(temp_match) {
go = true;
}
}
if(go) {
if(Object.keys(bridges).length == 0){
bot.say(msg.params.channel, 'cache empty. hitting multco api')
refresh()
}
var say = Object.keys(bridges).map(function(name){
var bridge = bridges[name]
var words = bridge.name+" is "+(bridge.isUp ? "UP" : "down")
if(bridge.events && bridge.events.length > 0) {
var event_date = new Date(bridge.events[0].upTime)
words += " ("+pretty_date(event_date)+" last lift)"
}
return words
}).join(', ')
say += " (cache "+pretty_date(cache_date)+")"
bot.say(msg.params.channel, say)
}
}
function refresh() {
var json = http.get('https://api.multco.us/bridges?access_token='+token)
if (json) {
var info = JSON.parse(json)
cache_date = new Date()
info.map(function(bridge){
var bjson = http.get('https://api.multco.us/bridges/'+bridge.name+'/events/actual/1?access_token='+token)
var events = JSON.parse(bjson)
bridge.events = events
var old_bridge = bridges[bridge.name]
if(old_bridge && old_bridge.events && bridge.events && old_bridge.events[0].id != bridge.events[0].id) {
var duration = (new Date() - new Date(bridge.events[0].upTime)) / 1000 / 60
bot.say('#pdxbots', bridge.name+" bridge opened at "+pretty_date(new Date(bridge.events[0].upTime))+
" for "+duration.toFixed(0)+" minutes and just closed.")
}
if(old_bridge && old_bridge.isUp != bridge.isUp) {
if(bridge.isUp) {
//bot.say('#pdxbots', ''+bridge.name+' bridge is '+(bridge.isUp ? 'raising' : 'down')+".")
}
}
bridges[bridge.name] = bridge
})
var bcount = upcount()
if(bcount == 3) {
bot.say('#pdxbots', 'Bridge lift trifecta!')
}
}
}
function upcount() {
return Object.keys(bridges).reduce(function(count, name){
if (bridges[name].isUp) {
count += 1
}
return count
},0)
}
function pretty_date(date) {
var distance = new Date() - date
var day = ""
if(distance > 1000*60*60*12) {
day = (date.getMonth()+1)+"/"+date.getDate()+" "
}
var hours = date.getHours()
if(hours < 12) {
ampm = "am"
} else {
ampm = "pm"
if(hours > 12) {
hours = hours - 12
}
}
var minutes = date.getMinutes()
if(minutes < 10) { minutes = "0"+minutes }
return day+hours+":"+minutes+ampm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment