Various improvements #17

Merged
Seil0 merged 11 commits from :fix/timeout-resilience into master 2021-10-24 14:38:13 +02:00
Showing only changes of commit 5ba9dfc263 - Show all commits

View File

@ -42,37 +42,29 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
val semaphore = Semaphore(3, 0) val semaphore = Semaphore(3, 0)
} }
private val htmlDoc: Document? = private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let {
hannesbraun marked this conversation as resolved Outdated
Outdated
Review

please us ?.let { ... } for null safety. Something like this:

private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let {
    runBlocking {
        ...
    }
}
please us `?.let { ... }` for null safety. Something like this: ```kotlin private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let { runBlocking { ... } } ```

This should be fixed with ca14a5d2f6

This should be fixed with ca14a5d2f6
htmlDoc runBlocking {
?: if (timetableURL == null) { try {
// Only allow sending a limited amount of requests at the same time
semaphore.acquire()
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null null
} else { } finally {
runBlocking { semaphore.release()
try {
// Only allow sending a limited amount of requests at the same time
semaphore.acquire()
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
} finally {
semaphore.release()
}
}
} }
}
}
/** /**
* parse the timetable from the previously given url * parse the timetable from the previously given url
* the timetable is organised per row not per column; * 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 * Mon 1, Tue 1, Wed 1, Thur 1, Fri 1, Sat 1, Mon 2 and so on
*/ */
fun parseTimeTable(): TimetableWeek? { fun parseTimeTable(): TimetableWeek? = htmlDoc?.let {
if (htmlDoc == null) {
return null
}
val timetableWeek = TimetableWeek() val timetableWeek = TimetableWeek()
val rows = htmlDoc.select("table.timetable").select("tr[scope=\"row\"]") val rows = it.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1 var sDay = -1
var sRow = -1 var sRow = -1
@ -141,12 +133,8 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
/** /**
* parse the week number of the year for the timetable * parse the week number of the year for the timetable
*/ */
fun parseWeekNumberYear(): Int? { fun parseWeekNumberYear(): Int? = htmlDoc?.let {
if (htmlDoc == null) { it.select("h1.timetable-caption").text().substringAfter("- ")
return null
}
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt() .substringBefore(".").replace(" ", "").toInt()
} }