Upload 3 files
Browse files- Dockerfile.txt +26 -0
- app.js +111 -0
- package.json +25 -0
Dockerfile.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gunakan image Node.js yang mendukung Playwright
|
| 2 |
+
FROM mcr.microsoft.com/playwright:focal
|
| 3 |
+
|
| 4 |
+
# Set environment variable untuk menghindari dialog pada Playwright
|
| 5 |
+
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD 1
|
| 6 |
+
|
| 7 |
+
# Tentukan work directory
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Copy file package.json dan package-lock.json
|
| 11 |
+
COPY package*.json ./
|
| 12 |
+
|
| 13 |
+
# Install dependencies
|
| 14 |
+
RUN npm install
|
| 15 |
+
|
| 16 |
+
# Copy semua file ke container
|
| 17 |
+
COPY . .
|
| 18 |
+
|
| 19 |
+
# Install Playwright dependencies dan browser binaries
|
| 20 |
+
RUN npx playwright install --with-deps
|
| 21 |
+
|
| 22 |
+
# Expose port untuk aplikasi
|
| 23 |
+
EXPOSE 7860
|
| 24 |
+
|
| 25 |
+
# Start aplikasi
|
| 26 |
+
CMD ["npm", "start"]
|
app.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
import { chromium } from 'playwright';
|
| 3 |
+
import cors from 'cors';
|
| 4 |
+
import dotenv from 'dotenv';
|
| 5 |
+
import os from 'os';
|
| 6 |
+
import sharp from 'sharp';
|
| 7 |
+
|
| 8 |
+
dotenv.config();
|
| 9 |
+
|
| 10 |
+
const config = {
|
| 11 |
+
maxTextLength: 100,
|
| 12 |
+
viewport: { width: 1920, height: 1080 },
|
| 13 |
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
let browser, page;
|
| 17 |
+
|
| 18 |
+
const utils = {
|
| 19 |
+
async initialize() {
|
| 20 |
+
if (!browser) {
|
| 21 |
+
browser = await chromium.launch({ headless: true });
|
| 22 |
+
const context = await browser.newContext({
|
| 23 |
+
viewport: config.viewport,
|
| 24 |
+
userAgent: config.userAgent
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
await context.route('**/*', (route) => {
|
| 28 |
+
const url = route.request().url();
|
| 29 |
+
if (url.endsWith('.png') || url.endsWith('.jpg') || url.includes('google-analytics')) {
|
| 30 |
+
return route.abort();
|
| 31 |
+
}
|
| 32 |
+
route.continue();
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
page = await context.newPage();
|
| 36 |
+
await page.goto('https://www.bratgenerator.com/', { waitUntil: 'domcontentloaded', timeout: 10000 });
|
| 37 |
+
|
| 38 |
+
try {
|
| 39 |
+
await page.click('#onetrust-accept-btn-handler', { timeout: 2000 });
|
| 40 |
+
} catch { }
|
| 41 |
+
|
| 42 |
+
await page.evaluate(() => setupTheme('white'));
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
|
| 46 |
+
async generateBrat(text) {
|
| 47 |
+
await page.fill('#textInput', text);
|
| 48 |
+
const overlay = page.locator('#textOverlay');
|
| 49 |
+
// Ambil screenshot dalam format PNG
|
| 50 |
+
const pngBuffer = await overlay.screenshot({
|
| 51 |
+
timeout: 3000,
|
| 52 |
+
type: 'png'
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
// Konversi ke WebP menggunakan sharp
|
| 56 |
+
return sharp(pngBuffer)
|
| 57 |
+
.webp({ quality: 80 })
|
| 58 |
+
.toBuffer();
|
| 59 |
+
},
|
| 60 |
+
|
| 61 |
+
async close() {
|
| 62 |
+
if (browser) await browser.close();
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
const app = express();
|
| 67 |
+
app.use(express.json());
|
| 68 |
+
app.use(cors());
|
| 69 |
+
|
| 70 |
+
app.get('*', async (req, res) => {
|
| 71 |
+
try {
|
| 72 |
+
const { q } = req.query;
|
| 73 |
+
if (!q) {
|
| 74 |
+
return res.json({
|
| 75 |
+
name: 'HD Bart Generator API',
|
| 76 |
+
message: 'Parameter q di perlukan',
|
| 77 |
+
version: '2.1.0',
|
| 78 |
+
runtime: {
|
| 79 |
+
os: os.type(),
|
| 80 |
+
platform: os.platform(),
|
| 81 |
+
architecture: os.arch(),
|
| 82 |
+
cpuCount: os.cpus().length,
|
| 83 |
+
uptime: `${os.uptime()} seconds`,
|
| 84 |
+
memoryUsage: `${Math.round((os.totalmem() - os.freemem()) / 1024 / 1024)} MB used of ${Math.round(os.totalmem() / 1024 / 1024)} MB`
|
| 85 |
+
}
|
| 86 |
+
});
|
| 87 |
+
}
|
| 88 |
+
const imageBuffer = await utils.generateBrat(q);
|
| 89 |
+
res.set('Content-Type', 'image/webp');
|
| 90 |
+
res.send(imageBuffer);
|
| 91 |
+
} catch (error) {
|
| 92 |
+
console.error(error);
|
| 93 |
+
res.status(500).json({
|
| 94 |
+
status: false,
|
| 95 |
+
message: 'Error generating image',
|
| 96 |
+
error: process.env.NODE_ENV === 'development' ? error.message : undefined
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
const PORT = process.env.PORT || 7860;
|
| 102 |
+
|
| 103 |
+
app.listen(PORT, async () => {
|
| 104 |
+
console.log(`Server running on port ${PORT}`);
|
| 105 |
+
await utils.initialize();
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
process.on('SIGINT', async () => {
|
| 109 |
+
await utils.close();
|
| 110 |
+
process.exit(0);
|
| 111 |
+
});
|
package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "hd-bart-generator-api",
|
| 3 |
+
"version": "2.1.0",
|
| 4 |
+
"description": "API for generating HD Bart images using Playwright",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"type": "module",
|
| 7 |
+
"scripts": {
|
| 8 |
+
"start": "node app.js",
|
| 9 |
+
"dev": "nodemon app.js"
|
| 10 |
+
},
|
| 11 |
+
"keywords": ["playwright", "express", "bart-generator", "API", "HD"],
|
| 12 |
+
"author": "Putu",
|
| 13 |
+
"license": "MIT",
|
| 14 |
+
"dependencies": {
|
| 15 |
+
"express": "latest",
|
| 16 |
+
"playwright": "latest",
|
| 17 |
+
"cors": "latest",
|
| 18 |
+
"dotenv": "latest",
|
| 19 |
+
"os": "latest",
|
| 20 |
+
"sharp":"latest"
|
| 21 |
+
},
|
| 22 |
+
"devDependencies": {
|
| 23 |
+
"nodemon": "latest"
|
| 24 |
+
}
|
| 25 |
+
}
|