Uwev2/src/inc/trigger/wttr.mjs
2017-12-04 17:40:24 +01:00

95 lines
3.9 KiB
JavaScript

import rp from "request-promise";
export default bot => {
bot._trigger.set("wttr", new bot.trigger({
call: /^\.wttr .*/i,
help: {
text: "Gets the weather from wttr.in",
usage: "[b].wttr[/b] [i]<location>[/i]"
},
clients: ["irc"],
f: e => {
let args = e.message.trim().substring(6);
let options = {
url: `http://wttr.in/${encodeURIComponent(args)}`,
headers: {
'User-Agent': 'curl/7.43.0',
'accept-language': 'de-DE,de'
}
};
rp(options).then(body => {
let origbody = body;
body = body
.split("\u001b[38;5;226m").join("\x0308") // yellowlight
.split("\u001b[38;5;154m").join("\x0303") // green
.split("\u001b[38;5;118m").join("\x0303") // green
.split("\u001b[38;5;190m").join("\x0309") // lime
.split("\u001b[38;5;046m").join("\x0309") // lime
.split("\u001b[38;5;048m").join("\x0309") // lime
.split("\u001b[38;5;082m").join("\x0309") // lime
.split("\u001b[38;5;047m").join("\x0309") // lime
.split("\u001b[38;5;202m").join("\x0305") // red
.split("\u001b[38;5;196m").join("\x0305") // red
.split("\u001b[38;5;220m").join("\x0304") // orange to brown
.split("\u001b[38;5;214m").join("\x0304") // orange to brown
.split("\u001b[38;5;208m").join("\x0304") // orange to brown
.split("\u001b[38;5;240;1m").join("\x0314") // darkgrey
.split("\u001b[38;5;21;1m").join("\x0302") // blue
.split("\u001b[38;5;21;25m").join("\x0302") // blue
.split("\u001b[38;5;111;25m").join("\x0311") // lightblue
.split("\u001b[38;5;111m").join("\x0311") // lightblue
.split("\u001b[38;5;251m").join("\x0315") // lightgrey
.split("\u001b[38;5;021m").join("\x0302") // arschkalt
.split("\u001b[38;5;051m").join("\x0302") // arschkalt
.split("\u001b[38;5;049m").join("\x0302") // arschkalt
.split("\u001b[38;5;255;1m").join("\x0300") // white
.split("\u001b[38;5;250m").join("\x0F") // yellow to white
.split("\u001b[1m").join("\x0F") // normalize
.split("\u001b[0m").join("\x0F") // normalize
.split("\u000f").join("\x0F") // normalize
.split("\u001b[38;5;228;5m").join("\x0F") // normalize
.split("\u26a1").join(" ") // fick emoji
.split("\n")
if (origbody.match(/ERROR.*(location|Unbekannter)/i))
return e.reply(body[0].replace("ERROR: ", ""));
if (args.trim().match(/^moon/i))
return e.reply("ob du behindert bist.");
[body[0], body[2], body[3], body[4], body[5], body[6]].map(e.reply);
})
.catch(err => {
console.log(err);
});
}
}));
bot._trigger.set("weather", new bot.trigger({
call: /^(\.|\/)weather .*/i,
help: {
text: "Gets the weather from Yahoo weather API",
usage: "[b].weather[/b] [i]<location>[/i]"
},
f: e => {
const loc = e.message.trim().substring(9);
const url = `https://query.yahooapis.com/v1/public/yql?format=json&q=`
+ `select * from weather.forecast where u="c" and woeid in`
+ `(select woeid from geo.places(1) where text="${encodeURIComponent(loc)}")`;
rp(url, { json: true }).then(data => {
if(!data.query.results)
return e.reply("Location not found");
const res = data.query.results.channel;
const location = res.location;
const condition = res.item.condition;
const units = res.units;
const wind = res.wind;
e.reply(
`${location.city}, ${location.region.trim()}, ${location.country}: `
+ `${condition.temp}°${units.temperature} ${condition.text}, `
+ `${[...'↑↗→↘↓↙←↖'][-~(parseInt(wind.direction) / 45) % 8]} ${wind.speed} ${units.speed}`
);
})
.catch(err => {
console.log(err);
});
}
}));
};