-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (54 loc) · 2.8 KB
/
main.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
const TelegramBot = require('node-telegram-bot-api');
const token = '760978574:AAGPNOfbFeo7X07I2MFJ8s02kiurNmisQUM';
const bot = new TelegramBot(token, {polling: true});
const groupId = '-351595481';
const parcer = require('node-html-parser');
const axois = require('axios');
const firebase = require("firebase");
firebase.initializeApp({
serviceAccount: "./serviceAccount.json",
databaseURL: 'https://rent-manager-bot.firebaseio.com/'
});
const targetAddress = 'Берсеневская набережная, 4, с3, Москва, Россия';
const maxPrice = 40000;
const URL = `https://www.avito.ru/moskva/kvartiry/sdam/na_dlitelnyy_srok?cd=1&pmax=${maxPrice}&pmin=0&user=1&s=104`;
setTimeout(syncOffers, 2, 60 * 60 * 1000);
bot.onText(/\/echo/, msg => {
bot.sendMessage(msg.chat.id, msg.chat.id);
});
function syncOffers() {
firebase.database().ref("offersIdArray").once("value", function (snapshot) {
const offersIdArray = snapshot.val() || [];
axois.get(URL).then(res => {
const root = parcer.parse(res.data);
const data = (root).querySelectorAll('.item__line');
const result = data.map(home => {
const
descriptionNode = home.querySelector('.item-description-title-link'),
priceNode = home.querySelector('.price'),
addressNode = home.querySelector('.item-address__string');
let link, title, price, address, route;
descriptionNode.rawAttrs.split('\n').forEach((val) => {
let match;
if ((match = val.match(/href="(.+)"/)) != null) link = `https://www.avito.ru${match[1]}`;
if ((match = val.match(/title="(.+)"/)) != null) title = match[1]
});
const id = link.slice(-10)
if (offersIdArray.includes(id)) return null;
price = priceNode.childNodes[0].rawText.replace('\n', '');
address = addressNode.childNodes[0].rawText.replace('\n', '');
route = (`https://www.google.ae/maps/dir/${address}/${targetAddress}`)
.replace(/ /g, '%20').replace(/,/g, '%2C');
return {id, link, title, price, address, route}
}).filter(val => val != null).map(({id, link, title, price, address, route}) => {
bot.sendMessage(groupId, `${title}: ${price} Рублей в месяц\nАдрес: ${address}`, {
reply_markup: {
inline_keyboard: [[{text: 'Ссылка на авито', url: link}], [{text: 'Маршрут', url: route}]]
}
});
return id
});
firebase.database().ref('/').update({offersIdArray: offersIdArray.concat(result)})
})
})
}