forked from zennn08/brat-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (111 loc) · 3.05 KB
/
Copy pathapp.js
File metadata and controls
133 lines (111 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require('dotenv').config();
const express = require('express');
const morgan = require('morgan');
const { chromium } = require('playwright');
const path = require('path');
const os = require('os');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(morgan('common'));
// Buat browser instance
let browser;
const launchBrowser = async () => {
browser = await chromium.launch(); // Browser headless
}
launchBrowser();
async function fetchCount() {
try {
return (await axios.get("https://api.counterapi.dev/v1/aqul/brat/up")).data?.count || 0
} catch {
return 0
}
}
app.use('*', async (req, res) => {
const text = req.query.text
const background = req.query.background
const color = req.query.color
const hit = fetchCount()
if (!text) return res.status(200).json({
author: 'zennn08 (aqul)',
repository: {
github: 'https://github.com/zennn08/brat-api/'
},
hit: await hit,
message: "Parameter `text` diperlukan",
runtime: {
os: os.type(),
platform: os.platform(),
architecture: os.arch(),
cpuCount: os.cpus().length,
uptime: `${os.uptime()} seconds`,
memoryUsage: `${Math.round((os.totalmem() - os.freemem()) / 1024 / 1024)} MB used of ${Math.round(os.totalmem() / 1024 / 1024)} MB`
}
})
if (!browser) {
await launchBrowser();
}
const context = await browser.newContext({
viewport: {
width: 1536,
height: 695
}
});
const page = await context.newPage();
const filePath = path.join(__dirname, './site/index.html');
// Open https://www.bratgenerator.com/
await page.goto(`file://${filePath}`);
// Click on <div> #toggleButtonWhite
await page.click('#toggleButtonWhite');
// Click on <div> #textOverlay
await page.click('#textOverlay');
// Click on <input> #textInput
await page.click('#textInput');
// Fill "sas" on <input> #textInput
await page.fill('#textInput', text);
await page.evaluate((data) => {
if (data.background) {
$('.node__content.clearfix').css('background-color', data.background);
}
if (data.color) {
$('.textFitted').css('color', data.color);
}
}, { background, color });
const element = await page.$('#textOverlay');
const box = await element.boundingBox();
res.set('Content-Type', 'image/png');
res.end(await page.screenshot({
clip: {
x: box.x,
y: box.y,
width: 500,
height: 500
}
}));
await context.close();
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Menangani penutupan server
const closeBrowser = async () => {
if (browser) {
console.log('Closing browser...');
await browser.close();
console.log('Browser closed');
}
};
process.on('SIGINT', async () => {
console.log('SIGINT received');
await closeBrowser();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('SIGTERM received');
await closeBrowser();
process.exit(0);
});
process.on('exit', async () => {
console.log('Process exiting');
await closeBrowser();
});