2018-10-29 13:04:20 +01:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < seil0 @mosad . xyz >
2018-10-29 13:04:20 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
package org.mosad.seil0.projectlaogai.hsoparser
import org.jsoup.Jsoup
class TimeTableParser {
2018-10-29 18:03:20 +01:00
private val days = arrayOf ( " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " , " Saturday " )
2019-02-17 15:05:03 +01:00
private var courseTTLinkList = ArrayList < Course > ( )
2018-10-29 13:04:20 +01:00
2019-01-03 13:46:09 +01:00
/ * *
* get the timetable from the given url
* the timetable is organised per row not per column ;
* Mon 1 , Tue 1 , Wed 1 , Thur 1 , Fri 1 , Sat 1 , Mon 2 and so on
* /
2019-03-01 18:59:17 +01:00
fun getTimeTable ( courseTTURL : String ) : TimeTable {
var timeTableWeek = TimeTable ( ) // this must be a var!
2019-01-03 13:46:09 +01:00
val scheduleHTML = Jsoup . connect ( courseTTURL ) . get ( )
2018-11-07 14:17:33 +01:00
//val week = scheduleHTML.select("h1.timetable-caption").text()
//println("$week successful!\n")
2018-10-29 13:04:20 +01:00
2019-02-28 14:52:40 +01:00
val rows = scheduleHTML . select ( " table.timetable " ) . select ( " tr[scope= \" row \" ] " )
var sDay = - 1
var sRow = - 1
var sLesson = Lesson ( " " , " " , " " , " " )
// get each row with index, reflects 1 timeslot per day
for ( ( rowIndex , row ) in rows . withIndex ( ) ) {
var day = 0
2019-03-09 22:15:53 +01:00
// elements are now all lessons, including empty ones
2019-02-28 14:52:40 +01:00
row . select ( " td.lastcol, td[style] " ) . forEach { element ->
// if there is a lecture with rowspan="2", we need to shift everything by one to the left. This is stupid and ugly there needs to bee an API
if ( ( sDay > - 1 && sRow > - 1 ) && ( sDay == day && ( ( sRow + 1 ) == rowIndex ) ) ) {
// we found a lecture that is longer than 1 lesson
2019-03-01 18:59:17 +01:00
timeTableWeek . days [ day ] . timeslots [ rowIndex ] . add ( sLesson ) // this just works if there is one lecture per slot
2019-02-28 14:52:40 +01:00
// adjust the following slot
sDay ++
sLesson = Lesson ( element . select ( " div.lesson-subject " ) . text ( ) , element . select ( " div.lesson-teacher " ) . text ( ) , element . select ( " div.lesson-room " ) . text ( ) , element . select ( " div.lesson-remark " ) . text ( ) )
// adjust the slot directly as we don't get there anymore
if ( sDay == 5 ) {
2019-03-01 18:59:17 +01:00
timeTableWeek . days [ day + 1 ] . timeslots [ rowIndex ] . add ( sLesson )
2019-02-28 14:52:40 +01:00
}
} else {
2019-03-01 18:59:17 +01:00
timeTableWeek . days [ day ] . timeslots [ rowIndex ] . add ( Lesson ( element . select ( " div.lesson-subject " ) . text ( ) , element . select ( " div.lesson-teacher " ) . text ( ) , element . select ( " div.lesson-room " ) . text ( ) , element . select ( " div.lesson-remark " ) . text ( ) ) )
2019-02-28 14:52:40 +01:00
}
// we found a lecture with rowspan="2", save day, row and lesson for later adjustment
if ( element . toString ( ) . contains ( " rowspan= \" 2 \" " ) ) {
sDay = day
sRow = rowIndex
2019-03-01 18:59:17 +01:00
sLesson = timeTableWeek . days [ day ] . timeslots [ rowIndex ] . get ( index = 0 )
2019-02-28 14:52:40 +01:00
}
if ( element . hasClass ( " lastcol " ) ) day ++
}
}
2019-03-01 18:59:17 +01:00
printTimeTableWeek ( timeTableWeek )
2019-02-28 14:52:40 +01:00
2018-10-29 18:03:20 +01:00
return timeTableWeek
2018-10-29 13:04:20 +01:00
}
/ * *
* parse all courses from the courses site at https : //www.hs-offenburg.de/studium/vorlesungsplaene/
* /
2019-02-17 15:05:03 +01:00
fun getCourseTTLinks ( ) : ArrayList < Course > {
2018-10-29 13:04:20 +01:00
val courseHTML = Jsoup . connect ( " https://www.hs-offenburg.de/studium/vorlesungsplaene/ " ) . get ( )
courseHTML . select ( " ul.index-group " ) . select ( " li.Class " ) . select ( " a[href] " ) . forEachIndexed { _ , element ->
2019-02-17 15:05:03 +01:00
courseTTLinkList . add ( Course ( element . attr ( " href " ) . replace ( " http " , " https " ) , element . text ( ) ) )
2018-10-29 13:04:20 +01:00
}
2018-11-01 02:12:43 +01:00
return courseTTLinkList
2018-10-29 13:04:20 +01:00
}
2019-03-01 18:59:17 +01:00
@Suppress ( " unused " )
fun printTimeTableWeek ( timetable : TimeTable ) {
2019-02-28 14:52:40 +01:00
for ( j in 0. . 5 ) print ( days [ j ] . padEnd ( 75 , ' ' ) + " | " )
println ( )
for ( j in 0. . 5 ) print ( " - " . padEnd ( 76 + ( j . toFloat ( ) . div ( j ) . toInt ( ) ) , '-' ) + " + " )
2018-10-29 13:04:20 +01:00
println ( )
2019-02-28 14:52:40 +01:00
// the timeslot
for ( i in 0. . 5 ) {
for ( j in 0. . 5 ) {
2019-03-01 18:59:17 +01:00
val ldiff = if ( timetable . days [ j ] . timeslots [ i ] . size == 0 ) 1 else timetable . days [ j ] . timeslots [ i ] . size
2019-02-28 14:52:40 +01:00
2019-03-01 18:59:17 +01:00
for ( lesson in timetable . days [ j ] . timeslots [ i ] ) print ( lesson . lessonSubject . padEnd ( 75 / ldiff , ' ' ) )
2019-02-28 14:52:40 +01:00
if ( ldiff == 2 ) print ( " " )
print ( " | " )
}
println ( )
for ( j in 0. . 5 ) {
2019-03-01 18:59:17 +01:00
val ldiff = if ( timetable . days [ j ] . timeslots [ i ] . size == 0 ) 1 else timetable . days [ j ] . timeslots [ i ] . size
2019-02-28 14:52:40 +01:00
2019-03-01 18:59:17 +01:00
for ( lesson in timetable . days [ j ] . timeslots [ i ] ) print ( lesson . lessonTeacher . padEnd ( 75 / ldiff , ' ' ) )
2019-02-28 14:52:40 +01:00
if ( ldiff == 2 ) print ( " " )
print ( " | " )
}
println ( )
for ( j in 0. . 5 ) {
2019-03-01 18:59:17 +01:00
val ldiff = if ( timetable . days [ j ] . timeslots [ i ] . size == 0 ) 1 else timetable . days [ j ] . timeslots [ i ] . size
2019-02-28 14:52:40 +01:00
2019-03-01 18:59:17 +01:00
for ( lesson in timetable . days [ j ] . timeslots [ i ] ) print ( lesson . lessonRoom . padEnd ( 75 / ldiff , ' ' ) )
2019-02-28 14:52:40 +01:00
if ( ldiff == 2 ) print ( " " )
print ( " | " )
}
println ( )
for ( j in 0. . 5 ) print ( " - " . padEnd ( 76 + ( j . toFloat ( ) . div ( j ) . toInt ( ) ) , '-' ) + " + " )
println ( )
}
println ( " \n " )
2018-10-29 13:04:20 +01:00
}
}