Browse Source

Merge pull request 'Parse the year of a timetable' (#21) from hannesbraun/TheCitadelofRicks:feature/parse-year into master

Reviewed-on: #21
feature/CoroutineScope
Jannik 8 months ago
parent
commit
940a30e9aa
  1. 5
      src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt
  2. 9
      src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt
  3. 11
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt
  4. 7
      src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt

5
src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt

@ -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 classes for the timetable part
data class CalendarWeek(val week: Int, val year: Int)
data class Lesson(
val lessonID: 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 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())

9
src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt

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

11
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

@ -26,6 +26,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.CalendarWeek
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
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 {
it.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
fun parseCalendarWeek(): CalendarWeek? = htmlDoc?.let {
val dateStr = it.select("h1.timetable-caption").text().substringAfter("- ")
val week = dateStr.substringBefore(".").replace(" ", "").toInt()
val year = dateStr.substringAfter("Woche ").replace(" ", "").toInt()
CalendarWeek(week, year)
}
@Suppress("unused")

7
src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt

@ -25,6 +25,7 @@ package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mosad.thecitadelofricks.CalendarWeek
import java.io.File
class TimetableParserTest {
@ -50,11 +51,11 @@ class TimetableParserTest {
}
@Test
fun parseWeekNumberYear() {
fun parseCalendarWeek() {
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 actualWeekNumberYear = TimetableParser(htmlDoc = htmlDoc).parseWeekNumberYear()
val actualCalendarWeek = TimetableParser(htmlDoc = htmlDoc).parseCalendarWeek()
Assertions.assertEquals(42, actualWeekNumberYear)
Assertions.assertEquals(CalendarWeek(42, 2019), actualCalendarWeek)
}
}
Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close