/** * Comment Quote Card Generator (Reverse-Engineered Scraper) * Author: Vamz Spectre / VamxAPI * Source: quotes-generator-teal-iota.vercel.app * * Install dependencies: * npm install skia-canvas axios * * Cara eksekusi: * curl -sL "https://code.vamxapi.my.id/snippet/raw/comment-quote-gen" -o comment-quote-gen.js * node comment-quote-gen.js "Kata-kata mutiara di sini" "Nama Author" "ID" */ const { Canvas, Path2D, Image } = require('skia-canvas'); const fs = require('fs'); const axios = require('axios'); const FF = 'Roboto, Arial, sans-serif'; const f4 = `400 24px ${FF}`, f2 = `400 20.4px ${FF}`, fb = `700 20.4px ${FF}`, f1 = `400 19.2px ${FF}`; const K = { st: '#000090', bg: '#000066', bd: '#4169e1', bb: '#2640cf' }; const NF = { PL: 'h,#fff,#dc143c', RU: 'h,#fff,#0039a6,#d52b1e', NL: 'h,#ae1c28,#fff,#21468b', DE: 'h,#000,#dd0000,#ffce00', AT: 'h,#ed2939,#fff,#ed2939', HU: 'h,#cd2a3e,#fff,#436f4d', UA: 'h,#0057b8,#ffd700', FR: 'v,#0055a4,#fff,#ef4135', IT: 'v,#009246,#fff,#ce2b37', IE: 'v,#169b62,#fff,#ff883e', BE: 'v,#000,#fae042,#ed2939', RO: 'v,#002b7f,#fcd116,#ce1126' }; const TP = new Path2D('M104 224H24c-13.255 0-24 10.745-24 24v240c0 13.255 10.745 24 24 24h80c13.255 0 24-10.745 24-24V248c0-13.255-10.745-24-24-24zM64 472c-13.255 0-24-10.745-24-24s10.745-24 24-24 24 10.745 24 24-10.745 24-24 24zM384 81.452c0 42.416-25.97 66.208-33.277 94.548h101.723c33.397 0 59.397 27.746 59.553 58.098.084 17.938-7.546 37.249-19.439 49.197l-.11.11c9.836 23.337 8.237 56.037-9.308 79.469 8.681 25.895-.069 57.704-16.382 74.757 4.298 17.598 2.244 32.575-6.148 44.632C440.202 511.587 389.616 512 346.839 512l-2.845-.001c-48.287-.017-87.806-17.598-119.56-31.725-15.957-7.099-36.821-15.887-52.651-16.178-6.54-.12-11.783-5.457-11.783-11.998v-213.77c0-3.2 1.282-6.271 3.558-8.521 39.614-39.144 56.648-80.587 89.117-113.111 14.804-14.832 20.188-37.236 25.393-58.902C282.515 39.293 291.817 0 312 0c24 0 72 8 72 81.452z'); const flagCache = {}; async function getFlagImage(countryCode) { const c = countryCode.toUpperCase(); if (flagCache[c]) return flagCache[c]; try { const res = await axios.get(`https://flagcdn.com/w160/${c.toLowerCase()}.png`, { responseType: 'arraybuffer', timeout: 5000 }); const img = new Image(); img.src = res.data; flagCache[c] = img; return img; } catch (err) { return null; } } const cn = n => Math.max(0, Math.floor(+n) || 0); const fd = v => { let d = new Date(v); if (isNaN(d.getTime())) d = new Date(); return d.toLocaleString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit' }).replace(/[\u202f\u00a0]/g, ' '); }; const mw = (g, t, f) => { g.font = f; return g.measureText(t).width; }; function T(g, t, x, y, f, a) { g.font = f; g.textAlign = a || 'left'; g.fillStyle = '#fff'; g.fillText(t, x, y); } function icon(g, x, y, dn) { g.save(); g.translate(x, dn ? y + 28 : y); g.scale(28 / 512, dn ? -28 / 512 : 28 / 512); g.fillStyle = '#fff'; g.fill(TP); g.restore(); } function drawFlag(g, c, x, hb, flagImg) { const y = hb - 15.3, w = 24, h = 19.3, n = NF[c]; g.save(); g.beginPath(); g.rect(x, y, w, h); g.clip(); if (c === 'ID') { g.fillStyle = '#f40439'; g.fillRect(x, y, w, h); g.fillStyle = '#fff'; g.fillRect(x, hb - 4.5, w, h); } else if (n) { const [d, ...k] = n.split(','); k.forEach((o, i) => { g.fillStyle = o; if (d === 'h') { g.fillRect(x, y + (h * i) / k.length, w, h / k.length + 1); } else { g.fillRect(x + (w * i) / k.length, y, w / k.length + 1, h); } }); } else if (flagImg) { g.drawImage(flagImg, x, y, w, h); } else { g.fillStyle = '#555'; g.fillRect(x, y, w, h); } g.restore(); } function wrap(g, t, mx) { g.font = f4; const m = s => g.measureText(s).width, o = []; for (const p of String(t).split('\n')) { let l = ''; for (let w of p.split(' ')) { const c = l ? l + ' ' + w : w; if (m(c) <= mx) { l = c; continue; } if (l) { o.push(l); l = ''; } while (m(w) > mx && w.length > 1) { let k = w.length - 1; while (k > 1 && m(w.slice(0, k)) > mx) k--; o.push(w.slice(0, k)); w = w.slice(k); } l = w; } o.push(l); } return o; } function blk(g, d, x0, xr, hb, lk, dr, flagImg) { if (dr) { drawFlag(g, d.c, x0 + 1.5, hb, flagImg); T(g, d.n, x0 + 32, hb, fb); T(g, fd(d.d), xr, hb, f2, 'right'); } const q = wrap(g, d.t, xr - x0); const ab = hb + 46 + (q.length - 1) * 28.1 + 45; if (dr) { q.forEach((s, i) => T(g, s, x0, hb + 46 + i * 28.1, f4)); const a = cn(d.l), b = cn(d.u); let x = x0 + 17.5; icon(g, x, ab - 20.5, 0); T(g, a ? '' + a : '', x + 36, ab, fb); x += 36 + mw(g, a ? '' + a : '0', fb) + 40.4; icon(g, x, ab - 18.5, 1); T(g, b ? '' + b : '', x + 35, ab, fb); x += 35 + mw(g, b ? '' + b : '0', fb) + 31; lk.forEach(k => { T(g, k, x, ab, f2); x += mw(g, k, f2) + 31; }); } return ab; } function lay(g, dr, bot, data, flags) { const { mode = 'N', name = 'Vamz Spectre', country = 'ID', text = 'Jangan takut gagal, takutlah kalau tidak pernah mencoba.', like = 999, unlike = 0, date = new Date(), hideReplies = false, replyCount = 1, replies = [] } = data; const rs = mode === 'R' ? replies : []; const t0 = 16, hb = t0 + 26; if (dr) { const H = bot + 17; g.fillStyle = K.st; g.fillRect(0, 0, 720, H); g.fillStyle = K.bg; g.fillRect(12, 0, 696, H); g.fillStyle = K.bd; g.fillRect(36, t0, 648, bot - t0 + 1); g.fillStyle = K.st; g.fillRect(37, t0 + 1, 646, bot - t0 - 1); } const m = { n: name, c: country.toUpperCase(), t: text, l: like, u: unlike, d: date }; const ab = blk(g, m, 49, 671, hb, rs.length ? ['Report'] : ['Reply', 'Report'], dr, flags[m.c]); if (!rs.length) { if (mode === 'N' && !hideReplies) { const n = Math.max(1, cn(replyCount)); const sb = ab + 62; if (dr) T(g, n === 1 ? 'Show 1 reply' : `Show ${n} replies`, 49, sb, f1); return Math.round(sb + 30); } return Math.round(ab + 17); } const hy = ab + 62; if (dr) T(g, 'Hide replies', 49, hy, f1); let y = hy + 30, la = 0; const y1 = Math.round(y); rs.forEach(r => { const rc = (r.c || 'ID').toUpperCase(); la = blk(g, { ...r, c: rc }, 96.5, 659, y + 25, ['Report'], dr, flags[rc]); y = la + 29; }); const bt = Math.round(la + 41); if (dr) { g.fillStyle = K.bd; g.fillRect(83, y1, 1, Math.round(la + 17) - y1); g.fillStyle = K.bb; g.fillRect(49, bt, 79, 40); g.fillStyle = K.st; g.fillRect(50, bt + 1, 77, 38); T(g, 'Reply', 63.1, bt + 25, f2); } return bt + 50; } async function renderCommentQuote(options = {}) { const countries = [options.country || 'ID']; if (options.replies && Array.isArray(options.replies)) { options.replies.forEach(r => { if (r.c) countries.push(r.c); }); } const flags = {}; for (const c of countries) { const uc = c.toUpperCase(); if (!['ID', 'PL', 'RU', 'NL', 'DE', 'AT', 'HU', 'UA', 'FR', 'IT', 'IE', 'BE', 'RO'].includes(uc)) { flags[uc] = await getFlagImage(uc); } } const s = options.scale || 1; const tempCanvas = new Canvas(720, 1000); const tempCtx = tempCanvas.getContext('2d'); const bot = lay(tempCtx, false, 0, options, flags); const canvas = new Canvas(720 * s, (bot + 17) * s); const ctx = canvas.getContext('2d'); ctx.setTransform(s, 0, 0, s, 0, 0); lay(ctx, true, bot, options, flags); return await canvas.toBuffer('png'); } module.exports = { renderCommentQuote }; // CLI Runner if (require.main === module) { (async () => { try { const quoteText = process.argv[2] || 'Jangan takut gagal, takutlah kalau tidak pernah mencoba.'; const authorName = process.argv[3] || 'Vamz Spectre'; const countryCode = process.argv[4] || 'ID'; const likeCount = parseInt(process.argv[5], 10) || 999; const unlikeCount = parseInt(process.argv[6], 10) || 0; const start = Date.now(); const imageBuffer = await renderCommentQuote({ name: authorName, country: countryCode, text: quoteText, like: likeCount, unlike: unlikeCount }); const base64Data = imageBuffer.toString('base64'); const result = { status: true, code: 200, creator: 'vamxapi', result: { name: authorName, country: countryCode, quote: quoteText, likes: likeCount, unlikes: unlikeCount, image_size: `${imageBuffer.length} bytes`, process_time: `${Date.now() - start}ms`, image_base64_preview: `data:image/png;base64,${base64Data.slice(0, 100)}...` } }; console.log(JSON.stringify(result, null, 2)); } catch (err) { console.log(JSON.stringify({ status: false, code: 500, creator: 'vamxapi', message: err.message }, null, 2)); process.exit(1); } })(); }