You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4 KiB

* TheCitadelofRicks
* Copyright 2019-2020 <>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Meal
import org.mosad.thecitadelofricks.MensaWeek
import org.slf4j.LoggerFactory
class MensaParser {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(
* returns the mensa's menu for a week
* @param mensaMenuURL the url to a mensa menu (swfr)
* @return the mensa's menu as MensaWeek found at menuURL or null if the request was not successful
fun getMensaMenu(mensaMenuURL: String): MensaWeek? {
return try {
val menuHTML = Jsoup.connect(mensaMenuURL).timeout(15000).get()
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from $mensaMenuURL, updating on next attempt!")
} catch (gex: Exception) {
logger.error("general MensaParser error", gex)
* parse the mensa's menu from the html document
* @param htmlDoc the html document containing the menu
* @return the mensa's menu as MensaWeek
fun parseMensaMenu(htmlDoc: Document): MensaWeek? {
val mealWeekList = MensaWeek()
try {"#speiseplan-tabs").select("").select("")
.forEachIndexed { dayIndex, day ->
val strDay ="h3").text()"").forEachIndexed { mealIndex, meal ->
val heading ="h4")[mealIndex].text()
val parts = ArrayList(meal.html().substringBefore("<br>\n").replace("\n", "").split("<br>"))
val additives ="").text()
parts.removeIf { x -> x.isEmpty() || x.isBlank() }
mealWeekList.days[dayIndex].meals.add(Meal(strDay, heading, parts, additives))
} catch (pex: Exception) {
logger.error("error while parsing the html file", pex)
return null
return mealWeekList
* return the link of the next weeks menus
* @param mensaMenuURL the current weeks menus link
fun getMenuLinkNextWeek(mensaMenuURL: String): String {
val menuHTML = Jsoup.connect(mensaMenuURL).get()
return "" +"#speiseplan-tabs").select("").attr("href")

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