Browse Source

added the mensa parser

pull/20/head
Jannik 4 years ago
parent
commit
47d48fb4d9
  1. 1
      app/build.gradle
  2. 1
      app/src/main/AndroidManifest.xml
  3. 28
      app/src/main/java/ord/mosad/seil0/projectlaogai/uicomponents/MensDayCardView.kt
  4. 33
      app/src/main/java/org/mosad/seil0/projectlaogai/HomeFragment.kt
  5. 100
      app/src/main/java/org/mosad/seil0/projectlaogai/MensaFragment.kt
  6. 14
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/DataTypes.kt
  7. 82
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/MensaParser.kt
  8. 101
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/TimeTableParser.kt
  9. 2
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/LessonCardView.kt
  10. 54
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/MensaDayCardView.kt
  11. 6
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/MenuCardView.kt
  12. 2
      app/src/main/res/layout/menu_cardview.xml

1
app/build.gradle

@ -30,6 +30,7 @@ dependencies {
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
implementation 'org.jsoup:jsoup:1.11.3'
implementation "org.jetbrains.anko:anko-commons:0.10.4"
implementation 'com.afollestad.material-dialogs:core:2.0.0-beta4'
implementation 'com.afollestad.material-dialogs:color:2.0.0-beta4'

1
app/src/main/AndroidManifest.xml

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mosad.seil0.projectlaogai">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"

28
app/src/main/java/ord/mosad/seil0/projectlaogai/uicomponents/MensDayCardView.kt

@ -1,28 +0,0 @@
package ord.mosad.seil0.projectlaogai.uicomponents
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import android.widget.TextView
import org.mosad.seil0.projectlaogai.R
class MensaDayCardView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : androidx.cardview.widget.CardView(context, attrs){
private var linLayoutMensaDay: LinearLayout
private var txtViewDayHeading: TextView
init {
inflate(context, R.layout.mensaday_cardview,this)
linLayoutMensaDay = findViewById(R.id.linLayout_MensaDay)
txtViewDayHeading = findViewById(R.id.txtView_DayHeading)
// workaround to prevent a white border
//this.setBackgroundColor(Color.TRANSPARENT)
}
fun getLinLayoutMensaDay(): LinearLayout {
return linLayoutMensaDay
}
}

33
app/src/main/java/org/mosad/seil0/projectlaogai/HomeFragment.kt

@ -32,7 +32,13 @@ import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import ord.mosad.seil0.projectlaogai.uicomponents.LessonCardView
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.MensaParser
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
import java.util.*
/**
* The "home" controller class
@ -57,7 +63,8 @@ class HomeFragment : Fragment() {
scrollViewTimeTable = view.findViewById(R.id.scrollView_TimeTable)
linLayoutTimeTable = view.findViewById(R.id.linLayout_TimeTable)
setText()
//setText()
addCurentMensaMenu()
addLessons()
@ -65,10 +72,24 @@ class HomeFragment : Fragment() {
return view
}
@SuppressLint("SetTextI18n")
private fun setText() {
txtViewMenu1.text = "Buntes Pfannengemüse\nCouscous\nBlattsalat"
txtViewMenu2.text = "Riesen Currywurst\nCurryketchup\nGitterkartoffeln\nBlattsalat"
private fun addCurentMensaMenu() {
doAsync {
val mensaParser = MensaParser()
val dayMenus: ArrayList<Meal> = mensaParser.getMensaMenuDay(mensaParser.getMensaMenu(), Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
uiThread {
for(part in dayMenus[0].parts) {
txtViewMenu1.append(part)
}
for(part in dayMenus[1].parts) {
txtViewMenu2.append(part)
}
}
}
}
@SuppressLint("SetTextI18n")

100
app/src/main/java/org/mosad/seil0/projectlaogai/MensaFragment.kt

@ -29,8 +29,14 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import ord.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import ord.mosad.seil0.projectlaogai.uicomponents.MenuCardView
import kotlinx.android.synthetic.main.mensaday_cardview.view.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.MensaParser
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MenuCardView
import java.util.*
/**
* The mensa controller class
@ -46,40 +52,86 @@ class MensaFragment : Fragment() {
linLayoutMensaFragment = view.findViewById(R.id.linLayout_MensaFragment)
addDay()
addCurrentWeek()
//addDay(Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
return view
}
@SuppressLint("SetTextI18n")
fun addDay() {
fun addCurrentWeek() {
doAsync {
val mensaParser = MensaParser()
val weekMenus: ArrayList<Meal> = mensaParser.getMensaMenu()
uiThread {
for(day in Calendar.getInstance().get(Calendar.DAY_OF_WEEK)..7) {
var strDay: String = when(day) {
Calendar.MONDAY -> "Mon"
Calendar.TUESDAY -> "Die"
Calendar.WEDNESDAY -> "Mit"
Calendar.THURSDAY -> "Don"
Calendar.FRIDAY -> "Fre"
Calendar.SATURDAY -> "Sam"
else -> "TODAY" //TODO
}
val cardViewMensaDay = MensaDayCardView(context!!, null)
var add = false
for (meal in weekMenus) {
//println("Day: " + meal.day)
if (meal.day.contains(strDay)) {
val menuViewMenu = MenuCardView(context!!, null)
menuViewMenu.setMenuHeading(meal.heading)
for(part in meal.parts) {
menuViewMenu.getTxtViewMenu().append(part)
}
cardViewMensaDay.setDayHeading(meal.day) //TODO move this out of the first for loop, performance!!
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu)
add = true
}
}
if(add)
linLayoutMensaFragment.addView(cardViewMensaDay)
}
}
}
}
fun addDay(day: Int) {
val cardViewMensaDay = MensaDayCardView(context!!, null)
val menuViewMenu1 = MenuCardView(context!!, null)
val menuViewMenu2 = MenuCardView(context!!, null)
menuViewMenu1.getTxtViewMenuHeading().text = "Essen 1"
menuViewMenu1.getTxtViewMenu().text = "Buntes Pfannengemüse\nCouscous\nBlattsalat"
menuViewMenu2.getTxtViewMenuHeading().text = "Essen 2"
menuViewMenu2.getTxtViewMenu().text = "Riesen Currywurst\nCurryketchup\nGitterkartoffeln\nBlattsalat"
doAsync {
val mensaParser = MensaParser()
val dayMenus: ArrayList<Meal> = mensaParser.getMensaMenuDay(mensaParser.getMensaMenu(), day)
uiThread {
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu1)
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu2)
linLayoutMensaFragment.addView(cardViewMensaDay)
for (meal in dayMenus) {
val menuViewMenu = MenuCardView(context!!, null)
menuViewMenu.setMenuHeading(meal.heading)
for(part in meal.parts) {
menuViewMenu.getTxtViewMenu().append(part)
}
val cardViewMensaDay2 = MensaDayCardView(context!!, null)
val menuViewMenu12 = MenuCardView(context!!, null)
val menuViewMenu22 = MenuCardView(context!!, null)
cardViewMensaDay.setDayHeading(meal.day) //TODO move this out of the first for loop, performance!!
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu)
}
menuViewMenu12.getTxtViewMenuHeading().text = "Essen 12"
menuViewMenu12.getTxtViewMenu().text = "Buntes Pfannengemüse\nCouscous\nBlattsalat"
menuViewMenu22.getTxtViewMenuHeading().text = "Essen 22"
menuViewMenu22.getTxtViewMenu().text = "Riesen Currywurst\nCurryketchup\nGitterkartoffeln\nBlattsalat"
linLayoutMensaFragment.addView(cardViewMensaDay)
}
cardViewMensaDay2.getLinLayoutMensaDay().addView(menuViewMenu12)
cardViewMensaDay2.getLinLayoutMensaDay().addView(menuViewMenu22)
linLayoutMensaFragment.addView(cardViewMensaDay2)
}
}

14
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/DataTypes.kt

@ -0,0 +1,14 @@
package org.mosad.seil0.projectlaogai.hsoparser
class DataTypes {
init {
// do something
}
}
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)

82
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/MensaParser.kt

@ -0,0 +1,82 @@
/**
* ProjectLaogai
*
* Copyright 2018 <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.seil0.projectlaogai.hsoparser
import org.jsoup.Jsoup
import java.util.*
class MensaParser {
private var mealList = ArrayList<Meal>()
init {
// do something
}
/**
* returns the mensa menu for the whole week
*/
fun getMensaMenu(): ArrayList<Meal> {
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
}
}
}
return mealList
}
/**
* return the mensa menu of a given day (Mon - Sat)
*/
fun getMensaMenuDay(mealList: ArrayList<Meal>, day: Int): ArrayList<Meal> {
var dayMenus = ArrayList<Meal>()
var strDay: String = when(day) {
Calendar.MONDAY -> "Mon"
Calendar.TUESDAY -> "Die"
Calendar.WEDNESDAY -> "Mit"
Calendar.THURSDAY -> "Don"
Calendar.FRIDAY -> "Fre"
Calendar.SATURDAY -> "Sam"
else -> "TODAY" //TODO
}
for (meal in mealList) {
if (meal.day.contains(strDay)) {
println(meal.day)
dayMenus.add(meal)
}
}
return dayMenus
}
}

