clean up APICOntroller & new CacheController

* added a CacheController to hold all cache variables and clean up the APIController
* more consistent naming of variables
* update mensaMenu and courseList only if the request was successful
This commit is contained in:
2019-05-28 12:49:25 +02:00
parent 9efc7fe4b7
commit 754c8cb17b
7 changed files with 221 additions and 170 deletions

View File

@ -32,13 +32,14 @@ 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
* return a list of all courses at courseListURL
* @param courseListURL the url to the course list page
* @return a ArrayList<Course> with all courses or null if the request was not successful
*/
fun getCourseLinks(): ArrayList<Course> {
fun getCourseLinks(courseListURL: String): ArrayList<Course>? {
val courseLinkList = ArrayList<Course>()
try {
val courseHTML = Jsoup.connect("https://www.hs-offenburg.de/studium/vorlesungsplaene/").get()
val courseHTML = Jsoup.connect(courseListURL).get()
courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element ->
courseLinkList.add(
@ -50,8 +51,10 @@ class CourseListParser {
}
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from hs-offenburg.de, updating on next attempt!")
return null
} catch (gex: Exception) {
logger.error("general CourseListParser error", gex)
return null
}
return courseLinkList

View File

@ -34,12 +34,13 @@ class MensaParser {
/**
* returns the mensa menu for a week
* @param menuLink the url to a mensa menu (swfr)
* @param mensaMenuURL the url to a mensa menu (swfr)
* @return the menu plan found at menuURL or null if the request was not successful
*/
fun getMensaMenu(menuLink: String): MensaWeek {
fun getMensaMenu(mensaMenuURL: String): MensaWeek? {
val mealWeekList = MensaWeek()
try {
val menuHTML = Jsoup.connect(menuLink).timeout(15000).get()
val menuHTML = Jsoup.connect(mensaMenuURL).timeout(15000).get()
menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan")
.forEachIndexed { dayIndex, day ->
@ -56,9 +57,11 @@ class MensaParser {
}
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from $menuLink, updating on next attempt!")
logger.warn("timeout from $mensaMenuURL, updating on next attempt!")
return null
} catch (gex: Exception) {
logger.error("general MensaParser error", gex)
return null
}
return mealWeekList
@ -66,10 +69,10 @@ class MensaParser {
/**
* return the link of the next weeks menus
* @param menuLink the current weeks menus link
* @param mensaMenuURL the current weeks menus link
*/
fun getMenuLinkNextWeek(menuLink: String): String {
val menuHTML = Jsoup.connect(menuLink).get()
fun getMenuLinkNextWeek(mensaMenuURL: String): String {
val menuHTML = Jsoup.connect(mensaMenuURL).get()
return "https://www.swfr.de" + menuHTML.select("#speiseplan-tabs").select("a.next-week").attr("href")
}

View File

@ -37,7 +37,7 @@ class TimetableParser {
*/
fun getTimeTable(timetableURL: String): TimetableWeek {
val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a tyr catch block to cover timeouts
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a try catch block to cover timeouts
//val week = scheduleHTML.select("h1.timetable-caption").text()
//println("$week successful!\n")