rp -> fetch (alles andere)

This commit is contained in:
Flummi 2018-09-14 21:55:13 +02:00
parent 56a078ec02
commit 7b5ae1ecbf
8 changed files with 100 additions and 114 deletions

View File

@ -1,4 +1,4 @@
import rp from "request-promise-native"; import fetch from "../fetch";
const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`; const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`;
const currencies = { const currencies = {
@ -38,9 +38,9 @@ const cryptowat_summary = (crypto, market, currency) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!Object.keys(currencies).includes(currency) || crypto === currency) if (!Object.keys(currencies).includes(currency) || crypto === currency)
reject(`Can't convert or invalid currency: ${currency}`); reject(`Can't convert or invalid currency: ${currency}`);
rp([{ market: market, crypto: crypto, currency: currency }].map(api_url).join``, { json: true }) fetch([{ market: market, crypto: crypto, currency: currency }].map(api_url).join``)
.then(res => res.json())
.then(res => { .then(res => {
console.log(res);
if (res.length === 0) if (res.length === 0)
reject("No data received"); reject("No data received");
const data = { const data = {
@ -51,9 +51,6 @@ const cryptowat_summary = (crypto, market, currency) => {
volume: res.result.volume volume: res.result.volume
}; };
resolve(`Current: [b]${data.last}[/b] - High: [b]${data.high}[/b] - Low: [b]${data.low}[/b] - Change: [b]${data.change}[/b]% - Volume: [b]${data.volume}[/b]`); resolve(`Current: [b]${data.last}[/b] - High: [b]${data.high}[/b] - Low: [b]${data.low}[/b] - Change: [b]${data.change}[/b]% - Volume: [b]${data.volume}[/b]`);
}) }).catch(err => reject("lol cryptowatch ist down"));
.catch(err => {
reject("lol cryptowatch ist down");
});
}); });
}; };

View File

@ -1,4 +1,4 @@
import rp from "request-promise-native"; import fetch from "../fetch";
const feed = "https://www.kernel.org/releases.json"; const feed = "https://www.kernel.org/releases.json";
@ -7,10 +7,12 @@ export default bot => {
call: /^(\.|\/)kernel/i, call: /^(\.|\/)kernel/i,
set: "nxy", set: "nxy",
f: e => { f: e => {
rp(feed).then(content => { fetch(feed)
const releases = JSON.parse(content).releases; .then(res => res.json())
e.reply(releases.map(entry => `[b]${entry.version}[/b] (${entry.moniker}${entry.iseol ? `, [i]EOL[/i]` : ""})`).join(", ")); .then(content => {
}); const releases = content.releases;
e.reply(releases.map(entry => `[b]${entry.version}[/b] (${entry.moniker}${entry.iseol ? `, [i]EOL[/i]` : ""})`).join(", "));
}).catch(err => console.log(err));
} }
})); }));
} }

View File

@ -1,5 +1,5 @@
import rp from "request-promise-native";
import { cfg } from "../../inc/cfg"; import { cfg } from "../../inc/cfg";
import fetch from "../fetch";
export default bot => { export default bot => {
bot._trigger.set("lastfm", new bot.trigger({ bot._trigger.set("lastfm", new bot.trigger({
@ -12,7 +12,8 @@ export default bot => {
f: e => { f: e => {
const api = `http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&api_key=${cfg.main.lastfm.val.key}&format=json&user=`; const api = `http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&api_key=${cfg.main.lastfm.val.key}&format=json&user=`;
const nick = e.args[0] || e.user.nick; const nick = e.args[0] || e.user.nick;
rp(`${api}${nick}`, { json: true }) fetch(`${api}${nick}`)
.then(res => res.json())
.then(res => { .then(res => {
if(res.error) if(res.error)
return e.reply("User not found"); return e.reply("User not found");
@ -28,10 +29,7 @@ export default bot => {
e.reply( `[b]${info.user}[/b] is listening to [b]${info.track}[/b]` ); e.reply( `[b]${info.user}[/b] is listening to [b]${info.track}[/b]` );
else else
e.reply( `[b]${info.user}[/b] is not listening to anything. They last listened to [b]${info.track}[/b]` ); e.reply( `[b]${info.user}[/b] is not listening to anything. They last listened to [b]${info.track}[/b]` );
}) }).catch(err => console.log(err));
.catch(err => {
console.log(err);
});
} }
})); }));
}; };

View File

@ -1,4 +1,4 @@
import rp from "request-promise-native"; import fetch from "../fetch";
import { cfg } from "../../inc/cfg"; import { cfg } from "../../inc/cfg";
export default bot => { export default bot => {
@ -10,14 +10,12 @@ export default bot => {
usage: "[b].scrnd[/b]" usage: "[b].scrnd[/b]"
}, },
f: e => { f: e => {
rp(`http://api.soundcloud.com/users/${cfg.main.soundcloud.val.user}/favorites?client_id=${cfg.main.soundcloud.val.clientid}`, { json: true }) fetch(`http://api.soundcloud.com/users/${cfg.main.soundcloud.val.user}/favorites?client_id=${cfg.main.soundcloud.val.clientid}`)
.then(res => res.json())
.then(res => { .then(res => {
const track = res[~~((Math.random() * res.length) + 1)]; const track = res[~~((Math.random() * res.length) + 1)];
e.reply(`${track.permalink_url}\n[b]${track.title}[/b] - length [b]${track.duration}[/b] - [b]${track.user.username}[/b] on [b]${track.created_at}[/b]`); e.reply(`${track.permalink_url}\n[b]${track.title}[/b] - length [b]${track.duration}[/b] - [b]${track.user.username}[/b] on [b]${track.created_at}[/b]`);
}) }).catch(err => console.log(err));
.catch(err => {
console.log(err);
});
} }
})); }));
}; };

View File

@ -1,4 +1,4 @@
import rp from "request-promise-native"; import fetch from "../fetch";
const url = "https://api.urbandictionary.com/v0/define" const url = "https://api.urbandictionary.com/v0/define"
@ -16,15 +16,16 @@ export default bot => {
index = parseInt(e.args.pop()); index = parseInt(e.args.pop());
const term = e.args.join(" ").trim().toLowerCase(); const term = e.args.join(" ").trim().toLowerCase();
rp(`${url}?term=${term}`, { json: true }).then(data => { fetch(`${url}?term=${term}`)
if(data.result_type === "no_results") .then(res => res.json())
return e.reply("Term not found"); .then(data => {
if(!data.list[index-1]) if(data.result_type === "no_results")
return e.reply("Index not found"); return e.reply("Term not found");
const res = data.list[index-1]; if(!data.list[index-1])
return e.reply("Index not found");
e.reply(`[${index}/${data.list.length}] [b]${res.word}[/b]: ${res.definition.replace(/\r\n/g, "")} - ${res.example.replace(/\r\n/g, "")}`); const res = data.list[index-1];
}) e.reply(`[${index}/${data.list.length}] [b]${res.word}[/b]: ${res.definition.replace(/\r\n/g, "")} - ${res.example.replace(/\r\n/g, "")}`);
}).catch(err => console.log(err));
} }
})); }));
}; };

View File

@ -1,5 +1,5 @@
import sql from "../sql"; import sql from "../sql";
import rp from "request-promise-native"; import fetch from "../fetch";
const data = { const data = {
yiff: [], yiff: [],
@ -239,7 +239,7 @@ export default bot => {
set: "nxy", set: "nxy",
f: e => { f: e => {
const addr = !e.args[0].match(/^https?/g) ? `https://${e.args[0]}` : e.args[0]; const addr = !e.args[0].match(/^https?/g) ? `https://${e.args[0]}` : e.args[0];
rp(addr, { timeout: 2000 }) fetch(addr)
.then(res => { .then(res => {
e.reply(`[b]${addr}[/b] seems to be [b]up[/b].`); e.reply(`[b]${addr}[/b] seems to be [b]up[/b].`);
}) })

View File

@ -1,5 +1,5 @@
import sql from "../sql"; import sql from "../sql";
import rp from "request-promise-native"; import fetch from "../fetch";
const data = { const data = {
abschieben: [], abschieben: [],
@ -288,7 +288,8 @@ export default bot => {
call: /^(\.|\/)witz$/i, call: /^(\.|\/)witz$/i,
set: "uwe", set: "uwe",
f: e => { f: e => {
rp("http://www.funny4you.at/webmasterprogramm/zufallswitz.php?id=312") fetch("http://www.funny4you.at/webmasterprogramm/zufallswitz.php?id=312")
.then(res => res.text())
.then(res => { .then(res => {
res = res res = res
.split("<br />").join("") .split("<br />").join("")
@ -311,10 +312,7 @@ export default bot => {
} }
else else
e.reply(res); e.reply(res);
}) }).catch(err => console.log(err));
.catch(err => {
console.log(err);
});
} }
})); }));
@ -322,10 +320,10 @@ export default bot => {
call: /^(\.|\/)joke$/i, call: /^(\.|\/)joke$/i,
set: "uwe", set: "uwe",
f: e => { f: e => {
rp("https://icanhazdadjoke.com/slack", { json: true }) fetch("https://icanhazdadjoke.com/slack")
.then(res => res.json())
.then(res => { .then(res => {
res = res.attachments[0].text; res = res.attachments[0].text;
if(e.network !== "Telegram") { if(e.network !== "Telegram") {
res res
.match(/.{1,450}/g) .match(/.{1,450}/g)
@ -333,10 +331,7 @@ export default bot => {
} }
else else
e.reply(res); e.reply(res);
}) }).catch(err => console.log(err));
.catch(err => {
console.log(err);
});
} }
})); }));
@ -376,16 +371,14 @@ export default bot => {
set: "uwe", set: "uwe",
f: e => { f: e => {
const sync = e.args[0] || "80s90s"; const sync = e.args[0] || "80s90s";
rp(`https://f0ck.space/sync.php?${sync}`, { json: true }) fetch(`https://f0ck.space/sync.php?${sync}`)
.then(res => res.json())
.then(data => { .then(data => {
if(data.err) if(data.err)
e.reply("Channel nicht gefunden D:"); e.reply("Channel nicht gefunden D:");
else else
e.reply(`${data.name} @ https://sync.f0ck.space/r/${data.name} : ${data.user} Users, now playing: ${decodeURIComponent(data.title)}`); e.reply(`${data.name} @ https://sync.f0ck.space/r/${data.name} : ${data.user} Users, now playing: ${decodeURIComponent(data.title)}`);
}) }).catch(err => e.reply("Channel nicht gefunden D:"));
.catch(err => {
e.reply("Channel nicht gefunden D:");
});
} }
})); }));

View File

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