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", "author": "Flummi & jkhsjdhjs",
"license": "WTFPL", "license": "WTFPL",
"dependencies": { "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", "long-timeout": "^0.1.1",
"pg-promise": "^7.3.2", "pg-promise": "^7.3.2",
"stringify-object": "^3.2.1", "stringify-object": "^3.3.0",
"winston": "^2.4.0" "winston": "^2.4.0"
} }
} }

View File

@ -1,76 +1,90 @@
import fetch from "flumm-fetch-cookies"; import {default as fetch, cookieJar, Cookie} from "flumm-fetch-cookies";
import url from "url";
const apis = { const apis = {
reverse: "https://pr0.totally.rip/api/v2/item?file=", reverse: "https://pr0gramm.com/api/items/get?flags=15&tags=!p:",
pr0gag: { list: "https://pr0gramm.com/api/items/get?flags=15&id=",
list: "https://pr0gramm.com/api/items/get/?flags=1&id=", // 15 item: "https://pr0gramm.com/api/items/info?flags=15&itemId="
item: "https://pr0gramm.com/api/items/info?flags=1&itemId="
}
}; };
const color = new Proxy({}, {
get: (_, prop) => s => `[color=${prop}]${s}[/color]`
});
const flags = { const flags = {
1: "[color=green]sfw[/color]", 1: color.green("sfw"),
2: "[color=brown]nsfw[/color]", 2: color.brown("nsfw"),
4: "[color=red]nsfl[/color]", 4: color.red("nsfl"),
8: "[color=blue]nsfp[/color]" 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 => { export default bot => {
bot._trigger.set("pr0gag", new bot.trigger({ bot._trigger.set("pr0gag", new bot.trigger({
call: /(full|vid|img)\.pr0gramm\.com.*/i, call: /pr0gramm\.com\/.+\/.+/i,
set: "uwe", set: "uwe",
f: e => { f: async e => {
const link = "https://" + e.message.match(regex)[0]; let reply;
const tmp = url.parse(link).path.substr(1); try {
fetch(apis.reverse + tmp) let matches, id;
.then(res => res.json()) if(matches = e.message.match(regex.direct)) {
.then(res => { const path = matches[1] === "full" && matches[3] === "png"
if(res.error) ? matches[2].slice(0, -3) + "jpg"
throw res.error; : matches[2];
return res.data.id; const response = await (await fetch(apis.reverse + path)).json();
}) console.log(response);
.then(id => Promise.all([ if(response.error || !response.items.length)
fetch(apis.pr0gag.list + id.toString()), throw "reverse lookup error";
fetch(apis.pr0gag.item + id.toString()), id = response.items[0].id;
id }
]) else if(matches = e.message.match(regex.normal))
.then(([list, item, id]) => Promise.all([ id = matches[1];
list.json(), else
item.json(), return;
id
])) let [list, info] = await Promise.all([
.then(([list, item, id]) => [ fetch(apis.list + id),
list.items.filter(item => item.id === id)[0], fetch(apis.item + id)
item, ].map(async request => (await request).json()));
id
]) list = list.items.filter(item => item.id == id)[0]; // ja, == ist hier ein muss!
.then(([list, item, id]) => ({ if(!list || !info || !Array.isArray(info.tags) || !Array.isArray(info.comments))
created: list.created, throw "item lookup error";
up: list.up,
down: list.down, const toptags = info.tags
score: list.up - list.down, .sort((a, b) => b.confidence - a.confidence)
ratio: (list.up / (list.up + list.down)).toFixed(2), .slice(0, 3)
flag: flags[list.flags], .map(tag => tag.tag)
user: list.user, .join(", ");
comments: item.comments.length,
toptags: item.tags reply = [
.sort((a, b) => a.confidence - b.confidence) "https://pr0gramm.com/" + (list.promoted ? "top" : "new") + "/" + id,
.reverse() `${list.up - list.down} (${color.green(list.up)} / ${color.red(list.down)})`,
.splice(0, 3) "user: " + list.user,
.map(tag => tag.tag), "comments: " + info.comments.length,
link: site + id.toString() `toptags: ${toptags} (${flags[list.flags]})`
})) ].join(" - ");
.then(meta => [ }
meta.link, catch(error) {
`${meta.score} ([color=green]${meta.up}[/color] / [color=red]${meta.down}[/color])`, reply = typeof error === "string" ? error : JSON.stringify(error);
`user: ${meta.user}`, }
`comments: ${meta.comments}`, finally {
`toptags: ${meta.toptags.join(", ")} (${meta.flag})` reply && e.reply(reply);
]) }
.then(out => e.reply(out.join(" - ")))
).catch(err => e.reply(JSON.stringify(err)));
} }
})); }));
}; };