irgendwas mit wttr

This commit is contained in:
Flummi 2019-01-07 13:19:26 +00:00
parent 10786ff297
commit 8e2489fde5
2 changed files with 51 additions and 6 deletions

View File

@ -0,0 +1,24 @@
export const conds = code => {
let out = [];
switch(code) {
case 200: case 201: case 210: case 230: case 231: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " ⚡ʻ ʻ⚡ʻ ʻ ", " ʻ ʻ ʻ ʻ"]; break; // ThunderyShowers
case 202: case 211: case 212: case 221: case 232: out = [ " .-.", " ( ).", " (___(__)", " ‚ʻ⚡ʻ‚⚡‚ʻ", " ‚ʻ‚ʻ⚡ʻ‚ʻ" ]; break; // ThunderyHeavyRain
case 300: case 301: case 310: case 311: case 313: case 321: out = [ " .-.", " ( ).", " (___(__)", " ʻ ʻ ʻ ʻ", " ʻ ʻ ʻ ʻ" ]; break; // LightRain
case 302: case 312: case 314: out = [ " .-.", " ( ).", " (___(__)", " ʻʻʻʻ", " ʻʻʻʻ" ]; break; // HeavyRain
case 500: case 501: case 520: case 521: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " ʻ ʻ ʻ ʻ", " ʻ ʻ ʻ ʻ" ]; break; // LightShowers
case 502: case 503: case 504: case 522: case 531: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " ʻʻʻʻ", " ʻʻʻʻ" ]; break; // HeavyShowers
case 511: case 611: case 615: case 616: out = [ " .-.", " ( ).", " (___(__)", " ʻ * ʻ *", " * ʻ * ʻ" ]; break; // LightSleet
case 600: case 601: out = [ " .-.", " ( ).", " (___(__)", " * * *", " * * *" ]; break; // LightSnow
case 602: out = [ " .-.", " ( ).", " (___(__)", " * * * *", " * * * * " ]; break; // HeavySnow
case 612: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " ʻ * ʻ *", " * ʻ * ʻ" ]; break; // LightSleetShowers
case 620: case 621: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " * * *", " * * *" ]; break; // LightSnowShowers
case 622: out = [ " _`/\"\".-.", " ,\\_( ).", " /(___(__)", " * * * *", " * * * *" ]; break; // HeavySnowShowers
case 701: case 711: case 721: case 741: out = ["", " _ - _ - _ -", " _ - _ - _ ", " _ - _ - _ -", ""]; break; // Fog
case 800: out = [ " \\ /", " .-.", " ( ) ", " `-᾿", " / \\" ]; break; // Sunny
case 801: out = [ " \\ /", " _ /\"\".-.", " \\_( ).", " /(___(__)", "" ]; break; // PartlyCloudy
case 802: out = [ "", " .--.", " .-( ).", " (___.__)__)", "" ]; break; // Cloudy
case 803: case 804: out = [ "", " .--.", " .-( ).", " (___.__)__)", "" ]; break; // VeryCloudy
default: out = [" .-.", " __)", " (", " `-᾿", " •"]; break; // not found
}
return out;
};

View File

@ -1,4 +1,6 @@
import fetch from "../fetch";
import { cfg } from "../../inc/cfg";
import { conds } from "./inc/wttr";
export default bot => {
bot._trigger.set("wttr", new bot.trigger({
@ -63,15 +65,34 @@ export default bot => {
}));
bot._trigger.set("weather", new bot.trigger({
call: /^(\.|\/)weather .*/i,
set: "nxy",
call: /^(\.|\/)w .*/i,
set: "all",
help: {
text: "Gets the weather from Yahoo weather API",
usage: "[b].weather[/b] [i]<location>[/i]"
text: "Gets the weather from OWM",
usage: "[b].w[/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=`
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`;
fetch(api)
.then(res => res.json())
.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")}`);
}).catch(err => console.log(err));
/*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)
@ -89,7 +110,7 @@ export default bot => {
+ `${condition.temp}°${units.temperature} ${condition.text}, `
+ `${[...'↑↗→↘↓↙←↖'][-~(parseInt(wind.direction) / 45) % 8]} ${wind.speed} ${units.speed}`
);
}).catch(err => console.log(err));
}).catch(err => console.log(err));*/
}
}));
};