-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (43 loc) · 1.18 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
'use strict';
var nodemailer = require('nodemailer'),
cheerio = require('cheerio'),
request = require('request'),
$,
transporter,
url = 'http://store.apple.com/ie/browse/home/specialdeals/mac/macbook_pro/15';
function scrapAndSendEmail() {
// The first parameter is our URL
// The callback function takes 3 parameters,
// an error, response status code and the html
request(url, function(err, response, html) {
// check for any error
if(err) {
console.error(err);
} else {
// we will assign the return html to jQuery function
$ = cheerio.load(html);
// scrap all products under box-content div
var product = $('.box-content').find('.product');
if (product) {
console.log('Product exits, please check ' + url);
sendEmail();
}
}
});
function sendEmail() {
transporter = nodemailer.createTransport({
service: 'gmail', // e.g. yahoo...
auth: {
user: '[email protected]',
pass: 'xyz'
}
});
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Apple sells 15" macbook_pro',
text: 'Product exits, please check ' + url
});
}
}
setInterval(scrapAndSendEmail, 21600000);