From 3bf47324bf0fe20dc01948011892ad77855e9c19 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 8 Jan 2019 08:04:56 +0100 Subject: [PATCH] wttr --- src/inc/trigger/wttr.mjs | 90 +++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/src/inc/trigger/wttr.mjs b/src/inc/trigger/wttr.mjs index 00b8e97..5154403 100644 --- a/src/inc/trigger/wttr.mjs +++ b/src/inc/trigger/wttr.mjs @@ -64,7 +64,7 @@ export default bot => { } })); - bot._trigger.set("weather", new bot.trigger({ + bot._trigger.set("wttrnew", new bot.trigger({ call: /^(\.|\/)w .*/i, set: "all", help: { @@ -72,7 +72,7 @@ export default bot => { usage: "[b].w[/b] [i][/i]" }, f: e => { - const loc = e.message.trim().substring(3); + const loc = encodeURIComponent(e.message.trim().substring(3)); const key = cfg.main.owm.val.key; const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`; @@ -81,36 +81,70 @@ export default bot => { .then(res => { if(res.cod !== 200) return e.reply(res.message); - const data = [ - res.weather[0].description, - `${res.main.temp} °C`, - `${[..."↓↙←↖↑↗→↘"][-~parseInt((res.wind.deg + 22) % 360 / 45)]} ${res.wind.speed} km/h`, - "", - "" - ]; - e.reply(`Wetterbericht für: ${res.name}, ${res.sys.country}\n${[...Array(5)].map((_, i) => `${conds(res.weather[0].id)[i].padEnd(15)} ${data[i]}`).join("\n")}`); + let data = []; + let icon = ""; + + if(/^bremen/i.test(loc)) { + const temp_min = (res.main.temp_min / Math.PI).toFixed(2); + const temp_max = Math.sqrt(res.main.temp_max).toFixed(2); + data = [ + "Gewitter mit Hagal", + `${temp_min} * π bis ${temp_max}² °C`, + `${[..."↓↙←↖↑↗→↘"][-~parseInt((res.wind.deg + 22) % 360 / 45)] || ""} ${res.wind.speed} km/h`, + "Mathe macht Spaß,", + "wenn's vorbei ist." + ]; + icon = conds(202); + } + else { + data = [ + res.weather[0].description, + `${res.main.temp_min} - ${res.main.temp_max} °C`, + `${[..."↓↙←↖↑↗→↘"][-~parseInt((res.wind.deg + 22) % 360 / 45)] || ""} ${res.wind.speed} km/h`, + "", + "" + ]; + icon = conds(res.weather[0].id); + } + + e.reply(`Wetterbericht für: ${res.name}, ${res.sys.country}\n${[...Array(5)].map((_, i) => `${icon[i].padEnd(15)} ${data[i]}`).join("\n")}`); }).catch(err => console.log(err)); + } + })); + bot._trigger.set("weather", new bot.trigger({ + call: /^(\.|\/)weather .*/i, + set: "all", + help: { + text: "Gets the weather from OWM", + usage: "[b].weather[/b] [i][/i]" + }, + f: e => { + const loc = encodeURIComponent(e.message.trim().substring(9)); + const key = cfg.main.owm.val.key; + const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`; - /*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)}")`; - fetch(url) + fetch(api) .then(res => res.json()) - .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));*/ + .then(res => { + if(res.cod !== 200) + return e.reply(res.message); + if(/^bremen/i.test(loc)) { + const temp = Math.sqrt(res.main.temp).toFixed(2); + e.reply( + `Bremen, DE: ` + + `${temp}² °C Schwere Gewitter mit Hagal und Sturzfluten, ` + + `${[..."↓↙←↖↑↗→↘"][-~parseInt((res.wind.deg + 22) % 360 / 45)] || ""} ${res.wind.speed} km/h` + ); + } + else { + e.reply( + `${res.name}, ${res.sys.country}: ` + + `${res.main.temp}°C ${res.weather[0].description}, ` + + `${[..."↓↙←↖↑↗→↘"][-~parseInt((res.wind.deg + 22) % 360 / 45)] || ""} ${res.wind.speed} km/h` + ); + } + }).catch(err => e.reply(err)); } })); };