Last active
June 4, 2019 19:56
-
-
Save donpdonp/a95f4ec39f9758581006caeeda419fa5 to your computer and use it in GitHub Desktop.
gluon shift2bikes pedalpalooza calendar
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() { | |
return {name:"shift2bikes"} | |
}) | |
function go(msg) { | |
if (msg.method == "irc.privmsg") { | |
var cmd = /^!shift2bikes(\s+(.*))?/.exec(msg.params.message) | |
if(cmd) { | |
var now = new Date() | |
var rides = shiftGet(now) | |
var ride = soonest(rides) | |
if (ride) { | |
bot.say(msg.params.channel, 'next ride: '+formatTime(ride.dateParsed)+' '+ride.title+' @ '+ride.venue+' '+ride.shareable) | |
} else { | |
bot.say(msg.params.channel, 'no rides left today') | |
} | |
} | |
} | |
} | |
function formatTime(date) { | |
var ap = 'am' | |
var hours = date.getHours() | |
if(hours > 11) { | |
ap = 'pm' | |
hours = hours - 12 | |
} | |
return hours + ':' + date.getMinutes() + ap | |
} | |
function soonest(rides) { | |
var winner | |
var distance | |
var now = new Date() | |
rides.forEach(function(r) { | |
var date = new Date(r.date+'T'+r.time+'-07:00') // PDT | |
r.dateParsed = date | |
var away = (date - now)/1000 | |
if (!distance || (away > 0 && away < distance)) { | |
winner = r | |
distance = away | |
} | |
}) | |
return winner | |
} | |
function shiftGet(date) { | |
var datepart = date.toISOString().slice(0,10) | |
var url = 'https://www.shift2bikes.org/api/events.php?startdate='+datepart+'&enddate='+datepart | |
var data = JSON.parse(http.get(url)) | |
return data.events | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment