2021-12-04 11:19:47 +00:00
|
|
|
import cfg from "../config.mjs";
|
2022-03-31 11:34:51 +00:00
|
|
|
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";
|
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";
|
|
|
|
import { exec as _exec } from "child_process";
|
2021-12-09 16:02:35 +00:00
|
|
|
|
|
|
|
const exec = cmd => new Promise((resolve, reject) => {
|
|
|
|
_exec(cmd, { maxBuffer: 5e3 * 1024 }, (err, stdout, stderr) => {
|
|
|
|
if(err)
|
|
|
|
return reject(err);
|
|
|
|
if(stderr)
|
|
|
|
console.error(stderr);
|
|
|
|
resolve({ stdout: stdout });
|
|
|
|
});
|
|
|
|
});
|
2020-04-03 11:12:35 +00:00
|
|
|
|
2021-05-16 11:24:31 +00:00
|
|
|
//const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
|
|
|
|
const regex = /https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/gi;
|
2020-04-03 11:12:35 +00:00
|
|
|
|
|
|
|
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)
|
2022-03-31 11:34:51 +00:00
|
|
|
const q_repost = await db`
|
|
|
|
select id
|
|
|
|
from "items"
|
|
|
|
where src = ${link}
|
|
|
|
`;
|
2020-04-03 11:12:35 +00:00
|
|
|
if(q_repost.length > 0)
|
|
|
|
return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);
|
|
|
|
|
|
|
|
// generate uuid
|
2022-03-31 11:34:51 +00:00
|
|
|
const uuid = (await db`
|
|
|
|
select gen_random_uuid() as uuid
|
|
|
|
`)[0].uuid.substring(0, 8);
|
2020-04-03 11:12:35 +00:00
|
|
|
|
2022-01-06 03:10:44 +00:00
|
|
|
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * cfg.main.adminmultiplier : 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-12-09 16:02:35 +00:00
|
|
|
meta = JSON.parse((await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --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-12-23 05:07:19 +00:00
|
|
|
//e.reply("[error] f0ck has no bock :(");
|
|
|
|
//console.error(err);
|
2021-01-25 19:30:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-03 11:12:35 +00:00
|
|
|
|
2021-05-25 12:43:43 +00:00
|
|
|
if(!Object.values(cfg.mimes).includes(meta.ext.toLowerCase())) {
|
|
|
|
const tmphead = (await fetch(link, { method: "HEAD" })).headers["content-type"];
|
|
|
|
if(!Object.keys(cfg.mimes).includes(tmphead))
|
|
|
|
return;
|
|
|
|
meta.ext = cfg.mimes[tmphead];
|
|
|
|
}
|
2021-05-16 11:24:31 +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-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") {
|
2021-12-09 16:02:35 +00:00
|
|
|
source = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' "${link}" --max-filesize ${maxfilesize}k --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
|
2021-12-26 16:26:30 +00:00
|
|
|
//change 720 to any other available resolution, higher = better quality but bigger filesize
|
2021-04-17 08:43:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-12-09 16:02:35 +00:00
|
|
|
source = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' "${link}" --max-filesize ${maxfilesize}k -o ./tmp/${filename}`)).stdout.trim();
|
2021-12-26 16:26:30 +00:00
|
|
|
//change 720 to any other available resolution, higher = better quality but bigger filesize
|
2021-04-17 08:43:23 +00:00
|
|
|
}
|
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();
|
2021-05-25 12:43:43 +00:00
|
|
|
if(!Object.keys(cfg.mimes).includes(mime))
|
2020-04-03 11:12:35 +00:00
|
|
|
return e.reply(`lol, go f0ck yourself (${mime})`);
|
2021-02-10 10:16:30 +00:00
|
|
|
|
2021-05-25 12:43:43 +00:00
|
|
|
if(!Object.values(cfg.mimes).includes(meta.ext.toLowerCase())) {
|
|
|
|
let tmpext = cfg.mimes[meta.ext.toLowerCase()];
|
2021-02-10 10:16:30 +00:00
|
|
|
fs.renameSync(`./tmp/${filename}`, `./tmp/${uuid}.${tmpext}`);
|
|
|
|
filename = `${uuid}.${tmpext}`;
|
|
|
|
}
|
2020-04-03 11:12:35 +00:00
|
|
|
|
|
|
|
// check repost (checksum)
|
2022-03-31 11:34:51 +00:00
|
|
|
const q_repostc = await db`
|
|
|
|
select id
|
|
|
|
from "items"
|
|
|
|
where checksum = ${checksum}
|
|
|
|
`;
|
2020-04-03 11:12:35 +00:00
|
|
|
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(_=>{});
|
|
|
|
|
2022-03-31 11:34:51 +00:00
|
|
|
await db`
|
|
|
|
insert into items ${
|
|
|
|
db({
|
|
|
|
src: e.photo ? "" : 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),
|
|
|
|
active: 1
|
|
|
|
}, 'src', 'dest', 'mime', 'size', 'checksum', 'username', 'userchannel', 'usernetwork', 'stamp', 'active')
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const itemid = (await db`
|
|
|
|
select *
|
|
|
|
from "items"
|
|
|
|
where dest = ${filename}
|
|
|
|
limit 1
|
|
|
|
`)[0].id;
|
2020-04-03 11:12:35 +00:00
|
|
|
|
|
|
|
// generate thumbnail
|
2021-04-17 08:43:23 +00:00
|
|
|
try {
|
2022-01-04 20:03:48 +00:00
|
|
|
if(mime.startsWith('video/') || mime == 'image/gif')
|
|
|
|
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${itemid}.png`);
|
|
|
|
else if(mime.startsWith('image/') && mime != 'image/gif')
|
|
|
|
await exec(`convert ./public/b/${filename} ./tmp/${itemid}.png`);
|
|
|
|
else if(mime.startsWith('audio/')) {
|
|
|
|
if(link.match(/soundcloud/)) {
|
2022-01-06 03:10:44 +00:00
|
|
|
let cover = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${link}"`)).stdout.trim();
|
2022-01-04 20:03:48 +00:00
|
|
|
if(!cover.match(/default_avatar/)) {
|
2022-01-06 03:10:44 +00:00
|
|
|
cover = cover.replace(/-(large|original)\./, '-t500x500.');
|
2022-01-04 20:03:48 +00:00
|
|
|
try {
|
|
|
|
await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
|
|
|
|
const size = (await fs.promises.stat(`./tmp/${itemid}.jpg`)).size;
|
2022-01-05 19:21:50 +00:00
|
|
|
if(size >= 0) {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
|
2022-01-06 03:10:44 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.jpg ./public/ca/${itemid}.webp`);
|
2022-01-05 19:21:50 +00:00
|
|
|
}
|
2022-01-04 20:03:48 +00:00
|
|
|
} catch(err) {}
|
|
|
|
}
|
2022-01-05 19:21:50 +00:00
|
|
|
else {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
|
2022-01-05 19:21:50 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
|
|
|
|
}
|
2021-04-17 08:43:23 +00:00
|
|
|
}
|
2022-01-05 19:21:50 +00:00
|
|
|
else {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
|
2022-01-05 19:21:50 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
|
|
|
|
}
|
2020-04-03 11:12:35 +00:00
|
|
|
}
|
2022-01-05 19:21:50 +00:00
|
|
|
|
|
|
|
await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.webp`);
|
|
|
|
await fs.promises.unlink(`./tmp/${itemid}.png`).catch(err => {});
|
|
|
|
await fs.promises.unlink(`./tmp/${itemid}.jpg`).catch(err => {});
|
2021-04-17 08:43:23 +00:00
|
|
|
} catch(err) {
|
2022-01-05 19:21:50 +00:00
|
|
|
await 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
|
2021-12-23 05:07:19 +00:00
|
|
|
let tag;
|
2022-01-04 20:03:48 +00:00
|
|
|
try {
|
|
|
|
if(mime.startsWith('image') && mime != 'image/gif') {
|
|
|
|
const body = { headers: { Authorization: `Basic ${cfg.tagger.btoa}` } };
|
|
|
|
const res = await (await fetch(`${cfg.tagger.endpoint}/categories/nsfw_beta?image_url=${cfg.main.url}/b/${filename}`, body)).json();
|
|
|
|
if(res)
|
|
|
|
tag = (res.result.categories[0].name.en === 'safe') ? 'sfw' : 'nsfw';
|
|
|
|
}
|
|
|
|
else if(mime.startsWith('audio')) {
|
|
|
|
tag = 'sfw';
|
|
|
|
}
|
2021-12-23 05:07:19 +00:00
|
|
|
|
2022-01-04 20:03:48 +00:00
|
|
|
if(tag === 'sfw' || tag === 'nsfw') {
|
2022-03-31 11:34:51 +00:00
|
|
|
await db`
|
|
|
|
insert into "tags_assign" ${
|
|
|
|
db({
|
|
|
|
tag_id: tag === "sfw" ? 1 : 2,
|
|
|
|
item_id: itemid,
|
|
|
|
user_id: 7 // user: autotagger (ID: 7)
|
|
|
|
}, 'tag_id', 'item_id', 'user_id')
|
|
|
|
}
|
|
|
|
`;
|
2021-12-23 05:07:19 +00:00
|
|
|
}
|
2022-01-04 20:03:48 +00:00
|
|
|
} catch(err) {
|
|
|
|
console.error(err);
|
2021-12-23 05:07:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 11:12:35 +00:00
|
|
|
e.reply([
|
2022-01-04 20:03:48 +00:00
|
|
|
`[f0cked] link: ${cfg.main.url}/${itemid} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
|
2020-04-03 11:12:35 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
};
|