Browse Source

Better null checks

pull/17/head
Hannes Braun 8 months ago
parent
commit
5ba9dfc263
Signed by: hannesbraun
GPG Key ID: 7B6557E1DFD685BE
  1. 44
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

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

@ -42,37 +42,29 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
val semaphore = Semaphore(3, 0)
}
private val htmlDoc: Document? =
htmlDoc
?: if (timetableURL == null) {
private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let {
runBlocking {
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
} else {
runBlocking {
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()
}
}
} finally {
semaphore.release()
}
}
}
/**
* 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
*/
fun parseTimeTable(): TimetableWeek? {
if (htmlDoc == null) {
return null
}
fun parseTimeTable(): TimetableWeek? = htmlDoc?.let {
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 sRow = -1
@ -141,12 +133,8 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
/**
* parse the week number of the year for the timetable
*/
fun parseWeekNumberYear(): Int? {
if (htmlDoc == null) {
return null
}
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
fun parseWeekNumberYear(): Int? = htmlDoc?.let {
it.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
}

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