f0ckv2/src/inc/trigger/parser.mjs

166 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-04-03 11:12:35 +00:00
import cfg from "../../../config.json";
import sql 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";
2020-04-03 11:12:35 +00:00
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 => {
2021-01-25 19:30:39 +00:00
const links = e.message.match(regex)?.filter(link => !link.includes("f0ck.me")) || [];
2021-04-17 08:43:23 +00:00
if(e.photo)
2021-01-25 19:30:39 +00:00
links.push(e.photo);
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 => {
// 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;
2021-04-17 08:43:23 +00:00
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * 2.5 : cfg.main.maxfilesize) / 1024;
2021-01-25 19:30:39 +00:00
let meta;
2020-04-03 11:12:35 +00:00
// read metadata
2021-01-25 19:30:39 +00:00
try {
2021-04-17 08:43:23 +00:00
meta = JSON.parse((await exec(`youtube-dl -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" --skip-download --dump-json "${link}"`)).stdout);
//meta = JSON.parse((await exec(`youtube-dl -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" --skip-download --dump-json "${link}"`)).stdout);
2021-01-25 19:30:39 +00:00
}
2021-04-17 08:43:23 +00:00
catch(err) {
2021-05-15 11:58:24 +00:00
//e.reply("(╯° °)╯︵ ┻━┻)");
2021-04-17 08:43:23 +00:00
console.error(err);
2021-01-25 19:30:39 +00:00
return;
}
2020-04-03 11:12:35 +00:00
2021-02-10 10:16:30 +00:00
let filename = `${uuid}.${meta.ext}`;
2020-04-03 11:12:35 +00:00
2021-04-17 08:43:23 +00:00
//e.reply(`downloading ${uuid}...`);
2021-04-22 02:12:40 +00:00
e.reply(`[charging the f0cker] downloading: ${uuid}`);
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();
2021-01-25 19:30:39 +00:00
let source;
2021-04-17 08:43:23 +00:00
if(meta.ext === "mp4") {
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
console.log("mp4 lol");
}
else {
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=${meta.ext},filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[ext=${meta.ext},width<2000][height<=1200]/best" --merge-output-format ${meta.ext} -o ./tmp/${filename}`)).stdout.trim();
source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[filesize<${maxfilesize}k][width<2000][height<=1200][ext=${meta.ext}]+bestaudio[filesize<${maxfilesize}k][ext=${meta.ext}]/best" -o ./tmp/${filename}`)).stdout.trim();
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" -o ./tmp/${filename}`)).stdout.trim();
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" -o ./tmp/${filename}`)).stdout.trim();
console.log("alles andere lol");
}
console.log(source);
2021-01-25 19:30:39 +00:00
2020-04-03 11:12:35 +00:00
if(source.match(/larger than/))
return 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
// 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})`);
2021-02-10 10:16:30 +00:00
if(!cfg.ext.includes(meta.ext)) {
let tmpext = "";
if(mime === "image/png")
tmpext = "png";
else if(mime === "video/webm")
tmpext = "webm";
else if(mime === "image/gif")
tmpext = "gif";
else if(mime === "image/jpeg")
tmpext = "jpg";
else if(mime === "video/mp4" || mime === "video/quicktime")
tmpext = "mp4";
else if(mime === "audio/mpeg")
tmpext = "mp3";
else if(mime === "audio/ogg")
tmpext = "ogg";
else if(mime === "audio/flac" || mime === "audio/x-flac")
tmpext = "flac";
fs.renameSync(`./tmp/${filename}`, `./tmp/${uuid}.${tmpext}`);
filename = `${uuid}.${tmpext}`;
}
2020-04-03 11:12:35 +00:00
// 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
2021-01-25 19:30:39 +00:00
[ e.photo ? "" : link, filename, mime, size, checksum, e.user.nick || e.user.username, e.channel, e.network, ~~(new Date() / 1000), 1 ]
2020-04-03 11:12:35 +00:00
);
// generate thumbnail
2021-04-17 08:43:23 +00:00
try {
let thumb_orig = (await exec(`youtube-dl -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --get-thumbnail "${link}"`)).stdout.trim();
if(!thumb_orig.startsWith("http")) {
if(mime.startsWith("image") && mime !== "image/gif")
thumb_orig = `./public/b/${filename}`;
else if(!mime.startsWith("audio")) {
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq.insertId}`);
thumb_orig = `./tmp/${insertq.insertId}`;
}
else if(mime.startsWith("audio")) {
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${insertq.insertId}.png`)
await exec(`cp ./tmp/${insertq.insertId}.png ./public/ca/${insertq.insertId}.png`)
thumb_orig = `./tmp/${insertq.insertId}.png`;
}
2020-04-03 11:12:35 +00:00
}
2021-04-17 08:43:23 +00:00
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(_=>{});
} catch(err) {
e.reply("\x033>no thumb lol");
console.error(err);
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`;
2020-04-03 11:12:35 +00:00
e.reply([
2021-03-03 04:34:57 +00:00
//`title: ${meta.fulltitle}`,
`link: ${cfg.main.url}/${insertq.insertId} | size: ${lib.formatSize(size)} | speed: ${speed}`
//`link: ${cfg.main.url}/${insertq.insertId}`
2020-04-03 11:12:35 +00:00
]);
});
}
}];
};