-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (114 loc) · 3.29 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
import puppeteer from 'puppeteer';
import TelegramBot from 'node-telegram-bot-api';
import winston from 'winston';
import cron from 'node-cron';
const hyundaiHost = 'https://showroom.hyundai.ru/';
const tgToken = 'SOME_TELEGRAM_TOKEN';
const tgChannelId = 'SOME_TELEGRAM_CHANNEL_ID';
const isProduction = process.env.NODE_ENV === 'production';
const bot = new TelegramBot(tgToken);
const logger = winston.createLogger({
transports: [
new winston.transports.File({
filename: './log.txt',
}),
],
});
start();
async function start() {
exec();
cron.schedule('* * * * *', () => {
exec();
});
}
async function exec() {
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
],
});
const page = await browser.newPage();
await setBlockingOnRequests(page);
try {
await page.goto(hyundaiHost, { waitUntil: 'networkidle2' });
} catch (error) {
await createErrorReport(page, 'no-page', 'Ошибка посещения страницы', error);
await page.close();
await browser.close();
return;
}
try {
await page.waitForSelector('#cars-all .car-columns', { timeout: 1000 });
const carsCount = (await page.$$('.car-item__wrap')).length;
const timestamp = new Date().toTimeString();
const message = `${pluralize(carsCount, 'Доступна', 'доступно', 'доступно')} ${carsCount} ${pluralize(carsCount, 'машина', 'машины', 'машин')} в ${timestamp}`;
if (isProduction) {
bot.sendMessage(tgChannelId, message);
}
logger.info(message);
} catch (error) {
await createErrorReport(page, 'no-cars', 'Ошибка поиска машин', error);
await page.close();
await browser.close();
}
}
async function createErrorReport(page, type, message, techError) {
const timestamp = new Date().toTimeString();
logger.error(`${message} в ${timestamp}`, techError);
const carListContainer = await page.$('#main-content');
if (carListContainer) {
await carListContainer.screenshot({
path: `${type}-${timestamp}.jpeg`,
type: 'jpeg',
quality: 1,
});
} else {
logger.error(`Не могу сделать скриншот отсутствия автомобилей в ${timestamp}`, techError);
}
}
async function setBlockingOnRequests(page) {
await page.setRequestInterception(true);
page.on('request', (req) => {
if (req.resourceType() === 'image'
|| req.resourceType() === 'media'
|| req.resourceType() === 'font'
|| req.resourceType() === 'stylesheet'
|| req.url()
.includes('yandex')
|| req.url()
.includes('nr-data')
|| req.url()
.includes('rambler')
|| req.url()
.includes('criteo')
|| req.url()
.includes('adhigh')
|| req.url()
.includes('dadata')
) {
req.abort();
} else {
req.continue();
}
});
}
function pluralize(n, one, few, many) {
const selectedRule = new Intl.PluralRules('ru-RU').select(n);
switch (selectedRule) {
case 'one': {
return one;
}
case 'few': {
return few;
}
default: {
return many;
}
}
}