Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active March 28, 2021 01:40
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/17fc248434a9064b56f4e6b44cc2aecf to your computer and use it in GitHub Desktop.
Save donpdonp/17fc248434a9064b56f4e6b44cc2aecf to your computer and use it in GitHub Desktop.
gluon launch spacex
(function() {
return {name:"launch"}
})
var alert_channel = "#pdxtech"
function go(msg) {
var privmsg = false
var clocktower = false
if (msg.method == "clocktower" && (new Date(Date.parse(msg.params.time))).getMinutes() == 30) {
var launches = launch_reports();
var close = launches.filter(function(launch) { return launch.win_open_ago < 30 })
if (close.len > 0) {
close.forEach(function(launch) {
bot.say(alert_channel, launch_to_words(launch))
})
}
}
if (msg.method == "irc.privmsg") {
var match = /^!?launch(\s+(\w+))?$/.exec(msg.params.message)
if(match) {
var launches = launch_reports();
if (match[2]) {
var provider = match[2].toLowerCase()
launches = launches.filter(function(launch) { return launch.provider.slug == provider })
}
var words = launches.map(launch_to_words).join(', ')
bot.say(msg.params.channel, words)
}
}
}
function launch_to_words(launch){
words = [launch.provider.name, launch.name, launch.win_open]
//+ " " + launch.pad.location.name + " " + launch.vehicle.name
if (launch.win_open_ago) {
words.push("[window open in " + minutes_to_words(launch.win_open_ago) + "]")
} else {
words.push("[no window date]")
}
return words.join(' ')
}
function launch_reports() {
var url = "https://fdo.rocketlaunch.live/json/launches/next/5"
var json = http.get(url)
var data = JSON.parse(json)
return data.result.map(function(launch){
var win_open_date = Date.parse(launch.win_open)
if (win_open_date) {
launch.win_open_ago = age_minutes(win_open_date, new Date())
}
return launch
})
}
function age_minutes(a,b) {
return ((new Date(a)).getTime() - (new Date(b)).getTime())/1000/60
}
function minutes_to_words(mins) {
if (mins < 1) {
return (mins*60).toFixed(0)+" secs"
} else if (mins < 60) {
return mins.toFixed(1)+" mins"
} else if (mins < 60*48) {
return (mins/60).toFixed(1)+" hours"
} else {
return (mins/60/24).toFixed(1)+" days"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment