Skip to content
Snippets Groups Projects
Commit d3cc2183 authored by echicken's avatar echicken
Browse files

Overhaul of nick/location update process.

parent 612831da
No related branches found
No related tags found
No related merge requests found
Bot_Commands["WEATHER"] = new Bot_Command(0,false,false);
Bot_Commands["WEATHER"].command = function (target,onick,ouh,srv,lvl,cmd) {
Bot_Commands["WEATHER"].command = function (target, onick, ouh, srv, lvl, cmd) {
var i;
var lstr;
var nick = onick;
var res;
var ll;
// Remove empty cmd args
for (i = 1; i < cmd.length; i++) {
// Remove empty cmd args
for (var i = 1; i < cmd.length; i++) {
if (cmd[i].search(/^\s*$/) == 0) {
cmd.splice(i, 1);
i--;
}
}
cmd.shift();
if (cmd[0]) nick = cmd[0];
if (typeof srv.users[nick.toUpperCase()] != 'undefined') {
try {
lstr = get_nickcoords(srv.users[nick.toUpperCase()].uh, srv.users[nick.toUpperCase()].servername, nick);
ll = lstr.match(/^(-*\d+\.\d+),(-*\d+\.\d+)$/);
} catch (e) {
log(LOG_DEBUG, 'Failed to get nick coordinates: ' + e);
}
}
try {
if (ll) {
res = owm.call_api('weather', { lat: ll[1], lon: ll[2], units: 'metric' });
const params = get_params(cmd, onick, srv);
if (params) {
const res = owm.call_api('weather', params);
var str = '\1n\1rWeather for \1h\1c' + res.name + '\1n\1r: '
+ '\1h\1c' + res.weather[0].main + ' \1n\1r(\1c' + res.weather[0].description + '\1r), '
+ '\1h\1c' + res.clouds.all + '% cloudy\1n\1r, '
+ 'Current temp: \1h\1c' + temperature_str(res.main.temp) + '\1n\1r, '
+ 'Min temp: \1h\1c' + temperature_str(res.main.temp_min) + '\1n\1r, '
+ 'Max temp: \1h\1c' + temperature_str(res.main.temp_max) + '\1n\1r, '
+ 'Wind: \1h\1c' + res.wind.speed + ' KM/h ' + owm.wind_direction(res.wind.deg) + '\1n\1r, '
+ 'Humidity: \1h\1c' + res.main.humidity + '%\1n\1r, '
+ 'Pressure: \1h\1c' + res.main.pressure + ' hPa\1n\1r, '
+ 'Sunrise: \1h\1c' + system.timestr(res.sys.sunrise) + '\1n\1r, '
+ 'Sunset: \1h\1c' + system.timestr(res.sys.sunset)
+ ' \1n\1m(\1h\1mProvided by OpenWeatherMap.org\1n\1m)';
srv.o(target, ctrl_a_to_mirc(str));
} else {
var usr = new User(system.matchuser(nick));
if (typeof usr == 'object' && usr.number > 0) {
lstr = usr.location.split(',')[0];
} else {
lstr = nick; // Could be city name
}
usr = undefined;
res = owm.call_api('weather', { q: lstr, units: 'metric' });
}
var str = 'Weather for ' + res.name + ': '
+ res.weather[0].main + ' (' + res.weather[0].description + '), '
+ res.clouds.all + '% cloudy, '
+ 'Current temp: ' + temperature_str(res.main.temp) + ', '
+ 'Min temp: ' + temperature_str(res.main.temp_min) + ', '
+ 'Max temp: ' + temperature_str(res.main.temp_max) + ', '
+ 'Wind: ' + res.wind.speed + ' KM/h ' + owm.wind_direction(res.wind.deg) + ', '
+ 'Humidity: ' + res.main.humidity + '%, '
+ 'Pressure: ' + res.main.pressure + ' hPa, '
+ 'Sunrise: ' + system.timestr(res.sys.sunrise) + ', '
+ 'Sunset: ' + system.timestr(res.sys.sunset);
srv.o(target, str);
} catch (err) {
log(LOG_DEBUG, 'Failed to display weather conditions for ' + lstr + ': ' + err);
srv.o(target, 'Failed to fetch weather conditions.');
log(LOG_DEBUG, 'Failed to display weather conditions: ' + err);
srv.o(target, 'Failed to fetch weather conditions: ' + err);
}
return true;
......
if(js.global.get_geoip==undefined)
js.global.load(js.global, "geoip.js");
if(js.global.get_nicklocation==undefined)
js.global.load(js.global, "nicklocate.js");
if (!js.global.OpenWeatherMap) {
js.global.load(js.global, 'openweathermap.js');
}
if (!js.global.get_geoip) js.global.load(js.global, "geoip.js");
if (!js.global.get_nicklocation) js.global.load(js.global, "nicklocate.js");
if (!js.global.OpenWeatherMap) js.global.load(js.global, 'openweathermap.js');
const owm = new OpenWeatherMap();
function get_nickcoords(uh, sn, n) {
var geo=get_nicklocation(uh, sn, n);
if(!geo)
geo = {latitude:0, longitude:0};
var ret=geo.latitude+','+geo.longitude;
if(ret=='0,0') {
uh=sn;
geo=get_geoip(uh);
ret=geo.latitude+','+geo.longitude;
}
return ret;
function locate_user(nick, srv) {
const ret = { units: 'metric' };
// If we have data about this IRC user
if (typeof srv.users[nick.toUpperCase()] != 'undefined') {
// Try geolocation
var res = get_nicklocation(
srv.users[nick.toUpperCase()].uh,
srv.users[nick.toUpperCase()].servername,
nick
);
// If we have coordinates
if (res && (res.latitude != 0 || res.longitude != 0)) {
ret.lat = res.latitude;
ret.lon = res.longitude;
return ret;
}
}
// See if the user exists in the local DB
var usr = new User(system.matchuser(nick));
if (typeof usr == 'object' && usr.number > 0) {
var loc = usr.location.split(',')[0];
// Assume loc is a city name
if (loc != '') {
ret.q = loc;
return ret;
}
}
return; // We got nothin'
}
function get_params(cmd, nick, srv) {
const ret = { units: 'metric' };
// No parameters given, try to look up the user who issued the command
if (cmd.length < 1) return locate_user(nick, srv);
// Parameters were given
if (cmd.length == 1) {
// This could be a nick
var res = locate_user(cmd[0], srv);
if (res) return res;
// If it's a ZIP code
if (cmd[0].search(/^[0-9]{5}(?:-[0-9]{4})?$/) > -1) {
ret.zip = cmd[0];
ret.us = '';
return ret;
// If it's a Canadian postal code
} else if (cmd[0].search(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/) > -1) {
ret.zip = cmd[0];
ret.ca = '';
return ret;
}
}
// Maybe a city name
return { q : cmd.join(' ').split(',')[0] };
}
function temperature_str(n) {
return n + '\xB0C (' + owm.c_to_f(n) + '\xB0F)';
return n + '\xB0C \1n\1r(\1h\1c' + owm.c_to_f(n) + '\xB0F\1n\1r)';
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment