Last active
May 3, 2026 04:23
-
-
Save weskerty/ec6f1c2cbe5946439e036815c8968b10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs').promises; | |
| const path = require('path'); | |
| const { promisify } = require('util'); | |
| const { exec: execCB } = require('child_process'); | |
| const { bot, pinterestSearch, addExif } = require('../lib'); | |
| const exec = promisify(execCB); | |
| const MAX = 1024 * 1024; | |
| const UK = ''; | |
| const TK = 'LIVDSRZULELA'; | |
| const NI = cmd => `nice -n 15 ${cmd}`; | |
| const SC = 'fps=15,scale=512:512'; | |
| const AV = '-c:v libwebp -quality ${q} -loop 0 -an'; | |
| const iA = m => ['image/gif','image/webp'].includes(m) || m.startsWith('video/'); | |
| const gE = m => ({'image/jpeg':'jpg','image/png':'png','image/gif':'gif','image/webp':'webp','image/avif':'avif','video/mp4':'mp4','video/webm':'webm','video/quicktime':'mov'})[m] || m.split('/')[1] || 'bin'; | |
| function gMD(q) { | |
| try { | |
| const m = q.message?.message || q; | |
| if (m?.imageMessage) return { isMedia: true, mimetype: m.imageMessage.mimetype || 'image/jpeg' }; | |
| if (m?.videoMessage) return { isMedia: true, mimetype: m.videoMessage.mimetype || 'video/mp4' }; | |
| if (m?.documentMessage) { const mt = m.documentMessage.mimetype || ''; return { isMedia: mt.startsWith('image/') || mt.startsWith('video/'), mimetype: mt }; } | |
| return { isMedia: false }; | |
| } catch { return { isMedia: false }; } | |
| } | |
| async function ffStatic(i, o, q) { await exec(NI(`ffmpeg -y -i "${i}" -vf "${SC}" -c:v libwebp -quality ${q} -frames:v 1 "${o}"`)); } | |
| async function ffVideo(i, o, q) { await exec(NI(`ffmpeg -y -i "${i}" -vf "${SC}" -c:v libwebp -quality ${q} -loop 0 -an "${o}"`)); } | |
| async function ffGifPalette(i, o, q) { | |
| const pal = i.replace('.gif', '_pal.png'); | |
| await exec(NI(`ffmpeg -y -i "${i}" -vf "${SC},palettegen=stats_mode=full" "${pal}"`)); | |
| await exec(NI(`ffmpeg -y -i "${i}" -i "${pal}" -lavfi "${SC} [x]; [x][1:v] paletteuse=dither=bayer" -c:v libwebp_anim -quality ${q} -loop 0 "${o}"`)); | |
| await fs.unlink(pal).catch(() => {}); | |
| } | |
| async function compressGif(i, o) { | |
| try { | |
| await exec('which gif2webp'); | |
| await exec(NI(`gif2webp -q 80 -m 4 -mt "${i}" -o "${o}"`)); | |
| if ((await fs.stat(o)).size >= MAX) await exec(NI(`gif2webp -q 50 -m 4 -mt "${i}" -o "${o}"`)); | |
| return; | |
| } catch {} | |
| let q = 50; | |
| while (q >= 5) { await ffGifPalette(i, o, q); if ((await fs.stat(o)).size < MAX) break; q -= 10; } | |
| } | |
| async function compress(input, animated, videoUrl) { | |
| const tmpDir = `/tmp/lss_${Date.now()}_${Math.random().toString(36).slice(2)}`; | |
| await fs.mkdir(tmpDir, { recursive: true }); | |
| try { | |
| const srcUrl = videoUrl || (typeof input === 'string' ? input : null); | |
| let buf; | |
| if (srcUrl) { const r = await fetch(srcUrl, { signal: AbortSignal.timeout(15000) }); if (!r.ok) throw new Error(`HTTP ${r.status}`); buf = Buffer.from(await r.arrayBuffer()); } | |
| else { buf = input; } | |
| const ext = videoUrl ? 'mp4' : animated ? 'gif' : 'jpg'; | |
| const inPath = path.join(tmpDir, `in.${ext}`); | |
| const outPath = path.join(tmpDir, 'out.webp'); | |
| await fs.writeFile(inPath, buf); | |
| if (!animated) { | |
| let q = 50; | |
| while (q >= 5) { await ffStatic(inPath, outPath, q); if ((await fs.stat(outPath)).size < MAX) break; q -= 10; } | |
| } else if (videoUrl || ext === 'mp4') { | |
| let q = 50; | |
| while (q >= 5) { await ffVideo(inPath, outPath, q); if ((await fs.stat(outPath)).size < MAX) break; q -= 10; } | |
| } else { | |
| await compressGif(inPath, outPath); | |
| } | |
| return await fs.readFile(outPath); | |
| } finally { await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {}); } | |
| } | |
| async function toSticker(buf, animated, id) { return addExif(buf, '', id); } | |
| bot({ | |
| pattern: 'ls ?(.*)', | |
| fromMe: true, | |
| desc: 'Sticker search', | |
| type: 'sticker', | |
| }, async (message, match) => { | |
| const q = (match || '').trim(); | |
| try { await exec('ffmpeg -version'); } catch { return message.send('FFmpeg not found'); } | |
| const reply = t => message.send(t); | |
| const quoted = message.reply_message; | |
| if (!q && !quoted) return message.send('Usage: .lss <query> | reply to media'); | |
| if (quoted) { | |
| const d = gMD(quoted); | |
| if (!d.isMedia) return reply('Not an image or video'); | |
| const buf = await quoted.downloadMediaMessage(); | |
| if (!buf) return reply('Download error'); | |
| const animated = iA(d.mimetype); | |
| const isGif = d.mimetype === 'image/gif'; | |
| const inPath = `/tmp/lss_in_${Date.now()}.${gE(d.mimetype)}`; | |
| const outPath = `/tmp/lss_out_${Date.now()}.webp`; | |
| try { | |
| await fs.writeFile(inPath, buf); | |
| if (!animated) { | |
| let q2 = 50; while (q2 >= 5) { await ffStatic(inPath, outPath, q2); if ((await fs.stat(outPath)).size < MAX) break; q2 -= 10; } | |
| } else if (isGif) { | |
| await compressGif(inPath, outPath); | |
| } else { | |
| let q2 = 50; while (q2 >= 5) { await ffVideo(inPath, outPath, q2); if ((await fs.stat(outPath)).size < MAX) break; q2 -= 10; } | |
| } | |
| const out = await toSticker(await fs.readFile(outPath), animated, message.id); | |
| return message.send(out, { isAnimated: animated }, 'sticker'); | |
| } finally { await fs.unlink(inPath).catch(() => {}); await fs.unlink(outPath).catch(() => {}); } | |
| } | |
| const [uR, tR, pR] = await Promise.allSettled([ | |
| fetch(`https://api.unsplash.com/search/photos?query=${encodeURIComponent(q)}&client_id=${UK}&per_page=10&page=1`).then(r => r.json()), | |
| fetch(`https://g.tenor.com/v1/search?key=${TK}&q=${encodeURIComponent(q)}&limit=10`).then(r => r.json()), | |
| pinterestSearch(q), | |
| ]); | |
| const photos = uR.status === 'fulfilled' ? (uR.value.results || []) : []; | |
| const gifs = tR.status === 'fulfilled' ? (tR.value.results || []) : []; | |
| const pins = (pR.status === 'fulfilled' ? (pR.value || []) : []).slice(0, 10); | |
| if (!photos.length && !gifs.length && !pins.length) return reply('No results: ' + q); | |
| for (const p of photos) { | |
| try { await message.send(await toSticker(await compress(p.urls.regular, false), false, message.id), { isAnimated: false }, 'sticker'); } catch {} | |
| } | |
| const mp4s = gifs.map(g => g.media?.[0]?.mp4?.url).filter(Boolean); | |
| const gifUrls = gifs.map(g => g.media?.[0]?.tinygif?.url || g.media?.[0]?.gif?.url).filter(Boolean); | |
| const animSrcs = mp4s.length >= 10 ? mp4s : [...mp4s, ...gifUrls.slice(0, 10 - mp4s.length)]; | |
| for (const src of animSrcs) { | |
| try { | |
| const isMp4 = src.includes('.mp4'); | |
| await message.send(await toSticker(await compress(isMp4 ? null : src, true, isMp4 ? src : null), true, message.id), { isAnimated: true }, 'sticker'); | |
| } catch {} | |
| } | |
| for (const u of pins) { | |
| try { await message.send(await toSticker(await compress(u, false), false, message.id), { isAnimated: false }, 'sticker'); } catch {} | |
| } | |
| }); | |
| module.exports = {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment