pushi push lul

modified:   src/inc/trigger/pr0gag.mjs
This commit is contained in:
Flummi 2019-08-17 04:05:00 +00:00
parent 9ee9f87f58
commit 05d42650ec
2 changed files with 80 additions and 66 deletions

View File

@ -10,10 +10,10 @@
"author": "Flummi & jkhsjdhjs",
"license": "WTFPL",
"dependencies": {
"flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies.git",
"flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies",
"long-timeout": "^0.1.1",
"pg-promise": "^7.3.2",
"stringify-object": "^3.2.1",
"stringify-object": "^3.3.0",
"winston": "^2.4.0"
}
}

View File

@ -1,76 +1,90 @@
import fetch from "flumm-fetch-cookies";
import url from "url";
import {default as fetch, cookieJar, Cookie} from "flumm-fetch-cookies";
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="
}
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[/color]",
2: "[color=brown]nsfw[/color]",
4: "[color=red]nsfl[/color]",
8: "[color=blue]nsfp[/color]"
1: color.green("sfw"),
2: color.brown("nsfw"),
4: color.red("nsfl"),
8: color.blue("nsfp")
};
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/";
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
};
/*fetch("https://pr0gramm.com/api/user/login", {
method: "POST",
body: {
username: "",
password: ""
}
}).then(res => {
console.log("pr0gag: " + (res.body.statusCode === 200 ? "login successful" : "login failed. http status " + res.body.statusCode));
return res.json();
}).then(console.log);*/
export default bot => {
bot._trigger.set("pr0gag", new bot.trigger({
call: /(full|vid|img)\.pr0gramm\.com.*/i,
call: /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)));
f: async e => {
let reply;
try {
let matches, id;
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();
console.log(response);
if(response.error || !response.items.length)
throw "reverse lookup error";
id = response.items[0].id;
}
else if(matches = e.message.match(regex.normal))
id = matches[1];
else
return;
let [list, info] = await Promise.all([
fetch(apis.list + id),
fetch(apis.item + id)
].map(async request => (await request).json()));
list = list.items.filter(item => item.id == id)[0]; // ja, == ist hier ein muss!
if(!list || !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(", ");
reply = [
"https://pr0gramm.com/" + (list.promoted ? "top" : "new") + "/" + id,
`${list.up - list.down} (${color.green(list.up)} / ${color.red(list.down)})`,
"user: " + list.user,
"comments: " + info.comments.length,
`toptags: ${toptags} (${flags[list.flags]})`
].join(" - ");
}
catch(error) {
reply = typeof error === "string" ? error : JSON.stringify(error);
}
finally {
reply && e.reply(reply);
}
}
}));
};