added spring and a API specific stuff

this commit adds a fully working API wit /courses, /timetable?courseName=[course] and /mensamenu
This commit is contained in:
2019-03-12 22:06:04 +01:00
parent ace2cb1e39
commit 6f2bed65ab
7 changed files with 247 additions and 77 deletions

View File

@ -20,7 +20,6 @@
*
*/
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
@ -29,17 +28,17 @@ import org.mosad.thecitadelofricks.Course
class CourseListParser {
fun getCourseLinks(): ArrayList<Course> {
val courseTTLinkList = ArrayList<Course>() // TODO val may cause bugs!
val courseLinkList = ArrayList<Course>()
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(
courseLinkList.add(
Course(
element.attr("href").replace("http", "https"),
element.text()
element.text(),
element.attr("href").replace("http", "https")
)
)
}
return courseTTLinkList
return courseLinkList
}
}

View File

@ -22,54 +22,36 @@
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.jsoup.Jsoup
import org.mosad.thecitadelofricks.Meal
import org.mosad.thecitadelofricks.MealWeek
import org.mosad.thecitadelofricks.MensaWeek
class MensaParser {
/**
* returns the mensa menu for the a week
*/
fun getMensaMenu(menuLink: String): MealWeek {
val mealList = ArrayList<Meal>()
val mealWeekList = MealWeek()
fun getMensaMenu(menuLink: String): MensaWeek {
val mealWeekList = MensaWeek()
val menuHTML = Jsoup.connect(menuLink).get()
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("<br>", " ").split("\n"))
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.day[dayIndex].add(Meal(strDay, heading, parts, additives))
mealWeekList.days[dayIndex].meals.add(Meal(strDay, heading, parts, additives))
}
for (i in 0..(day.select("div.row h4").size - 1)) {
try {
val heading = day.select("div.row h4")[i].text()
val parts = ArrayList<String>(
day.select("div.row").select("div.menu-info")[i].html().substringBefore("<span").replace(
"<br>",
" "
).split("\n")
)
val additives =
day.select("div.row").select("div.menu-info")[i].select("span.show-with-allergenes").text()
mealList.add(Meal(strDay, heading, parts, additives))
} catch (e: Exception) {
//println("Oooups! Something went wrong: ${e.printStackTrace()}")
}
}
}
// Mon to Sat (0 - 5)
println(mealWeekList.day[4])
//println(mealWeekList.days[4])
return mealWeekList
}

View File

@ -24,9 +24,9 @@ package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimeTable
import org.mosad.thecitadelofricks.TimetableWeek
class TimeTableParser {
class TimetableParser {
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
/**
@ -34,8 +34,8 @@ class TimeTableParser {
* 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): TimeTable {
val timeTableWeek = TimeTable()
fun getTimeTable(courseTTURL: String): TimetableWeek {
val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(courseTTURL).get()
//val week = scheduleHTML.select("h1.timetable-caption").text()
@ -44,23 +44,24 @@ class TimeTableParser {
val rows = scheduleHTML.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1
var sRow = -1
var sLesson = Lesson("", "", "", "")
var sLesson = Lesson("", "", "", "", "")
// get each row with index, reflects 1 timeslot per day
for ((rowIndex, row) in rows.withIndex()) {
var day = 0
// elements are now all lessons, including empty ones
row.select("td.lastcol, td[style]").forEach { element ->
row.select("td.lastcol, td[style]").forEachIndexed {elementIndex, element ->
// if there is a lecture with rowspan="2", we need to shift everything by one to the left. This is stupid and ugly there needs to bee an API
if ((sDay > -1 && sRow > -1) && (sDay == day && ((sRow + 1) == rowIndex))) {
// we found a lecture that is longer than 1 lesson
timeTableWeek.days[day].timeslots[rowIndex].add(sLesson) // this just works if there is one lecture per slot
timetableWeek.days[day].timeslots[rowIndex].add(sLesson) // this just works if there is one lecture per slot
// adjust the following slot
sDay++
sLesson = Lesson(
"$day.$rowIndex.$elementIndex",
element.select("div.lesson-subject").text(),
element.select("div.lesson-teacher").text(),
element.select("div.lesson-room").text(),
@ -69,12 +70,13 @@ class TimeTableParser {
// adjust the slot directly as we don't get there anymore
if (sDay == 5) {
timeTableWeek.days[day + 1].timeslots[rowIndex].add(sLesson)
timetableWeek.days[day + 1].timeslots[rowIndex].add(sLesson)
}
} else {
timeTableWeek.days[day].timeslots[rowIndex].add(
timetableWeek.days[day].timeslots[rowIndex].add(
Lesson(
"$day.$rowIndex.$elementIndex",
element.select("div.lesson-subject").text(),
element.select("div.lesson-teacher").text(),
element.select("div.lesson-room").text(),
@ -87,7 +89,7 @@ class TimeTableParser {
if (element.toString().contains("rowspan=\"2\"")) {
sDay = day
sRow = rowIndex
sLesson = timeTableWeek.days[day].timeslots[rowIndex].get(index = 0)
sLesson = timetableWeek.days[day].timeslots[rowIndex].get(index = 0)
}
if (element.hasClass("lastcol")) day++
@ -95,12 +97,13 @@ class TimeTableParser {
}
printTimeTableWeek(timeTableWeek)
//printTimetableWeek(timetableWeek)
return timeTableWeek
return timetableWeek
}
fun printTimeTableWeek(timetable: TimeTable) {
@Suppress("unused")
fun printTimetableWeek(timetable: TimetableWeek) {
for (j in 0..5) print(days[j].padEnd(75, ' ') + " | ")
println()
for (j in 0..5) print("-".padEnd(76 + (j.toFloat().div(j).toInt()), '-') + "+")