2019-03-11 20:12:49 +01:00
/ * *
* TheCitadelofRicks
*
* Copyright 2019 < seil0 @mosad . xyz >
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* 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
2019-03-12 22:06:04 +01:00
import org.jsoup.Jsoup
2019-08-16 13:59:44 +02:00
import org.jsoup.nodes.Document
2019-03-11 20:12:49 +01:00
import org.mosad.thecitadelofricks.Meal
2019-03-12 22:06:04 +01:00
import org.mosad.thecitadelofricks.MensaWeek
2019-05-25 19:37:04 +02:00
import org.slf4j.LoggerFactory
import java.net.SocketTimeoutException
2019-03-11 20:12:49 +01:00
class MensaParser {
2019-05-25 19:37:04 +02:00
var logger : org . slf4j . Logger = LoggerFactory . getLogger ( MensaParser :: class . java )
2019-03-11 20:12:49 +01:00
/ * *
2019-08-16 13:59:44 +02:00
* returns the mensa ' s menu for a week
2019-05-28 12:49:25 +02:00
* @param mensaMenuURL the url to a mensa menu ( swfr )
2019-08-16 13:59:44 +02:00
* @return the mensa ' s menu as MensaWeek found at menuURL or null if the request was not successful
2019-03-11 20:12:49 +01:00
* /
2019-05-28 12:49:25 +02:00
fun getMensaMenu ( mensaMenuURL : String ) : MensaWeek ? {
2019-08-16 13:59:44 +02:00
return try {
2019-05-28 12:49:25 +02:00
val menuHTML = Jsoup . connect ( mensaMenuURL ) . timeout ( 15000 ) . get ( )
2019-08-16 13:59:44 +02:00
parseMensaMenu ( menuHTML )
} catch ( ex : SocketTimeoutException ) {
logger . warn ( " timeout from $mensaMenuURL , updating on next attempt! " )
null
} catch ( gex : Exception ) {
logger . error ( " general MensaParser error " , gex )
null
}
}
/ * *
* 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 ( )
2019-03-11 20:12:49 +01:00
2019-08-16 13:59:44 +02:00
try {
htmlDoc . select ( " #speiseplan-tabs " ) . select ( " div.tab-content " ) . select ( " div.menu-tagesplan " )
2019-05-25 19:37:04 +02:00
. forEachIndexed { dayIndex , day ->
val strDay = day . select ( " h3 " ) . text ( )
2019-03-11 20:12:49 +01:00
2019-05-25 19:37:04 +02:00
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 ( ) }
2019-03-11 20:12:49 +01:00
2019-05-25 19:37:04 +02:00
mealWeekList . days [ dayIndex ] . meals . add ( Meal ( strDay , heading , parts , additives ) )
}
2019-03-11 20:12:49 +01:00
2019-05-25 19:37:04 +02:00
}
2019-08-16 13:59:44 +02:00
} catch ( pex : Exception ) {
logger . error ( " error while parsing the html file " , pex )
2019-05-28 12:49:25 +02:00
return null
2019-05-25 19:37:04 +02:00
}
2019-03-11 20:12:49 +01:00
return mealWeekList
}
/ * *
2019-04-06 11:33:21 +02:00
* return the link of the next weeks menus
2019-05-28 12:49:25 +02:00
* @param mensaMenuURL the current weeks menus link
2019-03-11 20:12:49 +01:00
* /
2019-05-28 12:49:25 +02:00
fun getMenuLinkNextWeek ( mensaMenuURL : String ) : String {
val menuHTML = Jsoup . connect ( mensaMenuURL ) . get ( )
2019-03-11 20:12:49 +01:00
return " https://www.swfr.de " + menuHTML . select ( " #speiseplan-tabs " ) . select ( " a.next-week " ) . attr ( " href " )
}
}