-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
128 lines (111 loc) · 3.22 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
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
const { Client, Events, GatewayIntentBits } = require('discord.js');
const puppeteer = require('puppeteer-extra');
const pluginStealth = require('puppeteer-extra-plugin-stealth');
const {
checkAndSaveCookies,
parseHtmlAndExtractData,
loadCookiesFromFile,
saveDataToFile,
loadDataFromFile,
loadUserAgentFromFile,
} = require('./helpers.js');
require('dotenv').config();
puppeteer.use(pluginStealth());
const CHECK_INTERVAL = 3 * 60 * 1000;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates,
],
});
client.once(Events.ClientReady, (c) => {
console.log(
`[${new Date().toLocaleString('tr-TR', {
timeZone: 'Europe/Istanbul',
})}]: ${c.user.tag} is ready!`
);
});
client.login(process.env.DISCORD_TOKEN);
const main = async () => {
let previousData = await loadDataFromFile();
await checkAndSaveCookies();
try {
const cookies = await loadCookiesFromFile();
const userAgent = await loadUserAgentFromFile();
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox'],
executablePath: '/usr/bin/chromium-browser',
});
const page = await browser.newPage();
await page.setUserAgent(userAgent);
if (cookies) {
await page.setCookie(...cookies);
}
await page.goto(process.env.CATEGORY_URL);
const html = await page.content();
const newData = parseHtmlAndExtractData(html);
if (JSON.stringify(newData) !== JSON.stringify(previousData)) {
saveDataToFile(newData);
if (previousData) {
const changes = newData.filter((item) => {
return !previousData.some((i) => {
return i.url === item.url;
});
});
changes.forEach((change) => {
const embed = {
title: change.title,
url: 'https://www.sahibinden.com' + change.url,
thumbnail: {
url: change.imageUrl,
},
fields: [
{
name: 'Metrekare',
value: change.squareMeter,
inline: true,
},
{
name: 'Oda Sayısı',
value: change.roomCount,
inline: true,
},
{
name: 'Fiyat',
value: change.price,
inline: true,
},
{
name: 'Tarih',
value: change.date.replace(/(\r\n|\n|\r)/gm, ''),
inline: true,
},
{
name: 'Konum',
value: change.location,
inline: true,
},
],
timestamp: new Date().toISOString(),
footer: {
text: 'Emlakçı Leblebi Sunar',
},
};
client.channels.cache
.get(process.env.DISCORD_CHANNEL_ID)
.send({ embeds: [embed] });
});
}
} else {
console.log('No changes in data.');
}
await browser.close();
} catch (error) {
console.error('Failed to get data from website.');
}
};
main();
setInterval(main, CHECK_INTERVAL);