parse the week number of the year too

This commit is contained in:
2019-09-06 23:10:35 +02:00
parent ea6be1db33
commit 5e220225f6
8 changed files with 1824 additions and 16 deletions

View File

@ -44,8 +44,8 @@ class APIController {
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
private val cache = CacheController()
private val apiVersion = "1.1.1"
private val softwareVersion = "1.1.3"
private val apiVersion = "1.1.2"
private val softwareVersion = "1.1.4"
private val startTime = System.currentTimeMillis() / 1000
private var requestCount = 0

View File

@ -58,29 +58,31 @@ class CacheController {
scheduledUpdates()
}
fun getTimetable(courseName: String, week: Int): TimetableCourseWeek = runBlocking {
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
val currentTime = System.currentTimeMillis() / 1000
var timetable = TimetableWeek()
var weekNumberYear = 0
// check if the timetable already exists and is up to date
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null)) {
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
// there is no such course yet, create one
null -> {
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
val timetableMeta = TimetableCourseMeta(currentTime, courseName, week, courseLink.replace("week=0","week=$week"))
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
val jobTimetable = GlobalScope.async {
timetable = TimetableParser().getTimeTable(timetableMeta.link)
timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
}
jobTimetable.await()
timetableList.add(TimetableCourseWeek(timetableMeta, timetable))
logger.info("added new timetable for $courseName, week $week")
timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
logger.info("added new timetable for $courseName, week $weekIndex")
}
}
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null)
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
}
/**

View File

@ -56,7 +56,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, val courseName: String, val week: Int, val link: String)
data class TimetableCourseMeta(var updateTime: Long, val courseName: String, val weekIndex: Int, val weekNumberYear: Int, val link: String)
data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: TimetableWeek)

View File

@ -23,6 +23,7 @@
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
@ -39,9 +40,6 @@ class TimetableParser {
val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a try catch block to cover timeouts
//val week = scheduleHTML.select("h1.timetable-caption").text()
//println("$week successful!\n")
val rows = scheduleHTML.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1
var sRow = -1
@ -105,11 +103,26 @@ class TimetableParser {
}
//printTimetableWeek(timetableWeek)
return timetableWeek
}
/**
* get the week number of the year for the timetable
* @param timetableURL the URL of the timetable you want to get
*/
fun getWeekNumberYear(timetableURL: String): Int {
return try {
parseWeekNumberYear(Jsoup.connect(timetableURL).get())
} catch (ex: Exception) {
0
}
}
fun parseWeekNumberYear(htmlDoc: Document): Int {
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
}
@Suppress("unused")
/**
* print a timetable