Skip to content

Commit 9cec194

Browse files
author
addison
committed
web scraping works
1 parent 36d43cd commit 9cec194

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

events/assignment_reminders.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const { EmbedBuilder, AttachmentBuilder } = require("discord.js");
2+
3+
const puppeteer = require("puppeteer");
4+
5+
async function extractDueDates(url) {
6+
const browser = await puppeteer.launch();
7+
const page = await browser.newPage();
8+
await page.goto(url, { waitUntil: "networkidle2" });
9+
10+
const assessments = await page.$$eval('.dynamic-table.has-mobile-table', tables => {
11+
const table = tables[2]; // Select the third occurrence
12+
const rows = Array.from(table.querySelectorAll('tbody tr'));
13+
return rows.map(row => {
14+
let assessmentItem = row.querySelector('td:first-child')?.innerText.trim();
15+
const weight = row.querySelector('td:nth-child(2)')?.innerText.trim();
16+
const dueDate = row.querySelector('dl.dynamic-table-dl div:nth-of-type(2) dd')?.innerText.trim();
17+
18+
// Remove the trailing "Assessment FormatIndividual"
19+
if (assessmentItem) {
20+
assessmentItem = assessmentItem.replace(/Assessment Format\s*Individual$/, '').trim();
21+
}
22+
23+
return { assessmentItem, weight, dueDate };
24+
});
25+
});
26+
27+
await browser.close();
28+
return assessments;
29+
}
30+
31+
function checkInBetween(date_string, c_date, months) {
32+
const beginning_date = date_string[0];
33+
const beginning_month = months.indexOf(date_string[1]);
34+
let ending_date = date_string[3];
35+
let ending_month = months.indexOf(date_string[4]);
36+
if (date_string.length > 5) {
37+
ending_month = months.indexOf(date_string[5]);
38+
ending_date = date_string[4];
39+
40+
if (c_date.getFullYear() == date_string[2]) {
41+
if (beginning_month <= c_date.getMonth() && beginning_date <= c_date.getDate()) {
42+
return true;
43+
}
44+
} else if (c_date.getFullYear() == date_string[6]) {
45+
if (ending_month <= c_date.getMonth() && ending_date >= c_date.getDate()) {
46+
return true;
47+
}
48+
}
49+
50+
return false;
51+
}
52+
53+
if (beginning_month <= c_date.getMonth() && ending_month >= c_date.getMonth()) {
54+
if (beginning_month == c_date.getMonth() && beginning_date > c_date.getDate()) {
55+
return false;
56+
} else if (ending_month == c_date.getMonth() && ending_date < c_date.getDate()) {
57+
return false;
58+
} else {
59+
return true;
60+
}
61+
}
62+
63+
return false;
64+
}
65+
66+
async function getCurrentTerm() {
67+
const months = [
68+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
69+
];
70+
71+
const c_date = new Date();
72+
const url = "https://www.student.unsw.edu.au/calendar";
73+
74+
try {
75+
const { data } = await axios.get(url);
76+
const $ = cheerio.load(data);
77+
const listItems = $(".table-striped tbody tr");
78+
const datas = [];
79+
80+
listItems.each((idx, el) => {
81+
const data_const = { period: "", term: "" };
82+
data_const.period = $(el).children("td:nth-child(2)").text();
83+
data_const.term = $(el).children("td:nth-child(1)").text();
84+
datas.push(data_const);
85+
});
86+
87+
for (let index = 0; index < datas.length; index++) {
88+
const date_string = datas[index].period.split(/\s/g);
89+
90+
if (datas[index].term === "Teaching period T1" && checkInBetween(date_string, c_date, months)) {
91+
return 1;
92+
} else if (datas[index].term === "Teaching period T2" && checkInBetween(date_string, c_date, months)) {
93+
return 2;
94+
} else if (datas[index].term === "Teaching period T3" && checkInBetween(date_string, c_date, months)) {
95+
return 3;
96+
}
97+
}
98+
99+
return -1; // Return -1 if not in any teaching period
100+
} catch (err) {
101+
console.error(err);
102+
return -1;
103+
}
104+
}
105+
106+
async function scheduleReminder() {
107+
const term = getCurrentTerm();
108+
const currentYear = new Date().getFullYear();
109+
const url = `https://www.unsw.edu.au/course-outlines/course-outline#year=${currentYear}&term=Term%20${term}&deliveryMode=In%20Person&deliveryFormat=Standard&
110+
teachingPeriod=T${term}&deliveryLocation=Kensington&courseCode=${course}&activityGroupId=1`;
111+
112+
extractDueDates(url);
113+
}
114+

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"nodemailer": "6.9.13",
4646
"nodemon": "^3.0.0",
4747
"pg": "8.12.0",
48-
"puppeteer": "^22.11.0",
48+
"puppeteer": "^22.12.1",
4949
"textversionjs": "1.1.3",
5050
"voucher-code-generator": "1.3.0",
5151
"xkcd-api": "^1.0.1",

0 commit comments

Comments
 (0)