textbook button, properly scraping the semester (year and season) from the url"
This commit is contained in:
@@ -53,6 +53,14 @@ export default function CourseButtons({ course }: Props) {
|
||||
openNewTab({ url: url.toString() });
|
||||
};
|
||||
|
||||
const openTextbookURL = () => {
|
||||
const { department, number, semester, uniqueId } = course;
|
||||
const url = new URL('https://www.universitycoop.com/adoption-search-results');
|
||||
url.searchParams.append('sn', `${semester.code}__${department}__${number}__${uniqueId}`);
|
||||
|
||||
openNewTab({ url: url.toString() });
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className={styles.container}>
|
||||
<Button
|
||||
@@ -72,7 +80,7 @@ export default function CourseButtons({ course }: Props) {
|
||||
</Text>
|
||||
<Icon className={styles.icon} color='white' name='grading' size='medium' />
|
||||
</Button>
|
||||
<Button type='tertiary' className={styles.button}>
|
||||
<Button onClick={openTextbookURL} type='tertiary' className={styles.button}>
|
||||
<Text size='medium' weight='regular' color='white'>
|
||||
Textbook
|
||||
</Text>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Serialized } from 'chrome-extension-toolkit';
|
||||
import { Course, Status, InstructionMode, ScrapedRow } from 'src/shared/types/Course';
|
||||
import { Course, Status, InstructionMode, ScrapedRow, Semester } from 'src/shared/types/Course';
|
||||
import { CourseSchedule } from 'src/shared/types/CourseSchedule';
|
||||
import Instructor from 'src/shared/types/Instructor';
|
||||
import { SiteSupport } from 'src/views/lib/getSiteSupport';
|
||||
@@ -71,16 +71,6 @@ export class CourseCatalogScraper {
|
||||
const [courseName, department, number] = this.separateCourseName(fullName);
|
||||
const [status, isReserved] = this.getStatus(row);
|
||||
|
||||
// TODO: get semester from somewhere
|
||||
const year = new Date().getFullYear();
|
||||
const month = new Date().getMonth();
|
||||
let season = 'Fall';
|
||||
if (month >= 0 && month < 5) {
|
||||
season = 'Spring';
|
||||
} else if (month >= 5 && month < 8) {
|
||||
season = 'Summer';
|
||||
}
|
||||
|
||||
const newCourse = new Course({
|
||||
fullName,
|
||||
courseName,
|
||||
@@ -96,11 +86,7 @@ export class CourseCatalogScraper {
|
||||
instructionMode: this.getInstructionMode(row),
|
||||
instructors: this.getInstructors(row) as Instructor[],
|
||||
description: this.getDescription(document),
|
||||
// TODO: get semester from somewhere
|
||||
semester: {
|
||||
year,
|
||||
season,
|
||||
},
|
||||
semester: this.getSemester(),
|
||||
});
|
||||
courses.push({
|
||||
element: row,
|
||||
@@ -213,6 +199,43 @@ export class CourseCatalogScraper {
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
getSemester(): Semester {
|
||||
const { pathname } = new URL(window.location.href);
|
||||
|
||||
const code = pathname.split('/')[4];
|
||||
if (!code) {
|
||||
throw new Error('Semester not found in URL');
|
||||
}
|
||||
|
||||
let year = Number(code.substring(0, 4));
|
||||
let seasonCode = Number(code.substring(4, 6));
|
||||
|
||||
if (!year || !seasonCode) {
|
||||
throw new Error('Invalid semester found in URL');
|
||||
}
|
||||
|
||||
let season: Semester['season'];
|
||||
switch (seasonCode) {
|
||||
case 2:
|
||||
season = 'Spring';
|
||||
break;
|
||||
case 6:
|
||||
season = 'Summer';
|
||||
break;
|
||||
case 9:
|
||||
season = 'Fall';
|
||||
break;
|
||||
default:
|
||||
throw new Error('Invalid season code');
|
||||
}
|
||||
|
||||
return {
|
||||
year,
|
||||
season,
|
||||
code,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full name of the course from the course catalog table row (e.g. "CS 314H - Honors Discrete Structures")
|
||||
* @param row the row of the course catalog table
|
||||
|
||||
Reference in New Issue
Block a user