Skip to content

Instantly share code, notes, and snippets.

@donpdonp

donpdonp/weather.js

Last active Oct 13, 2020
Embed
What would you like to do?
gluon weather rain
(function() {
// descriptor
setup()
return {name:"weather"}
})
var apikey_db = 'climacell'
var apikey
function setup(){
db.get(apikey_db, function(key){
apikey = key
bot.say(bot.admin_channel, "weather climacell 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 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
You can’t perform that action at this time.