diff --git a/index.ts b/index.ts index bd8122f..51979d6 100644 --- a/index.ts +++ b/index.ts @@ -1,24 +1,30 @@ import {Cheerio, Element, load} from "cheerio"; -import * as fs from "fs"; +import {readFileSync} from "fs"; +import {parse} from "date-fns"; +import {inspect} from "util"; -const $ = load(fs.readFileSync('list.html')); +const $ = load(readFileSync('list.html')); const classes = $('#scheduleListView').children('.listViewWrapper'); +function getOffset(period: string) { + switch (period) { + case "AM": + return 0; + case "PM": + return 12; + default: + throw new Error(`Unknown period: ${period}`); + } +} + function extractDetails(source: Cheerio) { - source.find('div.list-view-crn-info-div > span.bold').toArray().forEach((descriptor) => { + const details: [string, string][] = source.find('div.list-view-crn-info-div > span.bold').toArray().map((descriptor) => { const descriptor_element = $(descriptor); - - // console.log([descriptor_element.text(), descriptor_element.next().text()]) + return [descriptor_element.text(), descriptor_element.next().text()] }) - // console.log( - // source.find('div.listViewMeetingInformation').children('span').last().text() - // ) - - // For some reason this div divides the information into two parts. - const div_information_divier = source.find('div.list-view-pillbox.ui-pillbox'); - const timing_information = div_information_divier.next(); + const timing_information = source.find('div.list-view-pillbox.ui-pillbox').next(); const raw_time = timing_information.text(); const match = raw_time.match(/(\d{2})\s*:\s*(\d{2})\s*(AM|PM)\D*(\d{2})\s*:\s*(\d{2})\s*(AM|PM)/) @@ -29,22 +35,24 @@ function extractDetails(source: Cheerio) { const [start_hour, start_minute, end_hour, end_minute] = [1, 2, 4, 5].map((index) => parseInt(match[index])); const [start_period, end_period] = [match[3], match[6]]; + const raw_date = source.find('span.meetingTimes').text(); + const [start_date, end_date] = raw_date.split("--").map((date) => parse(date.trim(), "MM/dd/yyyy", new Date())); + return { - start: {minute: start_minute, hour: start_hour, period: start_period}, - end: {minute: end_minute, hour: end_hour, period: end_period}, + date: { + start: start_date, + end: end_date, + }, + time: { + start: {minute: start_minute, hour: start_hour + getOffset(start_period)}, + end: {minute: end_minute, hour: end_hour + getOffset(end_period)}, + }, days: source.find('div.ui-pillbox-summary.screen-reader').text().split(",").map((day) => day.trim()), name: source.find('span.list-view-course-title > a.section-details-link').text().trim() }; } console.log(`${classes.length} classes identified.`); - -classes.toArray().forEach((element) => { - console.log(extractDetails( - $(element) - )) -}); - -// console.log(classes.toArray().map((element) => element.attribs.class)) - -// console.log(`First class: ${extractDetails(classes)}`); \ No newline at end of file +console.log( + inspect(classes.toArray().map((element) => extractDetails($(element))), {colors: true, depth: null}) +); diff --git a/package.json b/package.json index 5a70312..2cccb7a 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "@types/node": "^20.4.9", "cheerio": "^1.0.0-rc.12", + "date-fns": "^2.30.0", "ics": "^3.2.0", "ts-node": "^10.9.1", "typescript": "^5.1.6" diff --git a/yarn.lock b/yarn.lock index 2ac3f3c..d1a4354 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,13 @@ # yarn lockfile v1 +"@babel/runtime@^7.21.0": + version "7.22.10" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.10.tgz#ae3e9631fd947cb7e3610d3e9d8fef5f76696682" + integrity sha512-21t/fkKLMZI4pqP2wlmsQAWnYW1PDyKyyUV4vCi+B25ydmdaYTKXPwCj0BzSUnZf4seIiYvSA3jcZ3gdsMFkLQ== + dependencies: + regenerator-runtime "^0.14.0" + "@cspotcode/source-map-support@^0.8.0": version "0.8.1" resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1" @@ -118,6 +125,13 @@ css-what@^6.1.0: resolved "https://registry.yarnpkg.com/css-what/-/css-what-6.1.0.tgz#fb5effcf76f1ddea2c81bdfaa4de44e79bac70f4" integrity sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw== +date-fns@^2.30.0: + version "2.30.0" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0" + integrity sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw== + dependencies: + "@babel/runtime" "^7.21.0" + diff@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" @@ -213,6 +227,11 @@ property-expr@^2.0.5: resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.5.tgz#278bdb15308ae16af3e3b9640024524f4dc02cb4" integrity sha512-IJUkICM5dP5znhCckHSv30Q4b5/JA5enCtkRHYaOVOAocnH/1BQEYTC5NMfT3AVl/iXKdr3aqQbQn9DxyWknwA== +regenerator-runtime@^0.14.0: + version "0.14.0" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz#5e19d68eb12d486f797e15a3c6a918f7cec5eb45" + integrity sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA== + tiny-case@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/tiny-case/-/tiny-case-1.0.3.tgz#d980d66bc72b5d5a9ca86fb7c9ffdb9c898ddd03"