Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active May 12, 2023 17:59
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/5933f0e000e035f606b867dc2a8fb19b to your computer and use it in GitHub Desktop.
Save donpdonp/5933f0e000e035f606b867dc2a8fb19b to your computer and use it in GitHub Desktop.
gluon weather rain
(function() {
// descriptor
setup()
return {name:"weather"}
})
var apikey_climacell
var apikey_pirateweather
function setup(){
db.get('climacell', function(key){
apikey_climacell = key
bot.say(bot.admin_channel, "weather climacell key loaded")
})
db.get('pirateweather', function(key){
apikey_pirateweather = key
bot.say(bot.admin_channel, "weather pirateweather key loaded")
})
}
function go(msg){
if(msg.method == "irc.privmsg") {
if(msg.params.nick == bot.owner) {
var match = /!rain(\s+(.*))?/.exec(msg.params.message)
if(match){
var lat, lng
if (match[1]) {
var ll = geocode(match[2])
lat = parseFloat(ll.lat)
lng = parseFloat(ll.lon)
} else {
lat = 45.5
lng = -122.6
}
var wdata = weather(lat, lng)
var report = precip_format(wdata)
bot.say(msg.params.channel, lat.toFixed(2)+","+lng.toFixed(2)+" next hour: "+report)
}
}
}
}
function geocode(street){
bot.say(bot.admin_channel, "weather geocode "+street)
var url = 'https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(street)
var data = JSON.parse(http.get(url))
return data[0]
}
function precip_format(report){
var units
var parts = report.map(function(r){
units = r.precipitation.units
//return parseFloat(r.precipitation.value).toFixed(2)
return r.precipitation.value
})
return parts.slice(0,11).join(',')+" "+units
}
function weather(lat,lng){
bot.say(bot.admin_channel, "weather climacell "+lat+","+lng)
//var url = "https://api.climacell.co/v3/weather/nowcast?unit_system=us&timestep=5&start_time=now&"+
// "fields=precipitation&lat="+lat+"&lon="+lng+"&apikey="+apikey
var url = 'https://api.tomorrow.io/v4/timelines?location='+lat+','+lng+
'&fields=temperature&timesteps=1h&units=metric&apikey='+apikey
bot.say(bot.admin_channel, url)
var json = JSON.parse(http.get(url))
return json
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment