-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
176 lines (155 loc) · 4.74 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const fs = require('fs');
const request = require('request');
const cheerio = require('cheerio');
const nodemailer = require('nodemailer');
const interval = 10 * 60 * 1000 //check every 10 mins
const maxTimeWithoutEmail = 24 * 60 * 60 * 1000; //24h
var lastSent = Date.now();
var happyRideBikes = {}
var canyonBikes = {}
var transporter;
function init() {
const settings = JSON.parse(fs.readFileSync('settings.json', 'utf8'));
const gmailAddress = settings.email
const gmailPassword = settings.password
const canyonUrls = settings.canyon_urls
const happyRideUrls = settings.happyride_urls
for (url of happyRideUrls) {
happyRideBikes[url] = []
}
for (url of canyonUrls) {
canyonBikes[url] = []
}
transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailAddress,
pass: gmailPassword
}
});
transporter.destination = gmailAddress
}
function scrapeHappyRide(url, oldResults, mailSender) {
console.log(new Date().toGMTString() + ' Scraping HappyRide');
request(url, function(error, response, html) {
if (!error) {
var $ = cheerio.load(html);
var salesTable = $('.sales-table')
if (salesTable.length == 0) {
console.log('No results')
} else {
var newBikes = []
$('.col-2').each(function(i, elem){
var name = $(elem).children().first().text()
var price = $(elem).children().last().text()
newBikes[i] = `${name} (${price})`
})
if (newBikes.length > oldResults.length) {
console.log("new happyRideBikes found: ", newBikes)
body = newBikes.join('\n') + "\nURL: " + url
mailContent = {
from: mailSender.destination,
to: 'hakan@maclean.se',
subject: 'Nya cyklar på HappyRide',
text: body
};
mailSender(mailContent)
}
oldResults.length = 0;
for (result in newBikes) {
oldResults.push(result)
}
}
}
})
}
function scrapeCanyon(url, oldResults, mailSender) {
console.log(new Date().toGMTString() + ' Scraping Canyon.com');
var Horseman = require('node-horseman');
var horseman = new Horseman({
injectJquery: true,
ignoreSSLErrors: true,
webSecurity: false,
loadImages: false,
});
horseman
.open(url)
.waitForSelector('.productGrid__list')
.html('body')
.then((html) => {
var newCanyonBikes = []
console.log('trying to parse')
var $ = cheerio.load(html);
const heading = $('.heading--1')
if (heading.text().includes('Campaign')) {
console.log('No hits, got Campaign sight')
canyonBikes = newCanyonBikes;
return
}
var products = $('.productGrid__list')
$('.productGrid__listItem').each((i, elm) => {
const name = $(elm).find('.productTile__productName').first().text().trim()
const price = $(elm).find('.productTile__size').first().text().trim()
newCanyonBikes.push(`${name} (${price})`)
});
console.log("bikes found: " + newCanyonBikes)
if (newCanyonBikes.length > oldResults.length) {
console.log("new canyon bikes found: ", newCanyonBikes)
body = newCanyonBikes.join('\n') + "\nURL: " + url
mailContent = {
from: mailSender.destination,
to: 'hakan@maclean.se',
subject: 'Nya cyklar på Canyon',
text: body
};
mailSender(mailContent)
}
oldResults.length = 0;
for (result in newCanyonBikes) {
oldResults.push(result)
}
})
.close()
}
function arraysEqual(a1,a2) {
return JSON.stringify(a1)==JSON.stringify(a2);
}
function sendEmail(mailContent) {
if (mailContent != null) {
console.log("sending email: ", mailContent)
lastSent = Date.now();
transporter.sendMail(mailContent, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
mailContent = null
}
});
}
}
function sendStatusEmailIfNeeed(transporter) {
if ((Date.now() - lastSent) > maxTimeWithoutEmail) {
var body = 'HappyRide bikes:\n' + happyRideBikes.join('\n')
+ "\nCANYON bikes:\n" + canyonBikes.join('\n');
var mailContent = {
from: transporter.destination,
to: 'hakan@maclean.se',
subject: 'HappyRideScraper online',
text: body
};
transporter(mailContent)
}
}
function scrapePages() {
for (const [url, results] of Object.entries(happyRideBikes)) {
scrapeHappyRide(url, results, sendEmail)
}
for (const [url, results] of Object.entries(canyonBikes)) {
scrapeCanyon(url, results, sendEmail)
}
sendStatusEmailIfNeeed(transporter);
}
init()
scrapePages()
setInterval(scrapePages, interval)