This commit is contained in:
Flummi
2021-04-17 10:43:23 +02:00
parent d553e71b50
commit 65454961ce
54 changed files with 1996 additions and 25454 deletions

View File

@ -1,5 +1,6 @@
import { promises as fs } from "fs";
import cfg from "../../../config.json";
import { exec } from "child_process";
import cfg from "../../inc/config.mjs";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
@ -27,13 +28,25 @@ export default async bot => {
case "thumb":
const rows = await sql.query("select id from items");
const dir = (await fs.readdir("./public/t")).filter(d => d.endsWith(".png")).map(e => +e.split(".")[0]);
const tmp = [];
for(let row of rows) {
for(let row of rows)
!dir.includes(row.id) ? tmp.push(row.id) : null;
}
e.reply(`${tmp.length}, ${rows.length}, ${dir.length}`);
break;
case "cache":
cfg.websrv.cache = !cfg.websrv.cache;
return e.reply(`Cache is ${cfg.websrv.cache ? "enabled" : "disabled"}`);
break;
case "uptime":
exec('sudo systemctl status f0ck', (err, stdout) => {
if(!err)
return e.reply(stdout.split('\n')[2].trim().replace("Active: active (running)", "i'm active"));
});
break;
case "restart":
e.reply("hay hay patron, hemen!");
exec("sudo systemctl restart f0ck");
break;
default:
return;
}

View File

@ -19,9 +19,8 @@ export default async bot => {
f: e => {
const links = e.message.match(regex)?.filter(link => !link.includes("f0ck.me")) || [];
if(e.photo) {
if(e.photo)
links.push(e.photo);
}
if(links.length === 0)
return false;
@ -32,7 +31,7 @@ export default async bot => {
if(!e.channel.includes("f0ck") && !e.message.match(/(!|-)f0ck/i))
return false;
//e.reply(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);
console.log(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);
links.forEach(async link => {
// check repost (link)
@ -43,28 +42,41 @@ export default async bot => {
// generate uuid
const uuid = (await sql.query("select left(uuid(), 8) as uuid"))[0].uuid;
const maxfilesize = ( getLevel(e.user).level > 50 ? cfg.main.maxfilesize * 2.5 : cfg.main.maxfilesize ) / 1024;
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * 2.5 : cfg.main.maxfilesize) / 1024;
let meta;
// read metadata
try {
meta = JSON.parse((await exec(`youtube-dl -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" --skip-download --dump-json "${link}"`)).stdout);
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);
}
catch(e) {
catch(err) {
e.reply("666 - kein b0ck!");
console.error(err);
return;
}
let filename = `${uuid}.${meta.ext}`;
e.reply(`downloading ${uuid}...`);
//e.reply(`downloading ${uuid}...`);
e.reply(`[charging the f0cker] downloading: ${uuid}.${meta.ext}`);
// download data
const start = new Date();
let source;
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();
else
source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" -o ./tmp/${filename}`)).stdout.trim();
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);
if(source.match(/larger than/))
return e.reply("too large lol");
@ -116,17 +128,27 @@ export default async bot => {
);
// generate thumbnail
let thumb_orig = (await exec(`youtube-dlc -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}`;
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`;
}
}
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);
}
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(_=>{});
let speed = lib.calcSpeed(size, end);
speed = !Number.isFinite(speed) ? "yes" : `${speed.toFixed(2)} Mbit/s`;

118
src/inc/trigger/tags.mjs Normal file
View File

@ -0,0 +1,118 @@
import sql from "../sql.mjs";
import cfg from "../config.mjs";
import { getLevel } from "../admin.mjs";
const queries = {
getTags: "select tags.id, tags.tag from tags_assign left join tags on tags.id = tags_assign.tag_id where tags_assign.item_id = ?",
assignTag: "insert into tags_assign (tag_id, item_id, prefix) values (?, ?, ?)"
};
const getTags = async itemid => {
return await sql.query(queries.getTags, [ itemid ]);
};
export default async bot => {
return [{
name: "tags show",
call: /^\!tags show \d+$/i,
active: true,
level: 100,
f: async e => {
const id = +e.args[1];
if(!id)
return e.reply("lol no");
const tags = await getTags(id).map(t => t.tag);
if(tags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is tagged as: ${tags.join(', ')}`);
}
}, {
name: "tags add",
call: /^\!tags add \d+ .*/i,
active: true,
level: 100,
f: async e => {
const id = +e.args[1];
if(!id)
return e.reply("lol no");
const tags = (await getTags(id)).map(t => t.tag);
const newtags = (e.message.includes(",")
? e.args.splice(2).join(" ").trim().split(",")
: e.args.splice(2)).filter(t => !tags.includes(t) && t.length > 0);
if(newtags.length === 0)
return e.reply("no (new) tags provided");
await Promise.all(newtags.map(async ntag => {
try {
let tagid;
const tag_exists = (await sql.query(`select id, tag from tags where tag = ?`, [ ntag ]));
if(tag_exists.length === 0) // create new tag
tagid = (await sql.query("insert into tags (tag) values (?)", [ ntag ])).insertId;
else
tagid = tag_exists[0].id;
return await sql.query(queries.assignTag, [ tagid, id, `${e.user.prefix}${e.channel}` ]);
} catch(err) {
console.error(err);
}
}));
const ntags = (await getTags(id)).map(t => t.tag);
if(ntags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is now tagged as: ${ntags.join(', ')}`);
}
}, {
name: "tags remove",
call: /^\!tags remove \d+ .*/i,
active: true,
level: 100,
f: async e => {
const id = +e.args[1];
if(!id)
return e.reply("lol no");
const tags = await getTags(id);
const removetags = (e.message.includes(",")
? e.args.splice(2).join(" ").trim().split(",")
: e.args.splice(2)).filter(t => t.length > 0);
if(removetags.length === 0)
return e.reply("no tags provided");
const res = await Promise.all(removetags.map(async rtag => {
const tagid = tags.filter(t => t.tag === rtag)[0]?.id ?? null;
if(!tagid || tagid?.length === 0) {
return {
success: false,
tag: rtag,
msg: "tag is not assigned"
};
}
let q;
if(getLevel(e.user).level > 50)
q = !!(await sql.query("delete from tags_assign where tag_id = ? and item_id = ? limit 1", [ tagid, id ])).affectedRows;
else
q = !!(await sql.query("delete from tags_assign where tag_id = ? and item_id = ? and prefix = ? limit 1", [ tagid, id, `${e.user.prefix}${e.channel}` ])).affectedRows;
return {
success: q,
tag: rtag,
tagid: tagid
};
}));
e.reply(JSON.stringify(res));
const ntags = (await getTags(id)).map(t => t.tag);
if(ntags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is now tagged as: ${ntags.join(', ')}`);
}
}]
};