Spaces:
Runtime error
Runtime error
Update app.js
Browse files
app.js
CHANGED
|
@@ -2,10 +2,58 @@ import express from 'express';
|
|
| 2 |
import { chromium } from 'playwright';
|
| 3 |
import axios from 'axios';
|
| 4 |
import frm from 'form-data'
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const port = 7860;
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app.get("/", (req,res) => {
|
| 10 |
res.send(`biji biliar`)
|
| 11 |
})
|
|
|
|
| 2 |
import { chromium } from 'playwright';
|
| 3 |
import axios from 'axios';
|
| 4 |
import frm from 'form-data'
|
| 5 |
+
import sharp from "sharp";
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const port = 7860;
|
| 9 |
|
| 10 |
+
async function brat(txt) {
|
| 11 |
+
const browser = await chromium.launch({ headless: true });
|
| 12 |
+
try {
|
| 13 |
+
const context = await browser.newContext({
|
| 14 |
+
viewport: { width: 400, height: 400 },
|
| 15 |
+
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
const page = await context.newPage();
|
| 19 |
+
await page.goto('https://www.bratgenerator.com/');
|
| 20 |
+
await page.evaluate(() => {
|
| 21 |
+
const del = (el) => document.querySelector(el)?.remove();
|
| 22 |
+
setupTheme('white');
|
| 23 |
+
del("div.themeSelector");
|
| 24 |
+
del("div.screenshot-blurb");
|
| 25 |
+
});
|
| 26 |
+
await page.fill('#textInput', txt);
|
| 27 |
+
await page.click('#onetrust-accept-btn-handler');
|
| 28 |
+
await page.waitForTimeout(500);
|
| 29 |
+
const buffer = await page.screenshot({ type: "png"})
|
| 30 |
+
const crop = await sharp(buffer).extract({ left: 0,
|
| 31 |
+
top: 0,
|
| 32 |
+
width: 400,
|
| 33 |
+
height: 360
|
| 34 |
+
}).toBuffer()
|
| 35 |
+
return crop
|
| 36 |
+
} catch (e) {
|
| 37 |
+
console.error(e);
|
| 38 |
+
} finally {
|
| 39 |
+
await browser.close();
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
app.get("/input", async (req, res) => {
|
| 44 |
+
const text = req.query.q;
|
| 45 |
+
if (!text) return res.status(400).send("Query 'q' kosong cuy");
|
| 46 |
+
|
| 47 |
+
try {
|
| 48 |
+
const imageBuffer = await brat(text);
|
| 49 |
+
res.set("Content-Type", "image/png");
|
| 50 |
+
res.send(imageBuffer);
|
| 51 |
+
} catch (err) {
|
| 52 |
+
console.error(err);
|
| 53 |
+
res.status(500).send("Gagal generate gambar");
|
| 54 |
+
}
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
app.get("/", (req,res) => {
|
| 58 |
res.send(`biji biliar`)
|
| 59 |
})
|