Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as fs from 'fs';
- import {getBrightness} from '../../Utilities';
- import {
- Cards, Class, Classes, Classroom, Classrooms, Group, Groups, Lessons, Periods, Subjects, Teacher, Teachers,
- TimetableData,
- } from './TimetableData';
- import {config} from '../..';
- import {preprocessDirectives} from 'tslint/lib/verify/parse';
- export class TimetableParser {
- public static parseTimetable(data: string) {
- console.log('zacalo spracovanie');
- const lessons: TimetableData[] = [];
- data = data.replace(/(\r\n|\n|\r)/gm, '');
- let jsonString = data.substring(data.indexOf('.then(function(f){return') + 35, data.length - 3);
- jsonString = jsonString.substring(
- 0,
- jsonString.indexOf(');});gi'));
- console.log('Pred naparzovanim JSONUs');
- fs.writeFile('download.log', jsonString, (err) => {
- if (err) {
- return console.log(err);
- }
- console.log('The file was saved!');
- });
- const timetable = JSON.parse(jsonString);
- console.log('Po parzovani');
- console.log('konvertuju sa data');
- for (const change of timetable.changes) {
- console.log(change.table);
- TimetableParser.timetableData[change.table] = change.rows;
- }
- console.log('Zkonvertovali sa');
- for (const card of TimetableParser.timetableData.cards) {
- const lesson = TimetableParser.getLessonById(card.lessonid);
- const period = TimetableParser.getPeriod(card.period);
- const subject = TimetableParser.getSubjectById(lesson.subjectid);
- const groups: Group[] = [];
- if (card.classroomids.length > 0) {
- for (const groupId of lesson.groupids) {
- const group = TimetableParser.getGroupById(groupId);
- if (group) {
- groups.push({
- name: group.name,
- short: group.short,
- entireclass: group.entireclass === '1',
- });
- }
- }
- }
- const classes: Class[] = [];
- if (lesson.classids.length > 0) {
- for (const classId of lesson.classids) {
- const classData = TimetableParser.getClassById(classId);
- if (classData) {
- classes.push({
- name: classData.name,
- short: classData.short,
- });
- }
- }
- }
- const classrooms: Classroom[] = [];
- if (card.classroomids.length > 0) {
- for (const classroomId of card.classroomids) {
- const classroom = TimetableParser.getClassroomById(classroomId);
- if (classroom) {
- let name = classroom.name;
- if (classroom.name.indexOf('Správca') !== -1) {
- name = classroom.name.substring(0,
- classroom.name.indexOf('Správca'),
- );
- }
- classrooms.push({
- name: name,
- short: classroom.short,
- });
- }
- }
- }
- const teachers: Teacher[] = [];
- if (lesson.teacherids.length > 0) {
- for (const teacherId of lesson.teacherids) {
- const teacher = TimetableParser.getTeacherById(teacherId);
- if (teacher) {
- teachers.push({
- firstname: teacher.firstname,
- lastname: teacher.lastname,
- short: teacher.short,
- nameprefix: teacher.nameprefix,
- namesuffix: teacher.namesuffix,
- color: teacher.color,
- })
- ;
- }
- }
- }
- let color = '#801170';
- if (getBrightness(subject.color) < 200) {
- color = subject.color;
- } else if (teachers.length > 0 && getBrightness(teachers[0].color) < 200) {
- color = teachers[0].color;
- }
- let startTime = null;
- let endTime = null;
- if (period != null) {
- startTime = period.starttime;
- const endPeriodId = (Number(card.period) + Number(lesson.durationperiods) - 1);
- const endPeriod = TimetableParser.getPeriod(endPeriodId);
- endTime = endPeriod.endtime;
- } else {
- console.log('Nema hodinu: ', card.period, TimetableParser.getPeriod(card.period).id);
- // break;
- }
- // console.log('pushuje sa');
- lessons.push({
- hour: card.period,
- duration: lesson.durationperiods,
- days: card.days,
- startTime: startTime,
- endTime: endTime,
- subject: subject.name,
- subjectShort: subject.short,
- groups: groups,
- classes: classes,
- classrooms: classrooms,
- teachers: teachers,
- color: color,
- substituted: false, // TODO: Treba dokoncit ked bude relny rozvrh
- });
- }
- lessons.sort((a, b) => a.hour - b.hour);
- return lessons;
- }
- private static timetableData: {
- lessons?: Lessons[];
- cards?: Cards[];
- periods?: Periods[];
- subjects?: Subjects[];
- groups?: Groups[];
- classes?: Classes[];
- classrooms?: Classrooms[];
- teachers?: Teachers[];
- [x: string]: any;
- } = {};
- private static getLessonById(id: string): Lessons {
- for (const lesson of TimetableParser.timetableData.lessons) {
- if (lesson.id === id) {
- return lesson;
- }
- }
- }
- private static getSubjectById(id: string): Subjects {
- for (const subject of TimetableParser.timetableData.subjects) {
- if (subject.id === id) {
- return subject;
- }
- }
- }
- private static getGroupById(id: string): Groups {
- for (const group of TimetableParser.timetableData.groups) {
- if (group.id === id) {
- return group;
- }
- }
- }
- private static getClassById(id: string): Classes {
- for (const ClassK of TimetableParser.timetableData.classes) {
- if (ClassK.id === id) {
- return ClassK;
- }
- }
- }
- private static getClassroomById(id: string): Classrooms {
- for (const classroom of TimetableParser.timetableData.classrooms) {
- if (classroom.id === id) {
- return classroom;
- }
- }
- }
- private static getTeacherById(id: string): Teachers {
- for (const teacher of TimetableParser.timetableData.teachers) {
- if (teacher.id === id) {
- return teacher;
- }
- }
- }
- private static getPeriod(periodNumber: number): Periods {
- for (const period of TimetableParser.timetableData.periods) {
- if (Number(period.id) === Number(periodNumber)) {
- return period;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement