lekker thumbnailer jongen

This commit is contained in:
Flummi
2022-01-04 21:03:48 +01:00
parent da6b310449
commit 081df808d9
4 changed files with 119 additions and 26 deletions

View File

@ -127,52 +127,70 @@ export default async bot => {
stamp: ~~(new Date() / 1000),
active: 1
});
const insertq = (await sql('items').where('dest', filename).limit(1))[0].id;
const itemid = (await sql('items').where('dest', filename).limit(1))[0].id;
// generate thumbnail
try {
let thumb_orig = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --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}`);
thumb_orig = `./tmp/${insertq}`;
}
else if(mime.startsWith("audio")) {
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${insertq}.png`)
await exec(`cp ./tmp/${insertq}.png ./public/ca/${insertq}.png`)
thumb_orig = `./tmp/${insertq}.png`;
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/)) {
const cover = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${link}"`)).stdout.trim();
if(!cover.match(/default_avatar/)) {
try {
await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
const size = (await fs.promises.stat(`./tmp/${itemid}.jpg`)).size;
if(size >= 0)
await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
await fs.promises.unlink(`./tmp/${itemid}.jpg`);
} catch(err) {}
}
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
}
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
await fs.promises.copyFile(`./tmp/${itemid}.png`, `./public/ca/${itemid}.png`);
}
await exec(`convert "${thumb_orig}" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq}.png`);
await fs.promises.unlink(`./tmp/${insertq}`).catch(_=>{});
await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.png`);
await fs.promises.unlink(`./tmp/${itemid}.png`);
} catch(err) {
e.reply("\x033>no thumb lol");
console.error(err);
await fs.promises.copyFile('./mugge.png', `./public/t/${itemid}.png`);
}
let speed = lib.calcSpeed(size, end);
speed = !Number.isFinite(speed) ? "yes" : `${speed.toFixed(2)} Mbit/s`;
// tagger
// autotagger
let tag;
if(mime.startsWith("image")) {
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";
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';
}
if(tag === 'sfw' || tag === 'nsfw') {
await sql("tags_assign").insert({
tag_id: tag === "sfw" ? 1 : 2,
item_id: insertq,
user_id: 7 // user: autotagger
item_id: itemid,
user_id: 7 // user: autotagger (ID: 7)
});
}
} catch(err) {
console.error(err);
}
e.reply([
`[f0cked] link: ${cfg.main.url}/${insertq} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
`[f0cked] link: ${cfg.main.url}/${itemid} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
]);
});