Uwev2/src/inc/trigger/pr0gag.mjs
2019-04-24 15:26:39 +02:00

77 lines
2.4 KiB
JavaScript

import fetch from "../fetch";
import url from "url";
const apis = {
reverse: "https://pr0.totally.rip/api/v2/item?file=",
pr0gag: {
list: "https://pr0gramm.com/api/items/get/?flags=1&id=", // 15
item: "https://pr0gramm.com/api/items/info?flags=1&itemId="
}
};
const flags = {
1: "[color=green]sfw[/color]",
2: "[color=brown]nsfw[/color]",
4: "[color=red]nsfl[/color]",
8: "[color=blue]nsfp[/color]"
};
const regex = /(full|img|vid)\.pr0gramm\.com\/((?:\d+){4}\/(?:\d+){2}(\/(?:\d+){2})?\/(?:\w{1,20})\.(?:gif|png|jpg|mp4)?)/gi;
const site = "https://pr0gramm.com/new/";
export default bot => {
bot._trigger.set("pr0gag", new bot.trigger({
call: /(vid|img)\.pr0gramm\.com.*/i,
set: "uwe",
f: e => {
const link = "https://" + e.message.match(regex)[0];
const tmp = url.parse(link).path.substr(1);
fetch(apis.reverse + tmp)
.then(res => res.json())
.then(res => {
if(res.error)
throw res.error;
return res.data.id;
})
.then(id => Promise.all([
fetch(apis.pr0gag.list + id.toString()),
fetch(apis.pr0gag.item + id.toString()),
id
])
.then(([list, item, id]) => Promise.all([
list.json(),
item.json(),
id
]))
.then(([list, item, id]) => [
list.items.filter(item => item.id === id)[0],
item,
id
])
.then(([list, item, id]) => ({
created: list.created,
up: list.up,
down: list.down,
score: list.up - list.down,
ratio: (list.up / (list.up + list.down)).toFixed(2),
flag: flags[list.flags],
user: list.user,
comments: item.comments.length,
toptags: item.tags
.sort((a, b) => a.confidence - b.confidence)
.reverse()
.splice(0, 3)
.map(tag => tag.tag),
link: site + id.toString()
}))
.then(meta => [
meta.link,
`${meta.score} ([color=green]${meta.up}[/color] / [color=red]${meta.down}[/color])`,
`user: ${meta.user}`,
`comments: ${meta.comments}`,
`toptags: ${meta.toptags.join(", ")} (${meta.flag})`
])
.then(out => e.reply(out.join(" - ")))
).catch(err => e.reply(JSON.stringify(err)));
}
}));
};