Last active
June 9, 2021 21:01
-
-
Save donpdonp/f70b57ff23ae1e2d90d0a39b3255c0f0 to your computer and use it in GitHub Desktop.
gluon spacex road closure watch
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: "spacex" | |
} | |
}) | |
var closurecount = 1 | |
var alert_channel = "#zrobo" | |
function setup() { | |
} | |
function go(msg) { | |
if (msg.method == "clocktower") { | |
var time = new Date(Date.parse(msg.params.time)) | |
if (time.getMinutes() == 0) { | |
var dates = scrape() | |
if (dates.length != closurecount) { | |
bot.say(alert_channel, "spacex road closure count changed from "+closurecount+" to "+dates.length+". use !spacex") | |
closurecount = dates.length | |
} | |
} | |
} | |
if(msg.method == "irc.privmsg") { | |
var cmd = /^\!spacex(\s+(.+))?$/ | |
var match = cmd.exec(msg.params.message) | |
if(match){ | |
var dates = scrape().map(datewords) | |
bot.say(msg.params.channel, "spacex " + dates.join(', ')) | |
} | |
} | |
} | |
function datewords(date) { | |
return date[1] + " " + date[2] + " " + date[3] | |
} | |
function scrape() { | |
var url = "https://www.cameroncounty.us/spacex/" | |
var html = http.get(url).replace(/\n/g,"").replace(/\r/g,"") | |
var body = /<tbody>(.*)<\/tbody>/.exec(html) | |
var rows = body[1].split("</tr>").map(function(t){return t.replace(/<tr>/,"")}) | |
return rows.reduce(function(memo, hrow){ | |
var s = rowsplit(hrow) | |
if(s.length > 0) { memo.push(s) } | |
return memo | |
}, []) | |
} | |
function rowsplit(data) { | |
var data = data.replace(/<\/?span[^>]*>/g,"") | |
var data = data.replace(/<td[^>]*>/g,"") | |
var data = data.replace(/<\/td[^>]*>/g,"__") | |
var data = data.replace(/<br[^>]*\/>/g,"__") | |
var parts = data.split('__') | |
return parts.reduce(function(m,p){ | |
if (p.length > 0) { m.push(p) } | |
return m | |
},[]) | |
} | |
/* | |
<tbody> | |
<tr> | |
<td><span style="color: #000000;">Primary Date</span></td> | |
<td style="text-align: center;"><span style="color: #000000;">Friday, May 21, 2021 </span></td> | |
<td style="text-align: center;"><span style="color: #000000;">9:30 a.m. to 12:00 p.m. </span></td> | |
<td style="text-align: center;"><span style="color: #000000;">Beach Open, Intermittent Road Closure<br /> | |
</span></td> | |
</tr> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment