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 currencies = {
@ -38,9 +38,9 @@ const cryptowat_summary = (crypto, market, currency) => {
return new Promise((resolve, reject) => {
if (!Object.keys(currencies).includes(currency) || crypto === 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 => {
console.log(res);
if (res.length === 0)
reject("No data received");
const data = {
@ -51,9 +51,6 @@ const cryptowat_summary = (crypto, market, currency) => {
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]`);
})
.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";
@ -7,10 +7,12 @@ export default bot => {
call: /^(\.|\/)kernel/i,
set: "nxy",
f: e => {
rp(feed).then(content => {
const releases = JSON.parse(content).releases;
e.reply(releases.map(entry => `[b]${entry.version}[/b] (${entry.moniker}${entry.iseol ? `, [i]EOL[/i]` : ""})`).join(", "));
});
fetch(feed)
.then(res => res.json())
.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 fetch from "../fetch";
export default bot => {
bot._trigger.set("lastfm", new bot.trigger({
@ -12,7 +12,8 @@ export default bot => {
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 nick = e.args[0] || e.user.nick;
rp(`${api}${nick}`, { json: true })
fetch(`${api}${nick}`)
.then(res => res.json())
.then(res => {
if(res.error)
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]` );
else
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";
export default bot => {
@ -10,14 +10,12 @@ export default bot => {
usage: "[b].scrnd[/b]"
},
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 => {
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]`);
})
.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"
@ -16,15 +16,16 @@ export default bot => {
index = parseInt(e.args.pop());
const term = e.args.join(" ").trim().toLowerCase();
rp(`${url}?term=${term}`, { json: true }).then(data => {
if(data.result_type === "no_results")
return e.reply("Term not found");
if(!data.list[index-1])
return e.reply("Index not found");
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, "")}`);
})
fetch(`${url}?term=${term}`)
.then(res => res.json())
.then(data => {
if(data.result_type === "no_results")
return e.reply("Term not found");
if(!data.list[index-1])
return e.reply("Index not found");
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 rp from "request-promise-native";
import fetch from "../fetch";
const data = {
yiff: [],
@ -239,7 +239,7 @@ export default bot => {
set: "nxy",
f: e => {
const addr = !e.args[0].match(/^https?/g) ? `https://${e.args[0]}` : e.args[0];
rp(addr, { timeout: 2000 })
fetch(addr)
.then(res => {
e.reply(`[b]${addr}[/b] seems to be [b]up[/b].`);
})

View File

@ -1,5 +1,5 @@
import sql from "../sql";
import rp from "request-promise-native";
import fetch from "../fetch";
const data = {
abschieben: [],
@ -288,7 +288,8 @@ export default bot => {
call: /^(\.|\/)witz$/i,
set: "uwe",
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 => {
res = res
.split("<br />").join("")
@ -311,10 +312,7 @@ export default bot => {
}
else
e.reply(res);
})
.catch(err => {
console.log(err);
});
}).catch(err => console.log(err));
}
}));
@ -322,10 +320,10 @@ export default bot => {
call: /^(\.|\/)joke$/i,
set: "uwe",
f: e => {
rp("https://icanhazdadjoke.com/slack", { json: true })
fetch("https://icanhazdadjoke.com/slack")
.then(res => res.json())
.then(res => {
res = res.attachments[0].text;
if(e.network !== "Telegram") {
res
.match(/.{1,450}/g)
@ -333,10 +331,7 @@ export default bot => {
}
else
e.reply(res);
})
.catch(err => {
console.log(err);
});
}).catch(err => console.log(err));
}
}));
@ -376,16 +371,14 @@ export default bot => {
set: "uwe",
f: e => {
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 => {
if(data.err)
e.reply("Channel nicht gefunden D:");
else
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 => {
bot._trigger.set("wttr", new bot.trigger({
@ -12,54 +12,52 @@ export default bot => {
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);
});
fetch(`http://wttr.in/${encodeURIComponent(args)}`, options)
.then(res => res.text())
.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));
}
}));
@ -75,23 +73,22 @@ export default bot => {
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);
});
fetch(url)
.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));
}
}));
};