-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
276 lines (235 loc) · 9.91 KB
/
server.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import express from 'express'
import path from 'path'
import { createServer } from 'http'
import { Server } from 'socket.io'
import os from 'os'
import { parse } from 'node-html-parser';
import { convert } from 'html-to-text';
import fetch from 'node-fetch';
import Helper from './lib/helper.js'
import db from './lib/database.js'
const headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36',
};
async function fetchNewsContent(news) {
if (!news || !news.url) {
throw new Error('Invalid news object or URL');
}
const embeds = [];
const response = await fetch(news.url, { headers });
if (response.status !== 200) {
throw new Error(`Failed to fetch ${news.url}, status code: ${response.status}`);
}
const content = await response.text();
const root = parse(content);
const newsBox = root.querySelector('div.useBox.newsBox');
if (!newsBox) {
throw new Error(`News Box not found in ${news.url}`);
}
const children = newsBox.childNodes.slice(
newsBox.childNodes.findIndex((child) => child.classList?.contains('smallTitleLine')) + 1,
newsBox.childNodes.findIndex((child) => child.classList?.contains('deluxetitle') && child.text === 'Note' && child.text === 'PERHATIAN')
);
const sections = [[null]];
for (const child of children) {
if (child.text === 'Back to Top') continue;
if (child.text === 'Kembali ke atas') continue;
if (child.classList?.contains('deluxetitle') && child.id) {
sections.push([child]);
} else {
sections[sections.length - 1].push(child);
}
}
for (const [head, ...contents] of sections) {
const title = head === null ? news.title : head.text;
const url = head === null ? news.url : `${news.url}#${head.id}`;
const thumbnail = head === null ? { url: news.thumbnail } : undefined;
const section = parse(contents.join(''));
const text = convert(section.toString(), {
wordwrap: false,
formatters: {
markdownLink: (elem, walk, builder, _) => {
const text = elem.children?.filter((child) => child.type === 'text')?.length === 1;
const href = elem.attribs?.href || '';
const link = href.startsWith('//') ? `https:${href}` : href.startsWith('#') ? `${news.url}${href}` : '';
if (text && link) {
builder.startNoWrap();
builder.addLiteral(`[`);
walk(elem.children, builder);
builder.addLiteral(`](`);
builder.addInline(link, { noWordTransform: true });
builder.addLiteral(`)`);
builder.stopNoWrap();
} else {
walk(elem.children, builder);
builder.addInline(link, { noWordTransform: true });
}
},
},
selectors: [
{ selector: 'a', format: 'markdownLink' },
{ selector: 'hr', format: 'skip' },
{ selector: 'img', format: 'skip' },
{ selector: 'button', format: 'skip' },
{ selector: 'table', format: 'dataTable' },
{ selector: 'font', format: 'inlineSurround', options: { prefix: '*', suffix: '*' } },
{ selector: 'span', format: 'inlineSurround', options: { prefix: '*', suffix: '*' } },
{ selector: 'strong', format: 'inlineSurround', options: { prefix: '*', suffix: '*' } },
],
}).replace(/\n{3,}/g, '\n\n');
const description = text.length > 5000 ? `${text.slice(0, 4097)}...` : text;
const images = section.querySelectorAll('img').map((img) => ({ url: img.getAttribute('src') }));
embeds.push({
title,
url,
thumbnail,
description,
image: images.shift(),
category: news.category,
});
for (const image of images) {
embeds.push({ url, image, category: news.category });
}
}
return embeds;
}
function delay(min, max) {
const ms = Math.random() * (max - min) + min;
return new Promise(resolve => setTimeout(resolve, ms));
}
function connect(conn, PORT) {
let app = global.app = express()
app.use(express.json())
let server = global.server = createServer(app)
// let _qr = 'invalid'
// conn.ev.on('connection.update', function appQR({ qr }) {
// if (qr) _qr = qr
// })
app.get(`/`, async (req, res, next) => {
const serverDetails = {
name: "Ely-MD",
version: "3.2.0",
description: "This is details about the server for run ely-md.",
author: "arasea",
system: {
platform: os.platform(),
architecture: os.arch(),
cpuCount: os.cpus().length,
cpuModel: os.cpus()[0].model,
totalMemory: `${(os.totalmem() / (1024 ** 3)).toFixed(2)} GB`,
freeMemory: `${(os.freemem() / (1024 ** 3)).toFixed(2)} GB`,
uptime: `${(os.uptime() / 3600).toFixed(2)} hours`,
}
};
res.json(serverDetails);
})
app.post('/toramnewsupdate', async (req, res) => {
const data = req.body.content;
if (Array.isArray(data) && data.length > 0) {
res.status(200).send('Data telah diterima')
try {
let combinedContents = [];
for (const item of data) {
const date = item.date;
const news = {
url: item.url,
title: item.title,
thumbnail: item.thumbnail,
category: item.category,
};
try {
const content = await fetchNewsContent(news);
let messageContent = `*${date}* *${news.category}*\n\n`;
messageContent += content
.map(c => {
if (!c.title) {
return null
}
const description = c.description ? `${c.description}` : '';
return `*${c.title}*${description}`;
})
.filter(item => item !== null)
.join('\n-------------------------------\n');
while (messageContent.length > 20000) {
combinedContents.push(messageContent.substring(0, 20000) + `....\n_*This is too much character for this message. Go to ${news.url} for more info!*_`);
messageContent = messageContent.substring(20000);
}
combinedContents.push(messageContent);
} catch (error) {
console.error(`Error fetching content for ${news.url}:`, error);
}
}
for (const [chatId, chatInfo] of Object.entries(db.data.chats)) {
if (chatInfo.newstoram) {
for (const content of combinedContents) {
await conn.sendMessage(chatId, { text: content })
await delay(2000, 5500)
}
}
}
} catch (error) {
console.error('Error processing the content:', error);
res.status(500).send('Error processing the content');
}
} else {
res.status(400).send('Invalid data')
}
})
app.post(`/botupdate`, async (req, res) => {
const payload = req.body;
let txt = `*New Commit Push!!!*\n\n`
if (payload && payload.commits) {
payload.commits.forEach(commit => {
txt += `*Author:* arasea\n`
txt += `*Message:* ${commit.message}`
if (commit.added && commit.added.length > 0) {
txt += `\n\n*Added files:* ${commit.added.join(`\n`)}`;
}
if (commit.modified && commit.modified.length > 0) {
txt += `\n\n*Modified files:* ${commit.modified.join(`\n`)}\n`;
}
if (commit.removed && commit.removed.length > 0) {
txt += `\n\n*Removed files:* ${commit.removed.join(`\n`)}`;
}
})
try {
await conn.sendMessage(`120363346650560029@g.us`, { text: txt })
} catch (err) {
console.error(err.message);
}
res.status(200).send('Webhook received and processed');
} else {
res.status(400).send('Invalid payload');
}
})
app.use(express.static(path.join(Helper.__dirname(import.meta.url), 'views')))
let io = new Server(server)
io.on('connection', socket => {
let { unpipeEmit } = pipeEmit(conn, socket, 'conn-')
socket.once('disconnect', unpipeEmit)
})
server.listen(PORT, () => {
console.log('App listened on port', PORT)
if (opts['keepalive']) keepAlive()
})
}
function pipeEmit(event, event2, prefix = '') {
let old = event.emit
event.emit = function (event, ...args) {
old.emit(event, ...args)
event2.emit(prefix + event, ...args)
}
return {
unpipeEmit() {
event.emit = old
}
}
}
function keepAlive() {
const url = `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
if (/(\/\/|\.)undefined\./.test(url)) return
setInterval(() => {
fetch(url).catch(console.error)
}, 5 * 1000 * 60)
}
export default connect