TimetableParser: parse year
continuous-integration/woodpecker the build was successful Details

This also adds the parsed year to the meta object included in the timetable response
This commit is contained in:
Hannes Braun 2021-10-23 23:39:18 +02:00
parent dc57a0d0c1
commit d863f4fb1e
Signed by: hannesbraun
GPG Key ID: 7B6557E1DFD685BE
4 changed files with 21 additions and 11 deletions

View File

@ -46,6 +46,9 @@ data class MensaMeta(val updateTime: Long, val mensaName: String)
data class MensaMenu(val meta: MensaMeta, val currentWeek: MensaWeek, val nextWeek: MensaWeek) data class MensaMenu(val meta: MensaMeta, val currentWeek: MensaWeek, val nextWeek: MensaWeek)
// data classes for the timetable part // data classes for the timetable part
data class CalendarWeek(val week: Int, val year: Int)
data class Lesson( data class Lesson(
val lessonID: String, val lessonID: String,
val lessonSubject: String, val lessonSubject: String,
@ -58,7 +61,7 @@ data class TimetableDay(val timeslots: Array<ArrayList<Lesson>> = Array(6) { Arr
data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() }) data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
data class TimetableCourseMeta(var updateTime: Long = 0, val courseName: String = "", val weekIndex: Int = 0, var weekNumberYear: Int = 0, val link: String = "") data class TimetableCourseMeta(var updateTime: Long = 0, val courseName: String = "", val weekIndex: Int = 0, var weekNumberYear: Int = 0, val year: Int = 0, val link: String = "")
data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeek = TimetableWeek()) data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeek = TimetableWeek())

View File

@ -68,6 +68,7 @@ class CacheController {
val currentTime = System.currentTimeMillis() / 1000 val currentTime = System.currentTimeMillis() / 1000
val timetableLink = "https://mosad.xyz" val timetableLink = "https://mosad.xyz"
val weekNumberYear = 0 val weekNumberYear = 0
val year = 0
val instr = CacheController::class.java.getResourceAsStream("/html/Timetable_normal-week.html") val instr = CacheController::class.java.getResourceAsStream("/html/Timetable_normal-week.html")
val timetableParser = val timetableParser =
@ -80,6 +81,7 @@ class CacheController {
courseName, courseName,
weekIndex, weekIndex,
weekNumberYear, weekNumberYear,
year,
timetableLink timetableLink
), timetableTest ?: TimetableWeek() ), timetableTest ?: TimetableWeek()
) )
@ -95,7 +97,7 @@ class CacheController {
val currentTime = System.currentTimeMillis() / 1000 val currentTime = System.currentTimeMillis() / 1000
val timetableParser = TimetableParser(timetableLink) val timetableParser = TimetableParser(timetableLink)
val weekNumberYear = timetableParser.parseWeekNumberYear() val calendarWeek = timetableParser.parseCalendarWeek()
val timetable = timetableParser.parseTimeTable() val timetable = timetableParser.parseTimeTable()
TimetableCourseWeek( TimetableCourseWeek(
@ -103,7 +105,8 @@ class CacheController {
currentTime, currentTime,
courseName, courseName,
weekIndex, weekIndex,
weekNumberYear ?: 0, calendarWeek?.week ?: 0,
calendarWeek?.year ?: 0,
timetableLink timetableLink
), timetable ?: TimetableWeek() ), timetable ?: TimetableWeek()
).also { if (timetable != null) timetableList[key] = it } ).also { if (timetable != null) timetableList[key] = it }
@ -198,7 +201,7 @@ class CacheController {
val timetableParser = TimetableParser(timetableCourse.value.meta.link) val timetableParser = TimetableParser(timetableCourse.value.meta.link)
timetableCourse.value.timetable = timetableParser.parseTimeTable() ?: return@execute timetableCourse.value.timetable = timetableParser.parseTimeTable() ?: return@execute
timetableCourse.value.meta.weekNumberYear = timetableCourse.value.meta.weekNumberYear =
timetableParser.parseWeekNumberYear() ?: return@execute timetableParser.parseCalendarWeek()?.week ?: return@execute
timetableCourse.value.meta.updateTime = System.currentTimeMillis() / 1000 timetableCourse.value.meta.updateTime = System.currentTimeMillis() / 1000
saveTimetableToCache(timetableCourse.value) // save the updated timetable to the cache directory saveTimetableToCache(timetableCourse.value) // save the updated timetable to the cache directory

View File

@ -26,6 +26,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.CalendarWeek
import org.mosad.thecitadelofricks.Lesson import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek import org.mosad.thecitadelofricks.TimetableWeek
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -131,11 +132,13 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
} }
/** /**
* parse the week number of the year for the timetable * parse the calendar week and the associated year for the timetable
*/ */
fun parseWeekNumberYear(): Int? = htmlDoc?.let { fun parseCalendarWeek(): CalendarWeek? = htmlDoc?.let {
it.select("h1.timetable-caption").text().substringAfter("- ") val dateStr = it.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt() val week = dateStr.substringBefore(".").replace(" ", "").toInt()
val year = dateStr.substringAfter("Woche ").replace(" ", "").toInt()
CalendarWeek(week, year)
} }
@Suppress("unused") @Suppress("unused")

View File

@ -25,6 +25,7 @@ package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mosad.thecitadelofricks.CalendarWeek
import java.io.File import java.io.File
class TimetableParserTest { class TimetableParserTest {
@ -50,11 +51,11 @@ class TimetableParserTest {
} }
@Test @Test
fun parseWeekNumberYear() { fun parseCalendarWeek() {
val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_normal-week.html").path) val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile, "UTF-8", "https://www.hs-offenburg.de/") val htmlDoc = Jsoup.parse(htmlFile, "UTF-8", "https://www.hs-offenburg.de/")
val actualWeekNumberYear = TimetableParser(htmlDoc = htmlDoc).parseWeekNumberYear() val actualCalendarWeek = TimetableParser(htmlDoc = htmlDoc).parseCalendarWeek()
Assertions.assertEquals(42, actualWeekNumberYear) Assertions.assertEquals(CalendarWeek(42, 2019), actualCalendarWeek)
} }
} }