-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (133 loc) · 7.75 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
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
const puppeteer = require('puppeteer');
const nodemailer = require('nodemailer');
const fs = require('fs');
const data = require('./data.json');
const url = "https://aru.ac.uk/news";
var transporter = nodemailer.createTransport({
service: data.service,
auth: {
user: data.email,
pass: data.password
}
});
var date = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var monthName = month[date.getMonth()];
var currentMonth = (date.getMonth() + 1).toString();
const getDaysInMonth = date => new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
var numberOfDaysInCurrentMonth = getDaysInMonth(new Date(date.getFullYear(), date.getMonth()));
if (date.getDate() !== numberOfDaysInCurrentMonth) return console.log("Today's not the last day of the month (" + numberOfDaysInCurrentMonth + " " + monthName +")");
var mailOptions = {
from: data.sender,
to: data.receiver,
subject: `What's been going on - `+ monthName,
html: ''
};
var html = fs.readFileSync('base.html','utf8')
mailOptions.html += html;
mailOptions.html += `<h1>Read ` + monthName + ` stories</h1><article class="clearfix">`;
mailOptions.html += `<table style="width: 100%; border-collapse: collapse; border-bottom: rgb(7, 41, 115); padding: 0px;" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="width: 100%; border-bottom: 2px solid rgb(7, 41, 115); padding-bottom: 10px;" data-ogsb="rgb(255, 255, 255)"></td></tr></tbody></table>`;
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle2'});
await page.waitForSelector("h1");
const title = await page.evaluate(() => Array.from(document.querySelectorAll('a[itemprop=url]'), element => element.textContent));
const description = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--listing__description]'), element => element.textContent));
const date = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--date]'), element => element.textContent)); // 31 July 2020
const dateFormat = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--date]'), element => element.getAttribute('content'))); // 31-07-2020
const href = await page.evaluate(() => Array.from(document.querySelectorAll('a[itemprop=url]'), element => element.getAttribute('href')));
const imgLink = await page.evaluate(() => Array.from(document.querySelectorAll('img[class=image--float-right]'), element => element.getAttribute('src')));
var firstNewsMonth = dateFormat[0].substring(5,7);
if (currentMonth >= 0 && currentMonth <= 9){
currentMonth = "0" + currentMonth;
}
if (firstNewsMonth != currentMonth){
console.log("There are no news for " + monthName);
process.exit();
}
for (i = 0; i < 10; i++)
{
if(dateFormat[i].substring(5,7) != firstNewsMonth) break;
description[i] = description[i].replace("\n ", "");
description[i] = description[i].replace("\n", "");
description[i] = description[i].trim();
console.log(date[i]);
console.log(title[i]);
console.log(description[i]);
mailOptions.html += `<p class="news--date" itemprop="startDate" content="2020-07-31">${date[i]}</p>`
mailOptions.html += `<h2 itemprop="name">${title[i]}</h2>`
mailOptions.html += `<img src="${url.slice(0,17)}${imgLink[i]}" class="image--float-right" itemprop="image">`
mailOptions.html += `<p class="news--listing__description" itemprop="articleBody">${description[i]}. <a href="${url.slice(0,17)}${href[i]}" itemprop="url">Read more...</a></p></article>`
if (i != 9 && dateFormat[i+1].substring(5,7) === firstNewsMonth)
{
console.log();
mailOptions.html += `<table style="width: 100%; border-collapse: collapse; border-bottom: rgb(7, 41, 115); padding: 0px;" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="width: 100%; border-bottom: 2px solid rgb(7, 41, 115); padding-bottom: 10px;" data-ogsb="rgb(255, 255, 255)"></td></tr></tbody></table>`;
}
}
if(dateFormat[9].substring(5,7) === firstNewsMonth)
{
await page.click('[class="location-anchor pagination__button pagination__button--next"]');
await NextPage(page, firstNewsMonth);
}
await browser.close();
console.log();
mailOptions.html += `</section></div></body>`;
await transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
})();
async function NextPage(page, firstNewsMonth) {
await page.waitForSelector("h1");
const title = await page.evaluate(() => Array.from(document.querySelectorAll('a[itemprop=url]'), element => element.textContent));
const description = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--listing__description]'), element => element.textContent));
const date = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--date]'), element => element.textContent));
const dateFormat = await page.evaluate(() => Array.from(document.querySelectorAll('p[class=news--date]'), element => element.getAttribute('content')));
const href = await page.evaluate(() => Array.from(document.querySelectorAll('a[itemprop=url]'), element => element.getAttribute('href')));
const imgLink = await page.evaluate(() => Array.from(document.querySelectorAll('img[class=image--float-right]'), element => element.getAttribute('src')));
for (i = 0; i < 10; i++)
{
if(dateFormat[i].substring(5,7) != firstNewsMonth) break;
else if (i === 0)
{
console.log();
mailOptions.html += `<table style="width: 100%; border-collapse: collapse; border-bottom: rgb(7, 41, 115); padding: 0px;" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="width: 100%; border-bottom: 2px solid rgb(7, 41, 115); padding-bottom: 10px;" data-ogsb="rgb(255, 255, 255)"></td></tr></tbody></table>`;
}
description[i] = description[i].replace("\n ", "");
description[i] = description[i].replace("\n", "");
description[i] = description[i].trim();
console.log(date[i]);
console.log(title[i]);
console.log(description[i]);
mailOptions.html += `<p class="news--date" itemprop="startDate" content="2020-07-31">${date[i]}</p>`
mailOptions.html += `<h2 itemprop="name">${title[i]}</h2>`
mailOptions.html += `<img src="${url.slice(0,17)}${imgLink[i]}" class="image--float-right" itemprop="image">`
mailOptions.html += `<p class="news--listing__description" itemprop="articleBody">${description[i]}. <a href="${url.slice(0,17)}${href[i]}" itemprop="url">Read more...</a></p></article>`
if (i != 9 && dateFormat[i+1].substring(5,7) === firstNewsMonth)
{
console.log();
mailOptions.html += `<table style="width: 100%; border-collapse: collapse; border-bottom: rgb(7, 41, 115); padding: 0px;" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="width: 100%; border-bottom: 2px solid rgb(7, 41, 115); padding-bottom: 10px;" data-ogsb="rgb(255, 255, 255)"></td></tr></tbody></table>`;
}
}
if(dateFormat[9].substring(5,7) === firstNewsMonth)
{
await page.click('[class="location-anchor pagination__button pagination__button--next"]');
await NextPage(page, firstNewsMonth);
}
}