-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (43 loc) · 1.63 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
import creds from './creds.js'
const puppeteer = require('puppeteer')
const nodemailer = require('nodemailer');
const fs = require('fs');
const buffer = new Buffer.alloc(1024);
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: creds.user,
pass: creds.pass,
},
});
const buildEmailObject = (newTimestamp) => {
return {
from: `"Second Use Mailer" <${creds.user}>`, // sender address
to: creds.recipientEmail, // list of receivers
subject: "New Stuff!", // Subject line
text: `Second Use ${newTimestamp} https://www.seconduse.com/inventory/`, // plain text body
html: `<b>Second Use ${newTimestamp} https://www.seconduse.com/inventory/</b>`, // html body
}
}
async function compareTimestamps(newTimestamp) {
let oldTimestamp = fs.readFileSync('./seconduseTimestamp.txt', {encoding:'utf8', flag:'r'})
if (oldTimestamp !== newTimestamp) {
transporter.sendMail(buildEmailObject(newTimestamp)).then(info => {
console.log({info});
}).catch(console.error)
fs.writeFileSync('./seconduseTimestamp.txt', newTimestamp)
console.log("Updated timestamp")
} else {
console.log("No new updates")
}
}
async function scrape() {
const browser = await puppeteer.launch({})
const page = await browser.newPage()
await page.goto('https://www.seconduse.com/inventory/')
const element = await page.waitForSelector(".timestamp > p")
const timestamp = await page.evaluate(element => element.textContent, element)
const isUpdated = compareTimestamps(timestamp)
browser.close()
}
scrape()