Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active December 10, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/4417189 to your computer and use it in GitHub Desktop.
Save donpdonp/4417189 to your computer and use it in GitHub Desktop.
neuronbot freezing temp watch
function(msg) {
var override = false;
var temp_match = /^temp(\s+(\w+))?$/.exec(msg.message)
if(temp_match) {
override = true;
}
if(override || (msg.type == "ticktock" && (new Date(Date.parse(msg.message))).getMinutes() % 15 == 0) ) {
var station
if(override){
if(temp_match[2]){
station = temp_match[2].toUpperCase()
station_set(msg.nick, station)
} else {
station = station_find(msg.nick)
}
} else {
station = station_find('autotimer')
}
var data = http.get("http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID="+station).replace(/\n/g,"").replace(/\r/g,"")
var entry_ex = /<temp_f>([^<]+)<\/temp_f>/g
var temp = entry_ex.exec(data)[1]
var entry_ex2 = /<neighborhood>([^<]+)<\/neighborhood>/g
var neighborhood = entry_ex2.exec(data)[1]
var entry_ex3 = /<observation_time_rfc822>([^<]+)<\/observation_time_rfc822>/g
var observation = entry_ex3.exec(data)[1]
var date = new Date(observation)
var say = "";
var last_freeze_announce_time = new Date(db.get('pdx_freeze_last_date'))
var freeze_announce_delay_mins = ((new Date()) - last_freeze_announce_time)/1000/60
var observation_delay_mins = ((new Date ()) - date)/1000/60
if(override || (temp < 33 && observation_delay_mins < 60 && freeze_announce_delay_mins > 60*12)) {
if(!override) {
db.set('pdx_freeze_last_date', observation)
say += "Freeze Alert: "
}
say += neighborhood+"/"+station.toLowerCase()+": "+temp+"F. observed "+observation_delay_mins.toFixed(1)+" mins ago"
}
if(override) {
say = msg.nick+": "+say
} else {
bot.emit({type:"temp", temp_f: temp, station: station.toLowerCase(), date: date})
db.set('outside:f', temp)
db.set('outside:time', date)
say = {target:"#pdxtech", message: say}
}
return say
}
function station_find(nick){
return (station_list()[nick]) || "KORPORTL198"
}
function station_set(nick, station){
var stations = station_list()
stations[nick] = station
db.set("weather:stations", JSON.stringify(stations))
}
function station_list(){
var key = "weather:stations"
var stations_json = db.get(key)
var stations
if(stations_json){
stations = JSON.parse(stations_json)
} else {
db.set(key,"{}")
stations = {}
}
return stations
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment