Added parser

This commit is contained in:
VityaSchel
2023-10-01 21:28:53 +04:00
parent b6ea0217c3
commit a4667f57c7
8 changed files with 378 additions and 57 deletions

View File

@@ -1,6 +1,5 @@
import { Day } from '@/shared/model/day'
import { Lesson } from '@/shared/model/lesson'
import { HTMLElement } from 'node-html-parser'
const dayTitleParser = (text: string) => {
const [dateString, week] = text.trim().split(' / ')
@@ -10,55 +9,69 @@ const dayTitleParser = (text: string) => {
return { date, weekNumber }
}
const parseLesson = (row: HTMLElement): Lesson => {
const cells = row.querySelectorAll(':scope > td')
const parseLesson = (row: Element): Lesson | null => {
const cells = Array.from(row.querySelectorAll(':scope > td'))
if (cells[3].textContent!.trim() === 'Свободное время') return null
const isChange = cells.every(td => td.getAttribute('bgcolor') === 'ffffbb')
const timeCell = cells[1].childNodes
const [startTime, endTime] = timeCell[0].textContent.trim().split(' ')
const [startTime, endTime] = timeCell[0].textContent!.trim().split(' ')
const time: Lesson['time'] = {
start: startTime,
end: endTime
start: startTime ?? '',
end: endTime ?? ''
}
if (timeCell[2]) {
time.hint = timeCell[2].textContent.trim()
time.hint = timeCell[2].textContent!.trim()
}
const subject = ''
const teacher = ''
const subject = cells[3].childNodes[0].textContent!.trim()
const place: Lesson['place'] = {
address: '1',
classroom: 1
let teacher: Lesson['teacher']
const teacherCell = cells[3].childNodes[2]
if (teacherCell) {
teacher = teacherCell.textContent!.trim()
}
const topic = cells[4].textContent.trim()
const placeCell = cells[3].childNodes[3]
let place: Lesson['place']
if (placeCell) {
place = {
address: placeCell.childNodes[1].textContent!.trim(),
classroom: Number(placeCell.childNodes[3].textContent!.trim().match(/^Кабинет: (\d+)(-2)?$/)![1])
}
}
const topic: Lesson['topic'] = cells[4].textContent!.trim()
const resources: Lesson['resources'] = []
// {
// type: 'link'
// title: string
// url: string
// } []
Array.from(cells[5].querySelectorAll('a'))
.forEach(a => {
resources.push({
type: 'link',
title: a.textContent!.trim(),
url: a.getAttribute('href')!
})
})
return {
isChange,
time,
type: cells[2].textContent.trim(),
type: cells[2].textContent!.trim(),
subject,
teacher,
place,
...(teacher && { teacher }),
...(place && { place }),
...(topic && { topic }),
resources,
homework: cells[6].textContent.trim()
homework: cells[6].textContent!.trim()
}
}
export function parsePage(document: HTMLElement): Day[] {
export function parsePage(document: Document): Day[] {
const tables = Array.from(document.querySelectorAll('body > table'))
const table = tables.find(table => table.querySelector(':scope > tbody > tr:first-child')?.textContent?.trim() === 'ПС-7')
const rows = Array.from(table!.querySelectorAll(':scope > tbody > tr')).slice(1)
const rows = Array.from(table!.children[0].children).filter(el => el.tagName === 'TR').slice(2)
const days = []
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -89,7 +102,9 @@ export function parsePage(document: HTMLElement): Day[] {
dayInfo.weekNumber = weekNumber
previousRowIsDayTitle = true
} else {
dayLessons.push(parseLesson(row))
const lesson = parseLesson(row)
if(lesson !== null)
dayLessons.push(lesson)
}
}