Browse Source

cover some timeouts

pull/18/head 1.1.0
Jannik 3 years ago
parent
commit
e804774970
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 14
      src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt
  2. 27
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt
  3. 35
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt
  4. 2
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

14
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)

27
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<Course> with all courses
*/
fun getCourseLinks(): ArrayList<Course> {
val courseLinkList = ArrayList<Course>()
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
}
}

35
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("<br>\n").replace("\n", "").split("<br>"))
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("<br>\n").replace("\n", "").split("<br>"))
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
}

2
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")

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