Timetable fixes

- Only one request is made to get the timetable HTML document for parsing the timetable and the weekNumberYear
- On timeouts or other errors, the cached data won't be overwritten with emptiness anymore
- The scheduled updates will now also update the weekNumberYear
This commit is contained in:
2021-10-13 15:57:26 +02:00
parent ae9bf2a562
commit 22f17d10e0
4 changed files with 66 additions and 42 deletions

View File

@ -28,26 +28,37 @@ import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
import org.slf4j.LoggerFactory
class TimetableParser {
/**
* @param timetableURL the URL of the timetable you want to get
* @param htmlDoc the html document to use (the timetableURL will be ignored if this value is present)
*/
class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
private val htmlDoc: Document? =
htmlDoc
?: if (timetableURL == null) {
null
} else {
try {
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
}
}
/**
* get the timetable from the given url
* parse the timetable from the previously 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
* @param timetableURL the URL of the timetable you want to get
*/
fun getTimeTable(timetableURL: String): TimetableWeek {
return try {
parseTimeTable(Jsoup.connect(timetableURL).get())
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
TimetableWeek()
fun parseTimeTable(): TimetableWeek? {
if (htmlDoc == null) {
return null
}
}
fun parseTimeTable(htmlDoc: Document): TimetableWeek {
val timetableWeek = TimetableWeek()
val rows = htmlDoc.select("table.timetable").select("tr[scope=\"row\"]")
@ -117,19 +128,13 @@ class TimetableParser {
}
/**
* get the week number of the year for the timetable
* @param timetableURL the URL of the timetable you want to get
* parse the week number of the year for the timetable
*/
fun getWeekNumberYear(timetableURL: String): Int {
return try {
parseWeekNumberYear(Jsoup.connect(timetableURL).get())
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
0
fun parseWeekNumberYear(): Int? {
if (htmlDoc == null) {
return null
}
}
fun parseWeekNumberYear(htmlDoc: Document): Int {
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
}