101
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/TimeTableParser.kt

@ -0,0 +1,101 @@
/**
* ProjectLaogai
*
* Copyright 2018 <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.seil0.projectlaogai.hsoparser
import org.jsoup.Jsoup
class TimeTableParser {
val days = arrayOf("Monday", "Tuesday" ,"Wednesday", "Thursday", "Friday", "Saturday")
var courseTTLinkList = ArrayList<CourseTTLink>()
var timeTableWeek = arrayOf<Array<Lesson>>()
init {
// create the timetable array
for (i in 0..5) {
var timeTableDay = arrayOf<Lesson>()
for (j in 0..6) {
timeTableDay += Lesson("", "","","")
}
timeTableWeek += timeTableDay
}
}
/**
* 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) {
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()
}
}

2
app/src/main/java/ord/mosad/seil0/projectlaogai/uicomponents/LessonCardView.kt → app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/LessonCardView.kt

@ -20,7 +20,7 @@
*
*/
package ord.mosad.seil0.projectlaogai.uicomponents
package org.mosad.seil0.projectlaogai.uicomponents
import android.content.Context
import android.graphics.Color

54
app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/MensaDayCardView.kt

@ -0,0 +1,54 @@
/**
* ProjectLaogai
*
* Copyright 2018 <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.seil0.projectlaogai.uicomponents
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import android.widget.TextView
import org.mosad.seil0.projectlaogai.R
class MensaDayCardView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : androidx.cardview.widget.CardView(context, attrs){
private var linLayoutMensaDay: LinearLayout
private var txtViewDayHeading: TextView
init {
inflate(context, R.layout.mensaday_cardview,this)
linLayoutMensaDay = findViewById(R.id.linLayout_MensaDay)
txtViewDayHeading = findViewById(R.id.txtView_DayHeading)
// workaround to prevent a white border
//this.setBackgroundColor(Color.TRANSPARENT)
}
fun getLinLayoutMensaDay(): LinearLayout {
return linLayoutMensaDay
}
fun setDayHeading(heading: String) {
txtViewDayHeading.text = heading
}
}

6
app/src/main/java/ord/mosad/seil0/projectlaogai/uicomponents/MenuCardView.kt → app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/MenuCardView.kt

@ -1,4 +1,4 @@
package ord.mosad.seil0.projectlaogai.uicomponents
package org.mosad.seil0.projectlaogai.uicomponents
import android.content.Context
import android.graphics.Color
@ -25,6 +25,10 @@ class MenuCardView @JvmOverloads constructor(context: Context, attrs: AttributeS
return txtViewMenuHeading
}
fun setMenuHeading(heading: String) {
txtViewMenuHeading.text = heading
}
fun getTxtViewMenu(): TextView {
return txtViewMenu
}

2
app/src/main/res/layout/menu_cardview.xml

@ -5,7 +5,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"

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