Uwev2/src/inc/trigger/pr0gag.mjs
2020-08-18 01:36:03 +02:00

92 lines
2.8 KiB
JavaScript

import {default as fetch, cookieJar, Cookie} from "flumm-fetch-cookies";
import cfg from "../../../cfg/config.json";
const apis = {
reverse: "https://pr0gramm.com/api/items/get?flags=15&tags=!p:",
list: "https://pr0gramm.com/api/items/get?flags=15&id=",
item: "https://pr0gramm.com/api/items/info?flags=15&itemId="
};
const color = new Proxy({}, {
get: (_, prop) => s => `[color=${prop}]${s}[/color]`
});
const flags = {
1: color.green("sfw"),
2: color.magenta("nsfw"),
4: color.red("nsfl"),
8: color.blue("nsfp")
};
const regex = {
normal: /pr0gramm\.com\/.+\/(\d+)(?:\s|$)/i,
direct: /(full|img|vid)\.pr0gramm\.com\/((?:\d+){4}\/(?:\d+){2}(?:\/(?:\d+){2})?\/(?:\w{1,20})\.(gif|png|jpg|mp4))/i
};
cookieJar.addCookie(Cookie.fromObject(cfg.kekse.me));
cookieJar.addCookie(Cookie.fromObject(cfg.kekse.pp));
export default async bot => {
return [{
name: "pr0gag",
call: /pr0gramm\.com\/.+\/.+/i,
set: "uwe",
f: async e => {
let matches, id, link, list;
if(matches = e.message.match(regex.direct)) {
const path = matches[1] === "full" && matches[3] === "png"
? matches[2].slice(0, -3) + "jpg"
: matches[2];
const response = await (await fetch(apis.reverse + path)).json();
if(response.error || !response.items.length)
throw "reverse lookup error";
list = response.items[0];
id = list.id;
link = "https://pr0gramm.com/" + (list.promoted ? "top" : "new") + "/" + id;
}
else if(matches = e.message.match(regex.normal)) {
id = matches[1];
link = "https://" + matches[0];
list = (await (await fetch(apis.list + id)).json())
.items
.filter(item => item.id == id)[0];
}
else
return;
let info = await (await fetch(apis.item + id)).json();
if(!info || !Array.isArray(info.tags) || !Array.isArray(info.comments))
throw "item lookup error";
const toptags = info.tags
.sort((a, b) => b.confidence - a.confidence)
.slice(0, 3)
.map(tag => tag.tag)
.join(", ");
const voteRatio = list.up / (list.up + list.down);
const voteRatioColors = [
"red",
"brown",
"orange",
"yellow",
"green",
"lightgreen",
];
const voteRatioColor = voteRatioColors[~~Math.min(voteRatio * voteRatioColors.length, voteRatioColors.length - 1)];
e.reply([
link,
`${list.up - list.down} (${color.green(list.up)} / ${color.red(list.down)} = ${color[voteRatioColor]((voteRatio * 100).toFixed(1))}${color.magenta("%")})`,
"user: " + list.user,
"comments: " + info.comments.length,
`toptags: ${toptags} (${flags[list.flags]})`
].join(" - "));
}
}];
};