-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
259 lines (217 loc) · 7.9 KB
/
node.js
File metadata and controls
259 lines (217 loc) · 7.9 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const CONFIG_PATH = path.join(__dirname, 'config.json');
let CONFIG = {};
try {
CONFIG = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
} catch (err) {
console.error('Failed to load config.json:', err.message);
process.exit(1);
}
let successfulPops = 0;
let failedPops = 0;
let totalPopsSent = 0;
let stopped = false;
let page = null;
let loopTimer = null;
let startTime = Date.now();
const STATS_FILE = path.join(__dirname, 'stats.json');
function log(message) {
const time = new Date().toISOString().replace('T', ' ').split('.')[0];
console.log(`[${time} UTC] ${message}`);
}
function loadStats() {
if (!CONFIG.ENABLE_STATS) return {};
if (fs.existsSync(STATS_FILE)) {
try {
return JSON.parse(fs.readFileSync(STATS_FILE, 'utf8'));
} catch (error) {
log(`Error loading stats file: ${error.message}. Starting with empty stats.`);
return {};
}
}
return {};
}
function saveStats(country, update = {}) {
if (!CONFIG.ENABLE_STATS) return;
const stats = loadStats();
if (!stats[country]) {
stats[country] = {
totalPopsSent: 0,
successfulPops: 0,
failedPops: 0,
firstPopTime: new Date().toISOString(),
lastPopTime: null
};
}
const c = stats[country];
c.totalPopsSent += update.totalPopsSent || 0;
c.successfulPops += update.successfulPops || 0;
c.failedPops += update.failedPops || 0;
c.lastPopTime = new Date().toISOString();
try {
fs.writeFileSync(STATS_FILE, JSON.stringify(stats, null, 2));
} catch (error) {
log(`Error saving stats: ${error.message}`);
}
}
async function runBot() {
try {
startTime = Date.now();
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--disable-gpu'
]
});
page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36');
await page.setViewport({ width: 1280, height: 800 });
log("🧭 Navigating to [popcat.click]...");
await page.goto('https://popcat.click/', { waitUntil: 'networkidle2', timeout: 60000 });
await page.evaluate((country) => {
document.cookie = `country=${country}; path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT`;
}, CONFIG.COUNTRY);
await page.reload({ waitUntil: 'networkidle2' });
console.clear();
const bannerWidth = 50;
const center = (text) => {
const pad = Math.floor((bannerWidth - text.length) / 2);
return ' '.repeat(Math.max(0, pad)) + text;
};
log('='.repeat(bannerWidth));
log(center('POPBOT NODE.JS (v1.0.0)'));
log(center('A simple NodeJS bot to automate pops in popcat.click'));
log(center('A project by AstrooKai - Made with AI'));
log('='.repeat(bannerWidth));
log("🚀 PopBot Started!");
log(`🌍 Country set to "${CONFIG.COUNTRY}"`);
log("🌐 Page loaded. Monitoring network...");
startPopLoop();
} catch (error) {
log(`🚨 CRITICAL ERROR in main function: ${error.message}`);
console.error(error.stack);
stopped = true;
await gracefullyStopBot();
process.exit(1);
}
}
async function sendPops() {
if (stopped) return;
try {
const responsePromise = page.waitForResponse(
(res) =>
res.url().includes('https://stats.popcat.click/pop') &&
res.status() === 201 &&
res.request().method() === 'POST',
{ timeout: 10000 } // extend timeout if needed
);
for (let i = 0; i < CONFIG.POP_AMOUNT; i++) {
if (stopped) break;
await page.click('div.cat-img');
}
const response = await responsePromise;
totalPopsSent += CONFIG.POP_AMOUNT;
log(`✅ ${CONFIG.POP_AMOUNT} pops successfully sent to ${CONFIG.COUNTRY}! (Total Pops Sent: ${totalPopsSent})`);
try {
const body = await response.text();
log(`[DEBUG]: HTTP 201 Created - ${body}`);
} catch (parseError) {
log(`⚠️ Could not parse response body: ${parseError.message}`);
}
saveStats(CONFIG.COUNTRY, {
totalPopsSent: CONFIG.POP_AMOUNT
});
if (CONFIG.MAX_POPS !== null && totalPopsSent >= CONFIG.MAX_POPS) {
log(`🛑 Max successful pops (${CONFIG.MAX_POPS}) reached. Stopping bot...`);
stopped = true;
await gracefullyStopBot();
}
} catch (error) {
log(`Error during sendPops: ${error.message}`);
if (error.message.includes('Timeout') || error.message.includes('Target closed') || error.message.includes('Session closed')) {
log('Browser or page closed or request failed. Stopping bot.');
stopped = true;
await gracefullyStopBot();
}
}
}
function startPopLoop() {
if (stopped) return;
const sendAndLog = async () => {
if (stopped) return;
log(`🐾 Sending ${CONFIG.POP_AMOUNT} pops to [stats.popcat.click]...`);
log(`⏳ Waiting for ${CONFIG.INTERVAL_MS / 1000} seconds before sending the next pops...`);
await sendPops();
};
sendAndLog();
loopTimer = setInterval(() => {
sendAndLog();
}, CONFIG.INTERVAL_MS);
}
async function gracefullyStopBot() {
stopped = true;
if (loopTimer) {
clearInterval(loopTimer);
loopTimer = null;
}
const elapsedMs = Date.now() - startTime;
const elapsedSec = Math.floor(elapsedMs / 1000);
const elapsedMin = Math.floor(elapsedSec / 60);
const elapsedStr = elapsedMin > 0
? `${elapsedMin}m ${elapsedSec % 60}s`
: `${elapsedSec}s`;
log("========== 📊 Stats Summary ==========");
log(`Total Pops Sent: ${totalPopsSent}`);
log(`Successful Pops: ${successfulPops}`);
log(`Failed Pops: ${failedPops}`);
log(`Elapsed Time: ${elapsedStr}`);
log("======================================");
log("Bot is stopping. Cleaning up...");
if (page) {
try {
log("Closing browser...");
await page.browser().close();
page = null;
log("Browser closed.");
} catch (closeError) {
log(`Error closing browser: ${closeError.message}`);
}
}
log("PopBot stopped.");
process.exit(0);
}
process.on('SIGINT', async () => {
log('SIGINT received. Attempting graceful shutdown...');
await gracefullyStopBot();
});
process.on('SIGTERM', async () => {
log('SIGTERM received. Attempting graceful shutdown...');
await gracefullyStopBot();
});
process.on('uncaughtException', async (error) => {
log(`UNCAUGHT EXCEPTION: ${error.message}`);
console.error(error.stack);
if (!stopped) {
await gracefullyStopBot();
} else {
process.exit(1);
}
});
process.on('unhandledRejection', async (reason) => {
log(`UNHANDLED REJECTION: ${reason instanceof Error ? reason.message : reason}`);
if (reason instanceof Error) console.error(reason.stack);
if (!stopped) {
await gracefullyStopBot();
} else {
process.exit(1);
}
});
runBot();