-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make zero trust loading more; remove fp-ts * add unit test for updateAssignmentsOfCourse * lint and format * remove console.log mergedNewAssignments from updateAssignmentsOfCourse; update config to double spacce for second header * fix test case
- Loading branch information
1 parent
06e7a5e
commit 5d32cf8
Showing
17 changed files
with
1,157 additions
and
1,001 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
import db, { Assignment } from '../database/database' | ||
import * as cheerio from 'cheerio' | ||
|
||
export default async function extractAssignmentsFromCheerio( | ||
mcvID: number, | ||
$: cheerio.Root | ||
): Promise<Array<Assignment>> { | ||
): Promise<[number, Array<Assignment>] | undefined> { | ||
const assignmentNameNodes = $('tbody tr td:nth-child(2) a').toArray() | ||
const assignments: Array<Assignment> = [] | ||
let foundMcvId: number | undefined = undefined | ||
for (let i = 0; i < assignmentNameNodes.length; i++) { | ||
const ele = assignmentNameNodes[i] | ||
const assignmentLink = $(ele).attr('href') | ||
const assignmentIdStr: string = assignmentLink!.match(/^.*\/(\d+)$/)![1] | ||
const mcvIdAndAssignment = assignmentLink!.match(/^.*\/(\d+)\/(\d+)$/)! | ||
|
||
const currentMcvId: number = parseInt(mcvIdAndAssignment[1]) | ||
if (foundMcvId == undefined) { | ||
foundMcvId = currentMcvId | ||
} else if (currentMcvId != foundMcvId) { | ||
throw new Error('Unexpected course id') | ||
} | ||
const assignmentIdStr: string = mcvIdAndAssignment[2] | ||
const assignmentId: number = parseInt(assignmentIdStr, 10) | ||
const assignment: Assignment = { | ||
mcvCourseID: mcvID, | ||
mcvCourseID: foundMcvId, | ||
assignmentName: $(ele).text(), | ||
assignmentID: assignmentId, | ||
} | ||
const found = await db.assignmentExists(assignment) | ||
if (!found) { | ||
// console.log('found new assignment',assignment) | ||
console.log('found new assignment', assignment) | ||
assignments.push(assignment) | ||
await db.saveAssignment(assignment) | ||
} | ||
} | ||
return assignments | ||
if (foundMcvId == undefined) { | ||
return undefined | ||
} | ||
return [foundMcvId, assignments] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Assignment } from '../database/database' | ||
import fetchAndCatch from '../utils/fetchAndCatch' | ||
import responseToCheerio from '../utils/responseToCheerio' | ||
import extractAssignmentsFromCheerio from './extractAssignmentsFromCheerio' | ||
import scrapeAssignmentsOfPage from './scrapeAssignmentsOfPage' | ||
|
||
/** | ||
* @throws {InvalidCookieError} | ||
*/ | ||
export default async function updateAssignmentsOfCourse( | ||
mcvID: number | ||
): Promise<Map<number, Array<Assignment>> | undefined> { | ||
const result = await fetchAndCatch( | ||
`https://www.mycourseville.com/?q=courseville/course/${mcvID}/assignment`, | ||
'GET' | ||
) | ||
const cheerioRootResponse = await responseToCheerio(result) | ||
if (cheerioRootResponse == undefined) { | ||
return undefined | ||
} | ||
const mergedNewAssignments: Map<number, Array<Assignment>> = new Map() | ||
|
||
let foundCourseIdAndAssignments = | ||
await extractAssignmentsFromCheerio(cheerioRootResponse) | ||
|
||
if (foundCourseIdAndAssignments == undefined) { | ||
return undefined | ||
} | ||
const [foundMcvId, assignments] = foundCourseIdAndAssignments | ||
if (assignments.length != 0) { | ||
mergedNewAssignments.set(foundMcvId, assignments) | ||
} | ||
|
||
let hasNext = true | ||
for (let currentAssignmentItems = 5; hasNext; currentAssignmentItems += 5) { | ||
const scrapeResult = await scrapeAssignmentsOfPage(currentAssignmentItems) | ||
|
||
if (scrapeResult == undefined) { | ||
break | ||
} | ||
|
||
const [resultHasNext, resultMcvId, resultAssignments] = scrapeResult | ||
|
||
hasNext = resultHasNext | ||
if (resultAssignments.length == 0) { | ||
continue | ||
} | ||
if (!mergedNewAssignments.has(resultMcvId)) { | ||
mergedNewAssignments.set(resultMcvId, resultAssignments) | ||
} else { | ||
const found = mergedNewAssignments.get(resultMcvId)! | ||
resultAssignments.forEach(function (v) { | ||
found.push(v) | ||
}, found) | ||
} | ||
} | ||
|
||
return mergedNewAssignments | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.