-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (123 loc) · 3.96 KB
/
app.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
const puppeteer = require('puppeteer');
const moment = require('moment');
const ejs = require('ejs');
const fs = require('fs');
const { promisify } = require('util');
const path = require('path');
const selectors = {
item: '._1m-2ED6C',
// month: '._2uPIj-wb',
// dayOfMonth: '._3al4E82b',
// time: '.d6Vv5DEm div:nth-child(3)',
name: '._1m-2ED6C',
time: 'time:first-of-type'
};
const showMore = '._1pWqBljl';
const dayFormat = 'ddd, MMM Do';
async function getData() {
const browser = await puppeteer.launch({args: ['--no-sandbox'], headless: false});
const page = await browser.newPage();
await page.goto('https://student.naviance.com/fairview/guest');
await page.waitFor('#password');
// await page.click('#password');
await page.type('#password', 'knights');
await page.keyboard.press('Enter');
await page.waitForNavigation({
waitUntil: 'networkidle0'
});
await page.goto('https://student.naviance.com/main');
// await page.waitFor(selectors.item);
await page.waitFor(showMore);
await page.click(showMore);
const collegeData = await page.evaluate(selectors => {
return [...document.querySelectorAll(selectors.item)].map(college => {
const text = college.innerText;
return {
time: college.querySelector(selectors.time).getAttribute('datetime'),
name: text.split('will be visiting your school')[0]
}
})
}, selectors);
// const collegeData = await page.evaluate(selectors => {
// return [...document.querySelectorAll(selectors.item)].map(college => {
// return Object.keys(selectors)
// .filter(selector => selector !== 'item')
// .reduce((prevObject, currSelect) => {
// return {
// ...prevObject,
// [currSelect]: college.querySelector(selectors[currSelect]).innerText
// }
// }, {});
// })
// }, selectors);
await browser.close();
const collegeWithDatetime = collegeData.map(college => {
// const {
// month,
// dayOfMonth,
// time,
// name
// } = college;
// return {
// name,
// datetime: moment(`${month} ${dayOfMonth} ${time}`, 'MMMM DD hh:mma')
// }
const {
time,
name
} = college;
return {
name,
datetime: moment(time, 'YYYY-MM-DD HH:mm:ss')
}
});
// console.log(collegeWithDatetime);
const firstCollegeWeek = collegeWithDatetime.reduce((prevWeek, college) => {
const collegeWeek = college.datetime.week();
return collegeWeek < prevWeek ? collegeWeek : prevWeek;
}, 52);
const lastCollegeWeek = collegeWithDatetime.reduce((prevWeek, college) => {
const collegeWeek = college.datetime.week();
return collegeWeek > prevWeek ? collegeWeek : prevWeek;
}, 1);
const weeks = [];
for (let week = firstCollegeWeek; week <= lastCollegeWeek; week++) {
const days = [];
for (let day = 0; day < 7; day++) {
const dayMoment = moment(`${day} ${week}`, 'e ww');
days.push({
day: dayMoment.format(dayFormat),
visits: []
});
}
weeks[week - firstCollegeWeek] = days;
}
// console.log(collegeWithDatetime);
collegeWithDatetime.forEach(college => {
const {
datetime
} = college;
const dayMoment = datetime.format(dayFormat);
// console.log(dayMoment);
weeks.forEach((week, weekIndex) => {
week.forEach((day, dayIndex) => {
if (day.day === dayMoment) {
weeks[weekIndex][dayIndex].visits.push({
name: college.name,
time: college.datetime.format('h:mma')
})
}
})
})
});
const template = ejs.compile(await promisify(fs.readFile)(path.join(__dirname, 'college_rep_visits.ejs'), 'utf-8'));
const rendered = template({
weeks,
updated: moment().format('dddd, MMMM, Do YYYY h:mm a')
});
await promisify(fs.writeFile)(path.join(__dirname, 'college_rep_visits.html'), rendered);
// await page.waitForNavigation({ waitUntil: 'networkidle0' });
}
module.exports = {
getData
};