From e804774970cbec8e78e661f5a182e4ca095a40d1 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Sat, 25 May 2019 19:37:04 +0200 Subject: [PATCH] cover some timeouts --- .../mosad/thecitadelofricks/APIController.kt | 14 +++++--- .../hsoparser/CourseListParser.kt | 27 ++++++++++---- .../hsoparser/MensaParser.kt | 35 ++++++++++++------- .../hsoparser/TimetableParser.kt | 2 +- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt index d9d6d99..c72eb3a 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt @@ -45,12 +45,13 @@ import kotlin.concurrent.scheduleAtFixedRate @RestController class APIController { + // TODO clean up and move stuff to a CacheController // Controller stuff var logger: Logger = LoggerFactory.getLogger(APIController::class.java) private var requestCount = 0 private val startTime = System.currentTimeMillis() / 1000 - private val softwareVersion = "1.0.3" - private val apiVersion = "1.0.2" + private val softwareVersion = "1.1.0" + private val apiVersion = "1.1.0" // hso parser links (hardcoded) private val mensaLink = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/" @@ -89,7 +90,7 @@ class APIController { } } - @Deprecated("courses is replaced by courseList") + @Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()")) @RequestMapping("/courses") fun courses(): CoursesList { return courseList() @@ -172,11 +173,16 @@ class APIController { try { val hsoURL = URL("https://www.hs-offenburg.de/") + val swfrURL = URL("https://www.swfr.de/") + var connection = hsoURL.openConnection() as HttpURLConnection connection.requestMethod = "HEAD" + + connection.connectTimeout = 15000 hsoCode = connection.responseCode - val swfrURL = URL("https://www.swfr.de/") + connection = swfrURL.openConnection() as HttpURLConnection + connection.connectTimeout = 15000 swfrCode = connection.responseCode } catch (e: Exception) { logger.error("Error while fetching url response codes!", e) diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt index ab6cc47..198c02e 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt @@ -24,21 +24,34 @@ package org.mosad.thecitadelofricks.hsoparser import org.jsoup.Jsoup import org.mosad.thecitadelofricks.Course +import org.slf4j.LoggerFactory +import java.net.SocketTimeoutException class CourseListParser { + var logger: org.slf4j.Logger = LoggerFactory.getLogger(MensaParser::class.java) + + /** + * return a list of all courses at hs-offenburg.de/studium/vorlesungsplaene/ + * @return a ArrayList with all courses + */ fun getCourseLinks(): ArrayList { val courseLinkList = ArrayList() - val courseHTML = Jsoup.connect("https://www.hs-offenburg.de/studium/vorlesungsplaene/").get() + try { + val courseHTML = Jsoup.connect("https://www.hs-offenburg.de/studium/vorlesungsplaene/").get() - courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element -> - courseLinkList.add( - Course( - element.text(), - element.attr("href").replace("http", "https") + courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element -> + courseLinkList.add( + Course( + element.text(), + element.attr("href").replace("http", "https") + ) ) - ) + } + } catch (ex: SocketTimeoutException) { + logger.warn("timeout from hs-offenburg.de, updating on next attempt!") } + return courseLinkList } } \ No newline at end of file diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt index 00ed3f0..2a5514a 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt @@ -25,30 +25,39 @@ package org.mosad.thecitadelofricks.hsoparser import org.jsoup.Jsoup import org.mosad.thecitadelofricks.Meal import org.mosad.thecitadelofricks.MensaWeek +import org.slf4j.LoggerFactory +import java.net.SocketTimeoutException class MensaParser { + var logger: org.slf4j.Logger = LoggerFactory.getLogger(MensaParser::class.java) + /** - * returns the mensa menu for the a week + * returns the mensa menu for a week + * @param menuLink the url to a mensa menu (swfr) */ fun getMensaMenu(menuLink: String): MensaWeek { val mealWeekList = MensaWeek() - val menuHTML = Jsoup.connect(menuLink).get() + try { + val menuHTML = Jsoup.connect(menuLink).timeout(15000).get() - menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan") - .forEachIndexed { dayIndex, day -> - val strDay = day.select("h3").text() + menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan") + .forEachIndexed { dayIndex, day -> + val strDay = day.select("h3").text() - day.select("div.menu-info").forEachIndexed { mealIndex, meal -> - val heading = day.select("h4")[mealIndex].text() - val parts = ArrayList(meal.html().substringBefore("
\n").replace("\n", "").split("
")) - val additives = meal.select("span.show-with-allergenes").text() - parts.removeIf { x -> x.isEmpty() || x.isBlank() } + day.select("div.menu-info").forEachIndexed { mealIndex, meal -> + val heading = day.select("h4")[mealIndex].text() + val parts = ArrayList(meal.html().substringBefore("
\n").replace("\n", "").split("
")) + val additives = meal.select("span.show-with-allergenes").text() + parts.removeIf { x -> x.isEmpty() || x.isBlank() } + + mealWeekList.days[dayIndex].meals.add(Meal(strDay, heading, parts, additives)) + } - mealWeekList.days[dayIndex].meals.add(Meal(strDay, heading, parts, additives)) } - - } + } catch (ex: SocketTimeoutException) { + logger.warn("timeout from $menuLink, updating on next attempt!") + } return mealWeekList } diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt index a74dda4..9134cec 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt @@ -37,7 +37,7 @@ class TimetableParser { */ fun getTimeTable(timetableURL: String): TimetableWeek { val timetableWeek = TimetableWeek() - val scheduleHTML = Jsoup.connect(timetableURL).get() + val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a tyr catch block to cover timeouts //val week = scheduleHTML.select("h1.timetable-caption").text() //println("$week successful!\n")