@@ -682,98 +682,106 @@ class RawParser {
682
682
*/
683
683
fun parseTimetable (rawResponse : String ): List <Lesson > {
684
684
val returnList = mutableListOf<Lesson >()
685
- // Extract table using jsoup
686
- val doc = Jsoup .parse(rawResponse)
687
- val table = doc.select(" div.plan tbody" )[0 ]
688
- // Table is split up into rows of hours (one or multiple)
689
- // There is one row for each hour, even if there aren't any lesson
690
- // However, there isn't a cell if values overlap from the previous hour
691
- // And there also insn't any attribute to let us know which day the lesson is on, if a cell has been skipped
692
- // The rowspan attribute (number of hours) will be the same for all entries within a cell
693
- // Remember the number of rows to skip for each day..
694
- val hoursSkipped = mutableListOf (0 , 0 , 0 , 0 , 0 )
695
- var rowspanRaw: String
696
- var rowspan: Int
697
- var rowspanInner: Int
698
- var currentHour = 1
699
- var currentDay: Int
700
- var currentDayRaw: Int
701
- var cells: Elements
702
- var lessons: Elements
703
- var gmbId: String
704
- var courseId: String
705
- var teacherId: String
706
- var room: String
707
685
708
- // For each hour
709
- for (row in table.select(" tr" )) {
710
- cells = row.select(" td" )
711
- // First column is always a description, including time
712
- // todo get lesson times
713
- currentDay = - 1
714
- currentDayRaw = 1
715
- // For every day, also skip non-existent columns
716
- while (currentDay <= 4 ) {
717
- // Skip first column
718
- if (currentDay != - 1 ) {
719
- // Only if this hour was not overridden by last hour
720
- if (hoursSkipped[currentDay] == 0 ) {
721
- // Remember if this overrides the next hour
722
- rowspanRaw = cells[currentDayRaw].attr(" rowspan" ) ? : " 1"
723
- if (rowspanRaw == " " ) rowspanRaw = " 1"
724
- rowspan = rowspanRaw.toInt() - 1
725
- hoursSkipped[currentDay] = rowspan
726
-
727
- // Find lessons within this cell
728
- lessons = cells[currentDayRaw].select(" div.stunde" )
729
- currentDayRaw++
730
-
731
- // Add each lesson
732
- for (lesson in lessons) {
733
- rowspanInner = 0
734
-
735
- // Extract data
736
- gmbId = lesson.select(" b" ).text().trim()
737
- teacherId = lesson.select(" small" ).text().trim()
738
- room = lesson.toString().substring(
739
- lesson.toString().indexOf(" </b>" ) + 4 ,
740
- lesson.toString().indexOf(" <small" )
741
- ).replace(" <br>" , " " ).trim()
742
- courseId = IdParser ().getCourseIdWithGmb(gmbId, teacherId, false )
743
-
744
- // Add each lesson to the return list
745
- // Individually, if rowspan is larger than 1
746
- while (rowspanInner <= rowspan) {
747
- // Check for duplicate courses with different rooms
748
- if (returnList.any {
686
+ // Return empty list if content is not what we expected
687
+ try {
688
+
689
+ // Extract table using jsoup
690
+ val doc = Jsoup .parse(rawResponse)
691
+ val table = doc.select(" div.plan tbody" )[0 ]
692
+ // Table is split up into rows of hours (one or multiple)
693
+ // There is one row for each hour, even if there aren't any lesson
694
+ // However, there isn't a cell if values overlap from the previous hour
695
+ // And there also insn't any attribute to let us know which day the lesson is on, if a cell has been skipped
696
+ // The rowspan attribute (number of hours) will be the same for all entries within a cell
697
+ // Remember the number of rows to skip for each day..
698
+ val hoursSkipped = mutableListOf (0 , 0 , 0 , 0 , 0 )
699
+ var rowspanRaw: String
700
+ var rowspan: Int
701
+ var rowspanInner: Int
702
+ var currentHour = 1
703
+ var currentDay: Int
704
+ var currentDayRaw: Int
705
+ var cells: Elements
706
+ var lessons: Elements
707
+ var gmbId: String
708
+ var courseId: String
709
+ var teacherId: String
710
+ var room: String
711
+
712
+ // For each hour
713
+ for (row in table.select(" tr" )) {
714
+ cells = row.select(" td" )
715
+ // First column is always a description, including time
716
+ // todo get lesson times
717
+ currentDay = - 1
718
+ currentDayRaw = 1
719
+ // For every day, also skip non-existent columns
720
+ while (currentDay <= 4 ) {
721
+ // Skip first column
722
+ if (currentDay != - 1 ) {
723
+ // Only if this hour was not overridden by last hour
724
+ if (hoursSkipped[currentDay] == 0 ) {
725
+ // Remember if this overrides the next hour
726
+ rowspanRaw = cells[currentDayRaw].attr(" rowspan" ) ? : " 1"
727
+ if (rowspanRaw == " " ) rowspanRaw = " 1"
728
+ rowspan = rowspanRaw.toInt() - 1
729
+ hoursSkipped[currentDay] = rowspan
730
+
731
+ // Find lessons within this cell
732
+ lessons = cells[currentDayRaw].select(" div.stunde" )
733
+ currentDayRaw++
734
+
735
+ // Add each lesson
736
+ for (lesson in lessons) {
737
+ rowspanInner = 0
738
+
739
+ // Extract data
740
+ gmbId = lesson.select(" b" ).text().trim()
741
+ teacherId = lesson.select(" small" ).text().trim()
742
+ room = lesson.toString().substring(
743
+ lesson.toString().indexOf(" </b>" ) + 4 ,
744
+ lesson.toString().indexOf(" <small" )
745
+ ).replace(" <br>" , " " ).trim()
746
+ courseId = IdParser ().getCourseIdWithGmb(gmbId, teacherId, false )
747
+
748
+ // Add each lesson to the return list
749
+ // Individually, if rowspan is larger than 1
750
+ while (rowspanInner <= rowspan) {
751
+ // Check for duplicate courses with different rooms
752
+ if (returnList.any {
753
+ it.idCourse == courseId
754
+ && it.day == currentDay
755
+ && it.hour == currentHour + rowspanInner
756
+ }) {
757
+ // Course was already added, but with a different room
758
+ // Update the course with this room added
759
+ returnList.find {
749
760
it.idCourse == courseId
750
761
&& it.day == currentDay
751
762
&& it.hour == currentHour + rowspanInner
752
- }) {
753
- // Course was already added, but with a different room
754
- // Update the course with this room added
755
- returnList.find {
756
- it.idCourse == courseId
757
- && it.day == currentDay
758
- && it.hour == currentHour + rowspanInner
759
- }!! .room + = " , $room "
760
- } else {
761
- // Just add the lesson to the list
762
- returnList.add(Lesson (
763
- courseId,
764
- currentDay,
765
- currentHour + rowspanInner,
766
- room
767
- ))
763
+ }!! .room + = " , $room "
764
+ } else {
765
+ // Just add the lesson to the list
766
+ returnList.add(Lesson (
767
+ courseId,
768
+ currentDay,
769
+ currentHour + rowspanInner,
770
+ room
771
+ ))
772
+ }
773
+ rowspanInner++
768
774
}
769
- rowspanInner++
770
775
}
771
- }
772
- } else hoursSkipped[currentDay]--
776
+ } else hoursSkipped[currentDay]--
777
+ }
778
+ currentDay++
773
779
}
774
- currentDay ++
780
+ currentHour ++
775
781
}
776
- currentHour++
782
+ } catch (e: java.lang.IndexOutOfBoundsException ) {
783
+ Log .d(TAG , " Timetable parsing failed!" )
784
+ Log .d(TAG , e.stackTraceToString())
777
785
}
778
786
779
787
return returnList
0 commit comments