-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (61 loc) · 1.93 KB
/
app.js
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
const fs = require('fs').promises;
const axios = require('axios');
const CUSTOM_TEXT = 'Bismillah Mati';
const URLS_FILE = 'urls.txt';
const REQUEST_DELAY_MS = 100;
class MessageSender {
constructor() {
this.totalMessagesSent = 0;
}
async sendRequest(url, index) {
try {
const response = await axios.get(`${url}${encodeURIComponent(CUSTOM_TEXT)}`);
if (response.status === 200) {
return `Link ${index + 1}: OK`;
} else {
return `Link ${index + 1}: Unexpected status code: ${response.status}`;
}
} catch (error) {
return `Link ${index + 1}: API down!: ${error.message}`;
}
}
async sendMessages(urls) {
console.log(`Sending: "${CUSTOM_TEXT}"`);
const requests = urls.map((url, index) => this.sendRequest(url, index));
const results = await Promise.allSettled(requests);
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(result.value);
} else {
console.error(`Error with Link ${index + 1}: ${result.reason}`);
}
});
this.totalMessagesSent++;
console.log(`Total messages sent: ${this.totalMessagesSent}`);
console.log('='.repeat(30));
}
async readUrlsFromFile() {
try {
const data = await fs.readFile(URLS_FILE, 'utf8');
return data.trim().split('\n').filter(Boolean);
} catch (error) {
throw new Error(`Failed to read URLs from file: ${error.message}`);
}
}
async start() {
try {
const urls = await this.readUrlsFromFile();
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
while (true) {
await this.sendMessages(urls);
await delay(REQUEST_DELAY_MS);
}
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
}
(async () => {
const sender = new MessageSender();
await sender.start();
})();