-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
141 lines (119 loc) · 4.3 KB
/
index.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
const cheerio = require('cheerio');
const { Webhook, MessageBuilder } = require('discord-webhook-node');
const puppeteer = require('puppeteer');
const locateChrome = require('locate-chrome');
require('dotenv').config()
const sentry = new Webhook(process.env.DISCORD_WEBHOOK_URL);
const extractTicketPart = (url) => {
const regex = /\/([A-Z]{3}-[A-Z]{3})/;
const match = url.match(regex);
return match ? match[1] : '';
};
const getTransportationType = (url) => {
if (url.includes('bus')) {
return '🚌 Bus';
} else if (url.includes('flights')) {
return '✈️ Plane';
} else if (url.includes('train')) {
return '🚂 Train';
} else {
return 'Transportation';
}
};
const getDepartingDate = (url) => {
const regex = /departing=(\d{4}-\d{2}-\d{2})/;
const match = url.match(regex);
return match ? match[1] : 'Unknown';
};
const getSeatsCount = ($, url) => {
const classSelector1 = '.text-grays-400.text-2.mt-2';
const classSelector2 = url.includes('flights') ? '.text-2.mt-1.text-danger-400' : '.mt-2.text-2.text-danger-400';
const elements1 = $(classSelector1);
const elements2 = $(classSelector2);
const elements = elements1.add(elements2);
let count = 0;
elements.each((index, element) => {
const text = $(element).text().trim();
const number = parseInt(text);
if (!isNaN(number)) {
count += number;
}
});
return count;
};
const ticketFinder = async (urls) => {
const executablePath = await locateChrome();
const browser = await puppeteer.launch({ headless: true, executablePath });
try {
for (const url of urls) {
const page = await browser.newPage();
await page.goto(url);
await page.waitForTimeout(5000);
const html = await page.content();
const $ = cheerio.load(html);
let ticketText = 'انتخاب بلیط';
let ignoreText = 'پرواز های تکمیل ظرفیت';
if (url.includes('flights')) {
ticketText = 'انتخاب پرواز';
ignoreText = 'پرواز های تکمیل ظرفیت';
}
const disabledButtons = $('button.is-disabled:contains("انتخاب پرواز")');
const disabledCount = disabledButtons.length;
const ticketCount = html.split(ticketText).length - 1 - disabledCount;
let availableCount = 0;
if (ticketCount > 0) {
const ticketsParent = $('#app > div.wrapper > main > div > div > section');
const availableCards = ticketsParent.children().nextAll('.available-card');
availableCount = availableCards.length;
// Ignore 'انتخاب پرواز' after 'پرواز های تکمیل ظرفیت'
let ignore = false;
availableCards.each((index, element) => {
const cardText = $(element).text();
const h3Element = $(element).find('h3');
const h3Text = h3Element.text().trim();
if (h3Text === ignoreText) {
ignore = true;
}
if (!ignore) {
availableCount++;
}
});
}
if (ticketCount > 0) {
const ticketPart = extractTicketPart(url);
const departingDate = getDepartingDate(url);
const title = `${getTransportationType(url)} Ticket Alert (${ticketPart}) - ${departingDate}`;
const embed = new MessageBuilder()
.setTitle(title)
.setURL(url)
.setColor('#00b0f4')
.addField('✉️ Available Number ', ` ${ticketCount}`, true)
.addField('🪑 Available Seats Count', ` ${getSeatsCount($, url)}`, true, url.includes('flights'));
sentry.send(embed);
} else if (ticketCount === 0) {
const ticketPart = extractTicketPart(url);
const departingDate = getDepartingDate(url);
const title = `${getTransportationType(url)} Ticket Alert (${ticketPart}) - ${departingDate}`;
const embed = new MessageBuilder()
.setTitle(title)
.setURL(url)
.setColor('#ff0000')
.setDescription(`🔴 No tickets available for ${ticketPart}`);
sentry.send(embed);
}
await page.close();
}
} catch (error) {
console.error(error);
} finally {
await browser.close();
}
};
// Example usage with multiple URLs
const urls = [
'example.com'
];
setInterval(() => {
console.log('Running ticket finder...');
ticketFinder(urls);
}, 10000);