-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrf.js
131 lines (115 loc) · 3.71 KB
/
brf.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
const ax = require('axios');
const fs = require('fs');
const nodemailer = require('nodemailer');
const colors = require('colors');
const credentials = require('./credentials');
const recipients = require('./recipients');
let apartments;
let queuedMails = [];
function logger(log) {
console.log(`${getCurrentTime()} ${log}`);
}
function addZeroBefore(n) {
return (n < 10 ? '0' : '') + n;
}
function getCurrentTime(){
let now = new Date();
let h = addZeroBefore(now.getHours());
let m = addZeroBefore(now.getMinutes());
let s = addZeroBefore(now.getSeconds());
let D = addZeroBefore(now.getDate());
let M = addZeroBefore(now.getMonth() + 1);
return `[${h}:${m}:${s} - ${D}/${M}]`;
}
fs.readFile('./data.json', 'utf8', (err, data) => {
if (err) {
apartments = [];
pingHyres(false);
return logger(`data.json not found, creating it..`);
}
apartments = JSON.parse(data);
});
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: 'true',
auth: {
user: credentials.user,
pass: credentials.pass
}
});
function mailOptions(subject, text) {
return {
from: credentials.user,
to: recipients.map((recipient) => recipient.email += ','),
subject: subject,
html: text
}
}
function pingHyres(sendMail = true) {
ax.get("https://bostad.stockholm.se/Lista/AllaAnnonser")
.then((resp) => {
let data = resp.data;
let dDidChange = false;
for(d of data) {
if (d.Student || d.Ungdom || d.BostadsSnabben) {
if (d.BostadsSnabben) {
transporter.sendMail(mailOptions('BOSTADSSNABBEN!1!', `<a href="https://bostad.stockholm.se${d.Url}">Ny hyresrätt i ${d.Stadsdel}</a> \n <p>Våning: ${d.Vaning} Rum: ${d.AntalRum} m2: ${d.Yta} Hyra: ${d.Hyra} Bostadssnabben: ${d.BostadsSnabben ? 'Ja' : 'Nej'} Ungdom: ${d.Ungdom ? 'Ja' : 'Nej'} Student: ${d.Student ? 'Ja' : 'Nej'}</p>`), (error, info) => {
if (error) {
logger(error);
queuedMails.push(mail);
} else {
logger('Email sent: ' + info.response);
}
});
} else {
if (!apartments.find(x => x.AnnonsId === d.AnnonsId)) {
dDidChange = true;
apartments.push(d);
logger(`Found new rental. ID: ${d.AnnonsId}`);
if (sendMail) {
queuedMails.push(`<a href="https://bostad.stockholm.se${d.Url}">Ny hyresrätt i ${d.Stadsdel}</a> \n <p>Våning: ${d.Vaning} Rum: ${d.AntalRum} m2: ${d.Yta} Hyra: ${d.Hyra} Bostadssnabben: ${d.BostadsSnabben ? 'Ja' : 'Nej'} Ungdom: ${d.Ungdom ? 'Ja' : 'Nej'} Student: ${d.Student ? 'Ja' : 'Nej'}</p>`);
}
}
}
}
}
!dDidChange && logger('No new results'.red);
save();
})
.catch((err) => {
logger(err);
});
}
function save() {
fs.writeFile('./data.json', JSON.stringify(apartments), (err) => {
if(err){
return logger(err);
}
})
}
function trySendQueuedMail(){
if(queuedMails.length === 1) {
let mail = queuedMails.shift();
transporter.sendMail(mailOptions(`Ny hyresrätt`, mail), (error, info) => {
if (error) {
logger(error);
queuedMails.push(mail);
} else {
logger('Email sent: ' + info.response);
}
});
}else if(queuedMails.length > 1){
let mails = queuedMails.join('\n');
transporter.sendMail(mailOptions(`Nya hyresrätter`, mails), (error, info) => {
if (error) {
logger(error);
} else {
logger('Email sent: ' + info.response);
queuedMails = [];
}
});
}
}
logger(`=======> Starting script <=======`);
setInterval(pingHyres, 15000);
setInterval(trySendQueuedMail, 5000);