forked from f0ck/f0ckv2
muh
This commit is contained in:
92
src/inc/trigger/parser.mjs
Normal file
92
src/inc/trigger/parser.mjs
Normal file
@@ -0,0 +1,92 @@
|
||||
import cfg from "../../../config.json";
|
||||
import sql from "../sql.mjs";
|
||||
|
||||
import fs from "fs";
|
||||
import { exec as _exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
const exec = promisify(_exec);
|
||||
|
||||
const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
|
||||
|
||||
export default async bot => {
|
||||
|
||||
return [{
|
||||
name: "parser",
|
||||
call: regex,
|
||||
active: true,
|
||||
f: e => {
|
||||
const links = e.message.match(regex)?.filter(link => {
|
||||
return (
|
||||
!link.includes("f0ck.me")
|
||||
);
|
||||
});
|
||||
if(links.length === 0)
|
||||
return false;
|
||||
|
||||
e.reply(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);
|
||||
|
||||
links.forEach(async link => {
|
||||
// check repost (link)
|
||||
const q_repost = await sql.query("select id from items where src = ?", [ link ]);
|
||||
if(q_repost.length > 0)
|
||||
return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);
|
||||
|
||||
// generate uuid
|
||||
const uuid = (await sql.query("select left(uuid(), 8) as uuid"))[0].uuid;
|
||||
|
||||
// read metadata
|
||||
const meta = JSON.parse((await exec(`youtube-dl --skip-download --dump-json "${link}"`)).stdout);
|
||||
|
||||
const filename = `${uuid}.${meta.ext}`;
|
||||
|
||||
// download data
|
||||
const source = (await exec(`youtube-dl "${link}" --max-filesize 100m -o ./tmp/${filename}`)).stdout.trim();
|
||||
if(source.match(/larger than/))
|
||||
return e.reply("too large lol");
|
||||
|
||||
// generate checksum
|
||||
const checksum = (await exec(`sha256sum ./tmp/${filename}`)).stdout.trim().split(" ")[0];
|
||||
const size = fs.statSync(`./tmp/${filename}`).size;
|
||||
|
||||
// mime check
|
||||
const mime = (await exec(`file --mime-type -b ./tmp/${filename}`)).stdout.trim();
|
||||
if(!cfg.allowed.includes(mime))
|
||||
return e.reply(`lol, go f0ck yourself (${mime})`);
|
||||
|
||||
// check repost (checksum)
|
||||
const q_repostc = await sql.query("select id from items where checksum = ?", [ checksum ]);
|
||||
if(q_repostc.length > 0)
|
||||
return e.reply(`repost motherf0cker (checksum): ${cfg.main.url}/${q_repostc[0].id}`);
|
||||
|
||||
await fs.promises.copyFile(`./tmp/${filename}`, `./public/b/${filename}`);
|
||||
await fs.promises.unlink(`./tmp/${filename}`).catch(_=>{});
|
||||
|
||||
const insertq = await sql.query(
|
||||
"insert into items (src, dest, mime, size, checksum, username, userchannel, usernetwork, stamp, active) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[ link, filename, mime, size, checksum, e.user.username, e.channel, e.network, ~~(new Date() / 1000), 1 ]
|
||||
);
|
||||
|
||||
// generate thumbnail
|
||||
let thumb_orig = (await exec(`youtube-dl --get-thumbnail "${link}"`)).stdout.trim();
|
||||
if(!thumb_orig.startsWith("http")) {
|
||||
if(mime.startsWith("image") && mime !== "image/gif")
|
||||
thumb_orig = `./public/b/${filename}`;
|
||||
else {
|
||||
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq.insertId}`);
|
||||
thumb_orig = `./tmp/${insertq.insertId}`;
|
||||
}
|
||||
}
|
||||
await exec(`convert "${thumb_orig}" -resize "200x200^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq.insertId}.png`);
|
||||
await fs.promises.unlink(`./tmp/${insertq.insertId}`).catch(_=>{});
|
||||
|
||||
e.reply([
|
||||
`title: ${meta.fulltitle}`,
|
||||
`name: ${filename}`,
|
||||
`size: ${size}`,
|
||||
`link: ${cfg.main.url}/${insertq.insertId}`
|
||||
]);
|
||||
|
||||
});
|
||||
}
|
||||
}];
|
||||
};
|
Reference in New Issue
Block a user