f0ckv2/src/inc/trigger/parser.mjs

229 lines
8.7 KiB
JavaScript
Raw Normal View History

2021-12-04 11:19:47 +00:00
import cfg from "../config.mjs";
import db from "../sql.mjs";
2020-04-09 16:44:09 +00:00
import lib from "../lib.mjs";
2020-10-15 11:02:56 +00:00
import { getLevel } from "../admin.mjs";
2023-05-01 12:10:11 +00:00
import queue from "../queue.mjs";
2023-05-02 00:06:40 +00:00
import autotagger from "../autotagger.mjs";
2022-03-31 17:53:44 +00:00
import fetch from "flumm-fetch";
2020-04-03 11:12:35 +00:00
import fs from "fs";
2023-05-01 12:10:11 +00:00
import path from "path";
2020-04-03 11:12:35 +00:00
2023-05-01 12:10:11 +00:00
const regex = {
all: /https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/gi,
yt: /(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\/?\?(?:\S*?&?v\=))|youtu\.be\/)([a-zA-Z0-9_-]{6,11})/gi,
imgur: /^https?:\/\/(\w+\.)?imgur.com\/(\w*\d\w*)+(\.[a-zA-Z]{3,4})?/gi
};
2020-04-03 11:12:35 +00:00
export default async bot => {
return [{
name: "parser",
2023-05-01 12:10:11 +00:00
call: regex.all,
2020-04-03 11:12:35 +00:00
active: true,
f: e => {
2023-05-02 00:06:40 +00:00
const links = e.message.match(regex.all)?.filter(link => !link.includes(cfg.main.url)) || [];
2023-05-01 12:10:11 +00:00
let repost;
2021-01-25 19:30:39 +00:00
2022-05-07 08:29:53 +00:00
if(e.media)
links.push(e.media);
2021-01-25 19:30:39 +00:00
2020-04-03 11:12:35 +00:00
if(links.length === 0)
return false;
2020-10-13 08:59:06 +00:00
if(e.message.match(/(!|-)ignore/))
return false;
2020-04-03 11:12:35 +00:00
2020-10-13 08:59:06 +00:00
if(!e.channel.includes("f0ck") && !e.message.match(/(!|-)f0ck/i))
return false;
2021-04-17 08:43:23 +00:00
console.log(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);
2020-04-03 11:12:35 +00:00
links.forEach(async link => {
2023-05-01 12:10:11 +00:00
if(regex.imgur.test(link))
return await e.reply(`imgur is not supported!`);
2020-04-03 11:12:35 +00:00
// check repost (link)
2023-05-01 12:10:11 +00:00
repost = await queue.checkrepostlink(link);
if(repost)
return await e.reply(`repost motherf0cker (link): ${cfg.main.url.full}/${repost}`);
2020-04-03 11:12:35 +00:00
// generate uuid
2023-05-01 12:10:11 +00:00
const uuid = await queue.genuuid();
2020-04-03 11:12:35 +00:00
2023-05-01 12:10:11 +00:00
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * cfg.main.adminmultiplier : cfg.main.maxfilesize);
2021-01-25 19:30:39 +00:00
2020-04-03 11:12:35 +00:00
// read metadata
2023-05-01 13:44:40 +00:00
/*if(!regex.yt.test(link)) {
2021-05-25 12:43:43 +00:00
const tmphead = (await fetch(link, { method: "HEAD" })).headers["content-type"];
if(!Object.keys(cfg.mimes).includes(tmphead))
return;
2023-05-01 12:10:11 +00:00
ext = cfg.mimes[tmphead];
2023-05-01 13:44:40 +00:00
}*/
let ext;
try {
const meta = JSON.parse((await queue.exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --skip-download --dump-json "${link}"`)).stdout);
ext = meta.ext;
} catch(err) {
const tmphead = (await fetch(link, { method: "HEAD" })).headers["content-type"];
ext = tmphead;
}
if(!Object.values(cfg.mimes).includes(ext.toLowerCase())) {
return await e.reply('mime schmime '+ext);
2021-05-25 12:43:43 +00:00
}
2021-05-16 11:24:31 +00:00
2023-04-28 05:04:06 +00:00
const msg = await e.reply(`[charging the f0cker] downloading: ${uuid}`, {
disable_notification: true
});
2020-10-13 08:59:06 +00:00
2020-04-03 11:12:35 +00:00
// download data
2020-04-09 16:44:09 +00:00
const start = new Date();
2023-05-01 13:44:40 +00:00
let source;
try {
source = (await queue.exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' "${link}" --max-filesize ${maxfilesize / 1024}k --postprocessor-args "ffmpeg:-bitexact" -o "./tmp/${uuid}.%(ext)s" --print after_move:filepath --merge-output-format "mp4"`)).stdout.trim();
} catch(err) {
if(e.type == 'tg')
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, "something went wrong lol");
return await e.reply("something went wrong lol");
}
2023-05-01 12:10:11 +00:00
if(!source) {
if(e.type == 'tg')
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, "something went wrong lol");
return await e.reply("something went wrong lol");
2021-04-17 08:43:23 +00:00
}
2021-01-25 19:30:39 +00:00
2023-04-28 05:04:06 +00:00
if(source.match(/larger than/)) {
if(e.type == 'tg')
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, "too large lol");
return await e.reply("too large lol");
}
2020-04-09 16:44:09 +00:00
const end = ~~((new Date() - start) / 1e3);
2020-04-03 11:12:35 +00:00
2023-05-01 12:10:11 +00:00
// filesize check
const size = fs.statSync(source).size;
if(size > maxfilesize) {
await fs.promises.unlink(source).catch(_=>{});
if(e.type == 'tg')
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, `too large lol. (${lib.formatSize(size)} / ${lib.formatSize(maxfilesize)})`);
return await e.reply(`too large lol. (${lib.formatSize(size)} / ${lib.formatSize(maxfilesize)})`);
}
2020-04-03 11:12:35 +00:00
// mime check
2023-05-01 13:44:40 +00:00
let mime = (await queue.exec(`file --mime-type -b ${source}`)).stdout.trim();
try {
if(mime == 'video/x-matroska') { // mkv failsafe
await queue.exec(`ffmpeg -i ./tmp/${uuid}.mkv -codec copy ./tmp/${uuid}.mp4`);
2023-05-01 13:51:41 +00:00
await fs.promises.unlink(source).catch(_=>{});
2023-05-01 13:44:40 +00:00
source = source.replace(/\.mkv$/, '.mp4');
mime = 'video/mp4';
}
2023-05-01 13:51:41 +00:00
if(source.match(/\.opus$/)) { // opus failsafe
2023-05-01 13:44:40 +00:00
await queue.exec(`ffmpeg -i ./tmp/${uuid}.opus -codec copy ./tmp/${uuid}.ogg`);
await fs.promises.unlink(source);
source = source.replace(/\.opus$/, '.ogg');
mime = 'audio/ogg';
}
} catch(err) {
2023-05-01 13:51:41 +00:00
await fs.promises.unlink(source).catch(_=>{});
2023-05-01 13:44:40 +00:00
if(e.type == 'tg')
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, "something went wrong lol");
return await e.reply("something went wrong lol");
}
2023-04-28 05:04:06 +00:00
if(!Object.keys(cfg.mimes).includes(mime)) {
2023-05-01 13:44:40 +00:00
await fs.promises.unlink(source).catch(_=>{});
2023-04-28 05:04:06 +00:00
if(e.type == 'tg')
2023-05-01 12:10:11 +00:00
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, `lol, go f0ck yourself (${mime})`);
2023-04-28 05:04:06 +00:00
return await e.reply(`lol, go f0ck yourself (${mime})`);
}
2023-05-01 12:10:11 +00:00
// generate checksum
const checksum = (await queue.exec(`sha256sum ${source}`)).stdout.trim().split(" ")[0];
2020-04-03 11:12:35 +00:00
// check repost (checksum)
2023-05-01 12:10:11 +00:00
repost = await queue.checkrepostsum(checksum);
if(repost) {
await fs.promises.unlink(source).catch(_=>{});
2023-04-28 05:04:06 +00:00
if(e.type == 'tg')
2023-05-01 12:10:11 +00:00
return await e.editMessageText(msg.result.chat.id, msg.result.message_id, `repost motherf0cker (checksum): ${cfg.main.url.full}/${repost}`);
return await e.reply(`repost motherf0cker (checksum): ${cfg.main.url.full}/${repost}`);
2023-04-28 05:04:06 +00:00
}
2020-04-03 11:12:35 +00:00
2023-05-01 12:10:11 +00:00
const filename = path.basename(source);
await fs.promises.copyFile(source, `./public/b/${filename}`);
await fs.promises.unlink(source).catch(_=>{});
2020-04-03 11:12:35 +00:00
await db`
insert into items ${
db({
2022-05-07 08:45:32 +00:00
src: e.media ? "" : link,
dest: filename,
mime: mime,
size: size,
checksum: checksum,
username: e.user.nick || e.user.username,
userchannel: e.channel,
usernetwork: e.network,
stamp: ~~(new Date() / 1000),
2022-05-22 14:42:01 +00:00
active: true
}, 'src', 'dest', 'mime', 'size', 'checksum', 'username', 'userchannel', 'usernetwork', 'stamp', 'active')
}
`;
2023-05-01 12:10:11 +00:00
const itemid = await queue.getItemID(filename);
2020-04-03 11:12:35 +00:00
// generate thumbnail
2021-04-17 08:43:23 +00:00
try {
2023-05-01 12:10:11 +00:00
await queue.genThumbnail(filename, mime, itemid, link);
2021-04-17 08:43:23 +00:00
} catch(err) {
2023-05-01 12:10:11 +00:00
await queue.exec(`convert ./mugge.png ./public/t/${itemid}.webp`);
2020-04-03 11:12:35 +00:00
}
2021-01-25 19:30:39 +00:00
let speed = lib.calcSpeed(size, end);
speed = !Number.isFinite(speed) ? "yes" : `${speed.toFixed(2)} Mbit/s`;
2022-01-04 20:03:48 +00:00
// autotagger
2023-05-02 00:06:40 +00:00
let tags = [];
if(cfg.apis?.nsfw1 && mime.startsWith('image')) {
const nsfw = await autotagger.isNSFW(filename, size);
tags.push(nsfw ? 'nsfw' : 'sfw');
if(nsfw)
await queue.tagNSFW(itemid);
else
await queue.tagSFW(itemid);
}
let outputmsgirc = `[f0cked] link: ${cfg.main.url.full}/${itemid} | size: ${lib.formatSize(size)} | speed: ${speed}`;
let outputmsgtg = `[f0cked] size: ${lib.formatSize(size)} | speed: ${speed}`;
2021-12-23 05:07:19 +00:00
2023-05-02 00:06:40 +00:00
if(tags.length > 0) {
const tagstr = tags.join(', ');
outputmsgirc += ` | tags: ${tagstr}`;
outputmsgtg += ` | tags: ${tagstr}`;
}
2020-04-03 11:12:35 +00:00
2023-04-28 05:04:06 +00:00
if(e.type == 'tg') {
await e.deleteMessage(msg.result.chat.id, msg.result.message_id);
await e.reply(outputmsgtg, {
reply_markup: JSON.stringify({
inline_keyboard: [[
2023-05-02 00:27:35 +00:00
{ text: (await lib.hasTag(itemid, 1) ? '✓ ' : '') + 'sfw', callback_data: `b_sfw:${itemid}` },
{ text: (await lib.hasTag(itemid, 2) ? '✓ ' : '') + 'nsfw', callback_data: `b_nsfw:${itemid}` },
2023-04-28 05:04:06 +00:00
{ text: 'tags', callback_data: `b_tags:${itemid}` },
{ text: '❌ delete', callback_data: `b_delete:${itemid}` }
], [
{ text: `open f0ck #${itemid}`, url: `${cfg.main.url.full}/${itemid}` }
]]
})
});
}
else {
await e.reply(outputmsgirc);
}
2020-04-03 11:12:35 +00:00
});
}
}];
};