Browse Source

added mensa and timetable parser

pull/18/head
Jannik 4 years ago
parent
commit
0430330801
  1. 3
      build.gradle
  2. 11
      src/main/kotlin/Main.kt
  3. 58
      src/main/kotlin/MensaParser.kt
  4. 80
      src/main/kotlin/TimeTableParser.kt

3
build.gradle

@ -10,7 +10,8 @@ repositories {
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'org.jsoup:jsoup:1.11.3'
}
compileKotlin {

11
src/main/kotlin/Main.kt

@ -0,0 +1,11 @@
fun main() {
// TODO implement
}
data class Lesson(val lessonSubject: String, val lessonTeacher: String, val lessonRoom:String, val lessonRemark: String)
data class CourseTTLink(val courseTTLink: String, val course: String)
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)

58
src/main/kotlin/MensaParser.kt

@ -0,0 +1,58 @@
import org.jsoup.Jsoup
class MensaParser {
fun getMensaMenu() {
val menuHTML = Jsoup.connect("https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/").get()
menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan").forEachIndexed { index, element ->
var day = element.select("h3").text()
for (i in 0 .. (element.select("div.row h4").size - 1)) {
try {
var heading = element.select("div.row h4")[i].text()
var parts = ArrayList<String>(element.select("div.row").select("div.menu-info")[i].html().substringBefore("<span").replace("<br>", "|").split("|"))
var additives = element.select("div.row").select("div.menu-info")[i].select("span.show-with-allergenes").text()
mealList.add(Meal(day, heading, parts, additives))
} catch (e: Exception) {
// catch
}
}
}
}
// TODO The following code can be removed
fun printMensaMenu(mealList: ArrayList<Meal>) {
for (meal in mealList) {
printMeal(meal)
}
println()
}
fun printDayMenu(mealList: ArrayList<Meal>, day: Int) {
var strDay: String = when(day) {
0 -> "Mon"
1 -> "Die"
2 -> "Mit"
3 -> "Don"
4 -> "Fre"
5 -> "Sam"
else -> "TODAY" //TODO
}
for (meal in mealList) {
if (meal.day.contains(strDay)) printMeal(meal)
}
}
fun printMeal(meal: Meal) {
println(meal.day)
println(meal.heading)
for (part in meal.parts) {
print(part)
}
println("\n" + meal.additives + "\n\n")
}
}

80
src/main/kotlin/TimeTableParser.kt

@ -0,0 +1,80 @@
import org.jsoup.Jsoup
class TimeTableParser {
val days = arrayOf("Monday", "Tuesday" ,"Wednesday", "Thursday", "Friday", "Saturday")
var timeTableWeek = arrayOf<Array<Lesson>>()
var courseTTLinkList = ArrayList<CourseTTLink>()
/**
* get the timetable from the given url
* the timetable is organised per row not per column;
* Mon 1, Tue 1, Wed 1, Thur 1, Fri 1, Sat 1, Mon 2 and so on
*/
fun getTimeTable(courseTTURL: String) {
// TODO do we even need this?
// create the timetable array
// for (i in 0..5) {
// var timeTableDay = arrayOf<Lesson>()
// for (j in 0..6) {
// timeTableDay += Lesson("", "","","")
// }
// timeTableWeek += timeTableDay
// }
val scheduleHTML = Jsoup.connect(courseTTURL).get()
val week = scheduleHTML.select("h1.timetable-caption").text()
println("$week successful!\n")
scheduleHTML.select("table.timetable").select("td.lastcol").forEachIndexed { index, element ->
timeTableWeek[index % 6][index / 6] = Lesson(element.select("div.lesson-subject").text(), element.select("div.lesson-teacher").text(), element.select("div.lesson-room").text(), element.select("div.lesson-remark").text())
}
}
/**
* parse all courses from the courses site at https://www.hs-offenburg.de/studium/vorlesungsplaene/
*/
fun getCourseTTLinks() {
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 ->
courseTTLinkList.add(CourseTTLink(element.attr("href"),element.text()))
}
}
fun printTimeTableWeek (timeTableWeek: Array<Array<Lesson>>) {
for (j in 0..5) print(days[j].padEnd(25 ,' ') + " | ")
println()
for (j in 0..5) print("-".padEnd(26 + (j.toFloat().div(j).toInt()), '-') + "+")
println()
for (i in 0..6) {
for (j in 0..5) print(timeTableWeek[j][i].lessonSubject.padEnd(25 ,' ').substring(0,25) + " | ")
println()
for (j in 0..5) print(timeTableWeek[j][i].lessonTeacher.padEnd(25 ,' ').substring(0,25) + " | ")
println()
for (j in 0..5) print(timeTableWeek[j][i].lessonRoom.padEnd(25 ,' ').substring(0,25) + " | ")
println()
for (j in 0..5) print("-".padEnd(26 + (j.toFloat().div(j).toInt()), '-') + "+")
println()
}
println()
}
fun printTimeTableDay(timeTableDay: Array<Lesson>, day: Int) {
println(days[day])
for (i in 0..6) {
println("-".padEnd(22, '-'))
println(timeTableDay[i].lessonSubject)
println(timeTableDay[i].lessonTeacher)
println(timeTableDay[i].lessonRoom)
}
println("-".padEnd(22, '-'))
println()
}
}
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