Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as fs from 'fs';
  2. import {getBrightness} from '../../Utilities';
  3. import {
  4.     Cards, Class, Classes, Classroom, Classrooms, Group, Groups, Lessons, Periods, Subjects, Teacher, Teachers,
  5.     TimetableData,
  6. } from './TimetableData';
  7. import {config} from '../..';
  8. import {preprocessDirectives} from 'tslint/lib/verify/parse';
  9.  
  10. export class TimetableParser {
  11.     public static parseTimetable(data: string) {
  12.         console.log('zacalo spracovanie');
  13.         const lessons: TimetableData[] = [];
  14.         data = data.replace(/(\r\n|\n|\r)/gm, '');
  15.         let jsonString = data.substring(data.indexOf('.then(function(f){return') + 35, data.length - 3);
  16.         jsonString = jsonString.substring(
  17.             0,
  18.             jsonString.indexOf(');});gi'));
  19.  
  20.         console.log('Pred naparzovanim JSONUs');
  21.         fs.writeFile('download.log', jsonString, (err) => {
  22.             if (err) {
  23.                 return console.log(err);
  24.             }
  25.  
  26.             console.log('The file was saved!');
  27.         });
  28.         const timetable = JSON.parse(jsonString);
  29.         console.log('Po parzovani');
  30.  
  31.         console.log('konvertuju sa data');
  32.         for (const change of timetable.changes) {
  33.             console.log(change.table);
  34.             TimetableParser.timetableData[change.table] = change.rows;
  35.         }
  36.         console.log('Zkonvertovali sa');
  37.         for (const card of TimetableParser.timetableData.cards) {
  38.             const lesson = TimetableParser.getLessonById(card.lessonid);
  39.             const period = TimetableParser.getPeriod(card.period);
  40.             const subject = TimetableParser.getSubjectById(lesson.subjectid);
  41.  
  42.             const groups: Group[] = [];
  43.             if (card.classroomids.length > 0) {
  44.                 for (const groupId of lesson.groupids) {
  45.                     const group = TimetableParser.getGroupById(groupId);
  46.                     if (group) {
  47.                         groups.push({
  48.                             name: group.name,
  49.                             short: group.short,
  50.                             entireclass: group.entireclass === '1',
  51.                         });
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             const classes: Class[] = [];
  57.             if (lesson.classids.length > 0) {
  58.                 for (const classId of lesson.classids) {
  59.                     const classData = TimetableParser.getClassById(classId);
  60.                     if (classData) {
  61.                         classes.push({
  62.                             name: classData.name,
  63.                             short: classData.short,
  64.                         });
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             const classrooms: Classroom[] = [];
  70.             if (card.classroomids.length > 0) {
  71.                 for (const classroomId of card.classroomids) {
  72.                     const classroom = TimetableParser.getClassroomById(classroomId);
  73.                     if (classroom) {
  74.                         let name = classroom.name;
  75.                         if (classroom.name.indexOf('Správca') !== -1) {
  76.                             name = classroom.name.substring(0,
  77.                                 classroom.name.indexOf('Správca'),
  78.                             );
  79.                         }
  80.                         classrooms.push({
  81.                             name: name,
  82.                             short: classroom.short,
  83.                         });
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             const teachers: Teacher[] = [];
  89.             if (lesson.teacherids.length > 0) {
  90.                 for (const teacherId of lesson.teacherids) {
  91.                     const teacher = TimetableParser.getTeacherById(teacherId);
  92.                     if (teacher) {
  93.                         teachers.push({
  94.                             firstname: teacher.firstname,
  95.                             lastname: teacher.lastname,
  96.                             short: teacher.short,
  97.                             nameprefix: teacher.nameprefix,
  98.                             namesuffix: teacher.namesuffix,
  99.                             color: teacher.color,
  100.                         })
  101.                         ;
  102.                     }
  103.                 }
  104.             }
  105.  
  106.             let color = '#801170';
  107.             if (getBrightness(subject.color) < 200) {
  108.                 color = subject.color;
  109.             } else if (teachers.length > 0 && getBrightness(teachers[0].color) < 200) {
  110.                 color = teachers[0].color;
  111.             }
  112.  
  113.             let startTime = null;
  114.             let endTime = null;
  115.             if (period != null) {
  116.                 startTime = period.starttime;
  117.                 const endPeriodId = (Number(card.period) + Number(lesson.durationperiods) - 1);
  118.                 const endPeriod = TimetableParser.getPeriod(endPeriodId);
  119.                 endTime = endPeriod.endtime;
  120.             } else {
  121.                 console.log('Nema hodinu: ', card.period, TimetableParser.getPeriod(card.period).id);
  122.                 // break;
  123.             }
  124.             // console.log('pushuje sa');
  125.  
  126.             lessons.push({
  127.                 hour: card.period,
  128.                 duration: lesson.durationperiods,
  129.                 days: card.days,
  130.                 startTime: startTime,
  131.                 endTime: endTime,
  132.                 subject: subject.name,
  133.                 subjectShort: subject.short,
  134.                 groups: groups,
  135.                 classes: classes,
  136.                 classrooms: classrooms,
  137.                 teachers: teachers,
  138.                 color: color,
  139.                 substituted: false, // TODO: Treba dokoncit ked bude relny rozvrh
  140.             });
  141.         }
  142.         lessons.sort((a, b) => a.hour - b.hour);
  143.         return lessons;
  144.     }
  145.  
  146.     private static timetableData: {
  147.         lessons?: Lessons[];
  148.         cards?: Cards[];
  149.         periods?: Periods[];
  150.         subjects?: Subjects[];
  151.         groups?: Groups[];
  152.         classes?: Classes[];
  153.         classrooms?: Classrooms[];
  154.         teachers?: Teachers[];
  155.         [x: string]: any;
  156.     } = {};
  157.  
  158.     private static getLessonById(id: string): Lessons {
  159.         for (const lesson of TimetableParser.timetableData.lessons) {
  160.             if (lesson.id === id) {
  161.                 return lesson;
  162.             }
  163.         }
  164.     }
  165.  
  166.     private static getSubjectById(id: string): Subjects {
  167.         for (const subject of TimetableParser.timetableData.subjects) {
  168.             if (subject.id === id) {
  169.                 return subject;
  170.             }
  171.         }
  172.     }
  173.  
  174.     private static getGroupById(id: string): Groups {
  175.         for (const group of TimetableParser.timetableData.groups) {
  176.             if (group.id === id) {
  177.                 return group;
  178.             }
  179.         }
  180.     }
  181.  
  182.     private static getClassById(id: string): Classes {
  183.         for (const ClassK of TimetableParser.timetableData.classes) {
  184.             if (ClassK.id === id) {
  185.                 return ClassK;
  186.             }
  187.         }
  188.     }
  189.  
  190.     private static getClassroomById(id: string): Classrooms {
  191.         for (const classroom of TimetableParser.timetableData.classrooms) {
  192.             if (classroom.id === id) {
  193.                 return classroom;
  194.             }
  195.         }
  196.     }
  197.  
  198.     private static getTeacherById(id: string): Teachers {
  199.         for (const teacher of TimetableParser.timetableData.teachers) {
  200.             if (teacher.id === id) {
  201.                 return teacher;
  202.             }
  203.         }
  204.     }
  205.  
  206.     private static getPeriod(periodNumber: number): Periods {
  207.         for (const period of TimetableParser.timetableData.periods) {
  208.             if (Number(period.id) === Number(periodNumber)) {
  209.                 return period;
  210.             }
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement