Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
a13203c477
|
|||
2bb6bc6534 | |||
d0d5e4b6d1
|
|||
16cb89ca38
|
|||
b428840df7
|
|||
4ca79e6540
|
|||
9dd875fcb3
|
|||
e2169ba8ab
|
|||
697f5e3167
|
|||
dd064d63af
|
|||
3177be1bf0
|
|||
db57059727
|
|||
132cf2df0e
|
|||
283cc96e48
|
|||
238947a894
|
@ -6,3 +6,4 @@ steps:
|
|||||||
image: gradle:jdk11
|
image: gradle:jdk11
|
||||||
commands:
|
commands:
|
||||||
- gradle test
|
- gradle test
|
||||||
|
|
||||||
|
6
.gitignore
vendored
6
.gitignore
vendored
@ -35,6 +35,7 @@ captures/
|
|||||||
|
|
||||||
# Intellij
|
# Intellij
|
||||||
*.iml
|
*.iml
|
||||||
|
.idea/
|
||||||
.idea/workspace.xml
|
.idea/workspace.xml
|
||||||
.idea/tasks.xml
|
.idea/tasks.xml
|
||||||
.idea/gradle.xml
|
.idea/gradle.xml
|
||||||
@ -55,6 +56,9 @@ freeline.py
|
|||||||
freeline/
|
freeline/
|
||||||
freeline_project_description.json
|
freeline_project_description.json
|
||||||
|
|
||||||
|
# KDE
|
||||||
.directory
|
.directory
|
||||||
.idea/
|
|
||||||
|
# tcor
|
||||||
|
tcor/
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
[](https://drone.mosad.xyz/Seil0/TheCitadelofRicks)
|
[](https://drone.mosad.xyz/Seil0/TheCitadelofRicks)
|
||||||
|
[](https://git.mosad.xyz/Seil0/TheCitadelofRicks/releases)
|
||||||
[](https://www.gnu.org/licenses/gpl-3.0)
|
[](https://www.gnu.org/licenses/gpl-3.0)
|
||||||
# TheCitadelofRicks
|
# TheCitadelofRicks
|
||||||
|
|
||||||
|
17
build.gradle
17
build.gradle
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.3.50'
|
ext.kotlin_version = '1.3.61'
|
||||||
ext.spring_boot_version = '2.1.9.RELEASE'
|
ext.spring_boot_version = '2.1.11.RELEASE'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
@ -33,19 +33,20 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
|
||||||
implementation 'org.jsoup:jsoup:1.12.1'
|
implementation "org.jsoup:jsoup:1.12.1"
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation "org.springframework.boot:spring-boot-starter-web"
|
||||||
|
implementation "com.google.code.gson:gson:2.8.6"
|
||||||
|
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter:5.5.1")
|
testImplementation("org.junit.jupiter:junit-jupiter:5.5.1")
|
||||||
}
|
}
|
||||||
|
|
||||||
compileKotlin {
|
compileKotlin {
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
kotlinOptions.jvmTarget = "11"
|
||||||
}
|
}
|
||||||
compileTestKotlin {
|
compileTestKotlin {
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
kotlinOptions.jvmTarget = "11"
|
||||||
}
|
}
|
||||||
|
|
||||||
group 'org.mosad'
|
group 'org.mosad'
|
||||||
version '1.1.4'
|
version '1.2.1'
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -22,35 +22,42 @@
|
|||||||
|
|
||||||
package org.mosad.thecitadelofricks
|
package org.mosad.thecitadelofricks
|
||||||
|
|
||||||
import org.mosad.thecitadelofricks.CacheController.Companion.courseList
|
import org.mosad.thecitadelofricks.controller.CacheController
|
||||||
import org.mosad.thecitadelofricks.CacheController.Companion.mensaMenu
|
import org.mosad.thecitadelofricks.controller.CacheController.Companion.courseList
|
||||||
import org.mosad.thecitadelofricks.CacheController.Companion.timetableList
|
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLesson
|
||||||
|
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLessonSubjectList
|
||||||
|
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getTimetable
|
||||||
|
import org.mosad.thecitadelofricks.controller.CacheController.Companion.mensaMenu
|
||||||
|
import org.mosad.thecitadelofricks.controller.StartupController
|
||||||
|
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getStatus
|
||||||
|
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateMensaMenuRequests
|
||||||
|
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateTimetableRequests
|
||||||
|
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateTotalRequests
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
import org.springframework.web.bind.annotation.RequestParam
|
import org.springframework.web.bind.annotation.RequestParam
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import java.net.HttpURLConnection
|
|
||||||
import java.net.URL
|
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.collections.HashSet
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
class APIController {
|
class APIController {
|
||||||
|
|
||||||
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
||||||
private val cache = CacheController()
|
|
||||||
|
|
||||||
private val apiVersion = "1.1.3"
|
companion object {
|
||||||
private val softwareVersion = "1.1.5"
|
const val apiVersion = "1.1.4"
|
||||||
private val startTime = System.currentTimeMillis() / 1000
|
const val softwareVersion = "1.2.1"
|
||||||
|
val startTime = System.currentTimeMillis() / 1000
|
||||||
|
}
|
||||||
|
|
||||||
private var totalRequests = 0
|
init {
|
||||||
private var mensaMenuRequests = 0
|
StartupController()
|
||||||
private var timetableRequests = ArrayList<TimetableCounter>()
|
CacheController()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO remove this with API version 2.0.0
|
||||||
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
|
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
|
||||||
@RequestMapping("/courses")
|
@RequestMapping("/courses")
|
||||||
fun courses(): CourseList {
|
fun courses(): CourseList {
|
||||||
@ -60,15 +67,15 @@ class APIController {
|
|||||||
@RequestMapping("/courseList")
|
@RequestMapping("/courseList")
|
||||||
fun courseList(): CourseList {
|
fun courseList(): CourseList {
|
||||||
logger.info("courseList request at ${LocalDateTime.now()}!")
|
logger.info("courseList request at ${LocalDateTime.now()}!")
|
||||||
totalRequests++
|
updateTotalRequests()
|
||||||
return courseList
|
return courseList
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/mensamenu")
|
@RequestMapping("/mensamenu")
|
||||||
fun mensamenu(): MensaMenu {
|
fun mensamenu(): MensaMenu {
|
||||||
logger.info("mensamenu request at ${LocalDateTime.now()}!")
|
logger.info("mensamenu request at ${LocalDateTime.now()}!")
|
||||||
mensaMenuRequests++
|
updateTotalRequests()
|
||||||
totalRequests++
|
updateMensaMenuRequests()
|
||||||
return mensaMenu
|
return mensaMenu
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,9 +85,9 @@ class APIController {
|
|||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): TimetableCourseWeek {
|
): TimetableCourseWeek {
|
||||||
logger.info("timetable request at ${LocalDateTime.now()}!")
|
logger.info("timetable request at ${LocalDateTime.now()}!")
|
||||||
|
updateTotalRequests()
|
||||||
updateTimetableRequests(courseName)
|
updateTimetableRequests(courseName)
|
||||||
totalRequests++
|
return getTimetable(courseName, week)
|
||||||
return cache.getTimetable(courseName, week)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/lessonSubjectList")
|
@RequestMapping("/lessonSubjectList")
|
||||||
@ -89,7 +96,7 @@ class APIController {
|
|||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): HashSet<String> {
|
): HashSet<String> {
|
||||||
logger.info("lessonSubjectList request at ${LocalDateTime.now()}!")
|
logger.info("lessonSubjectList request at ${LocalDateTime.now()}!")
|
||||||
totalRequests++
|
updateTotalRequests()
|
||||||
return getLessonSubjectList(courseName, week)
|
return getLessonSubjectList(courseName, week)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,112 +107,20 @@ class APIController {
|
|||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): ArrayList<Lesson> {
|
): ArrayList<Lesson> {
|
||||||
logger.info("lesson request at ${LocalDateTime.now()}!")
|
logger.info("lesson request at ${LocalDateTime.now()}!")
|
||||||
totalRequests++
|
updateTotalRequests()
|
||||||
return getLesson(courseName, lessonSubject, week)
|
return getLesson(courseName, lessonSubject, week)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/status")
|
@RequestMapping("/status")
|
||||||
fun status(): Status {
|
fun status(): Status {
|
||||||
|
logger.info("status request at ${LocalDateTime.now()}!")
|
||||||
return getStatus()
|
return getStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
// non direct api functions
|
@RequestMapping("/health")
|
||||||
|
fun health(): Int {
|
||||||
/**
|
logger.info("health request at ${LocalDateTime.now()}!")
|
||||||
* get every explicit lesson in a week
|
return 200
|
||||||
* @param courseName the name of the course to be requested
|
|
||||||
* @param weekIndex request week number (current week = 0)
|
|
||||||
* @return a HashSet of explicit lessons for one week
|
|
||||||
*/
|
|
||||||
private fun getLessonSubjectList(courseName: String, weekIndex: Int): HashSet<String> {
|
|
||||||
val lessonSubjectList = ArrayList<String>()
|
|
||||||
|
|
||||||
// get every lesson subject for the given week
|
|
||||||
val flatMap = cache.getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
|
||||||
flatMap.forEach {
|
|
||||||
it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) }
|
|
||||||
}
|
|
||||||
|
|
||||||
return HashSet(lessonSubjectList)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get every explicit lesson in a week
|
|
||||||
* @param courseName the name of the course to be requested
|
|
||||||
* @param lessonSubject the lesson subject to be requested
|
|
||||||
* @param weekIndex request week number (current week = 0)
|
|
||||||
* @return a ArrayList of every lesson with lessonSubject for one week
|
|
||||||
*/
|
|
||||||
private fun getLesson(courseName: String, lessonSubject: String, weekIndex: Int): ArrayList<Lesson> {
|
|
||||||
val lessonList = ArrayList<Lesson>()
|
|
||||||
|
|
||||||
// get all lessons from the weeks timetable
|
|
||||||
val flatMap = cache.getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
|
||||||
flatMap.forEach {
|
|
||||||
it.forEach { lesson ->
|
|
||||||
if(lesson.lessonSubject.contains(lessonSubject)) {
|
|
||||||
lessonList.add(lesson)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> println("${x.lessonSubject}, ${x.lessonTeacher}") }
|
|
||||||
}
|
|
||||||
|
|
||||||
return lessonList
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* if a timetable is requested update the request counter
|
|
||||||
*/
|
|
||||||
private fun updateTimetableRequests(courseName: String) {
|
|
||||||
timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
|
|
||||||
it.requests++
|
|
||||||
}, {
|
|
||||||
timetableRequests.add(TimetableCounter(courseName, 1))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getStatus(): Status {
|
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
|
||||||
val minutes = (currentTime - startTime) % 3600 / 60
|
|
||||||
val hours = (currentTime - startTime) % 86400 / 3600
|
|
||||||
val days = (currentTime - startTime) / 86400
|
|
||||||
|
|
||||||
var hsoCode = 999
|
|
||||||
var swfrCode = 999
|
|
||||||
logger.info("status request at ${LocalDateTime.now()}!")
|
|
||||||
|
|
||||||
try {
|
|
||||||
val hsoURL = URL("https://www.hs-offenburg.de/")
|
|
||||||
val swfrURL = URL("https://www.swfr.de/")
|
|
||||||
|
|
||||||
var connection = hsoURL.openConnection() as HttpURLConnection
|
|
||||||
connection.requestMethod = "HEAD"
|
|
||||||
|
|
||||||
connection.connectTimeout = 15000
|
|
||||||
hsoCode = connection.responseCode
|
|
||||||
|
|
||||||
connection = swfrURL.openConnection() as HttpURLConnection
|
|
||||||
connection.connectTimeout = 15000
|
|
||||||
swfrCode = connection.responseCode
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("Error while fetching url response codes!", e)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Status(
|
|
||||||
LocalDateTime.now(),
|
|
||||||
"$days days, $hours:$minutes",
|
|
||||||
apiVersion,
|
|
||||||
softwareVersion,
|
|
||||||
totalRequests,
|
|
||||||
mensaMenuRequests,
|
|
||||||
timetableRequests,
|
|
||||||
timetableList.size,
|
|
||||||
Date(courseList.meta.updateTime * 1000),
|
|
||||||
Date(mensaMenu.meta.updateTime * 1000),
|
|
||||||
hsoCode,
|
|
||||||
swfrCode
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -1,190 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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
|
|
||||||
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
|
|
||||||
import org.mosad.thecitadelofricks.hsoparser.MensaParser
|
|
||||||
import org.mosad.thecitadelofricks.hsoparser.TimetableParser
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import java.util.*
|
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.concurrent.scheduleAtFixedRate
|
|
||||||
|
|
||||||
class CacheController {
|
|
||||||
|
|
||||||
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
|
|
||||||
|
|
||||||
// hso parser links (hardcoded)
|
|
||||||
private val courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
|
|
||||||
private val mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
|
|
||||||
private val mensaName = "Offenburg"
|
|
||||||
|
|
||||||
// cache objects
|
|
||||||
companion object{
|
|
||||||
|
|
||||||
lateinit var courseList: CourseList
|
|
||||||
lateinit var mensaMenu: MensaMenu
|
|
||||||
|
|
||||||
var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
|
||||||
initUpdates()
|
|
||||||
scheduledUpdates()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get a timetable, since they may not cached, we need to make sure it's cached, otherwise download
|
|
||||||
* @param courseName the name of the course to be requested
|
|
||||||
* @param weekIndex request week number (current week = 0)
|
|
||||||
* @return the timetable of a course (courseName)
|
|
||||||
*/
|
|
||||||
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
|
||||||
var timetable = TimetableWeek()
|
|
||||||
var weekNumberYear = 0
|
|
||||||
|
|
||||||
// check if the timetable already exists and is up to date
|
|
||||||
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
|
|
||||||
// there is no such course yet, create one
|
|
||||||
null -> {
|
|
||||||
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
|
|
||||||
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
|
|
||||||
|
|
||||||
val jobTimetable = GlobalScope.async {
|
|
||||||
timetable = TimetableParser().getTimeTable(timetableLink)
|
|
||||||
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
|
|
||||||
}
|
|
||||||
|
|
||||||
jobTimetable.await()
|
|
||||||
|
|
||||||
timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
|
|
||||||
logger.info("added new timetable for $courseName, week $weekIndex")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* this function updates the courseList
|
|
||||||
* during the update process the old data will be returned for a API request
|
|
||||||
*/
|
|
||||||
private fun asyncUpdateCourseList() = GlobalScope.launch {
|
|
||||||
CourseListParser().getCourseLinks(courseListURL)?.let {
|
|
||||||
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, it.size), it)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* this function updates the mensa menu list
|
|
||||||
* during the update process the old data will be returned for a API request
|
|
||||||
*/
|
|
||||||
private fun asyncUpdateMensa() = GlobalScope.launch {
|
|
||||||
val mensaCurrentWeek = MensaParser().getMensaMenu(mensaMenuURL)
|
|
||||||
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaMenuURL))
|
|
||||||
|
|
||||||
// only update if we get valid data
|
|
||||||
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
|
||||||
mensaMenu = MensaMenu(MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* this function updates all existing timetables
|
|
||||||
* during the update process the old data will be returned for a API request
|
|
||||||
*/
|
|
||||||
private fun asyncUpdateTimetables() = GlobalScope.launch {
|
|
||||||
timetableList.forEach { timetableCourse ->
|
|
||||||
val updateURL = timetableCourse.meta.link
|
|
||||||
timetableCourse.timetable = TimetableParser().getTimeTable(updateURL)
|
|
||||||
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
|
||||||
}
|
|
||||||
logger.info("updated ${timetableList.size} timetables successful!")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* before the APIController is up, get the data fist
|
|
||||||
* runBlocking: otherwise the api would return no data to requests for a few seconds after startup
|
|
||||||
*/
|
|
||||||
private fun initUpdates() = runBlocking {
|
|
||||||
// get all courses on startup
|
|
||||||
val jobCourseUpdate = GlobalScope.async {
|
|
||||||
CourseListParser().getCourseLinks(courseListURL)?.let {
|
|
||||||
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, it.size), it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the current and next weeks mensa menus
|
|
||||||
val jobMensa = GlobalScope.async{
|
|
||||||
val mensaCurrentWeek = MensaParser().getMensaMenu(mensaMenuURL)
|
|
||||||
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaMenuURL))
|
|
||||||
|
|
||||||
// only update if we get valid data
|
|
||||||
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
|
||||||
mensaMenu = MensaMenu(MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jobCourseUpdate.await()
|
|
||||||
jobMensa.await()
|
|
||||||
|
|
||||||
logger.info("init updates successful")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* doesn't account the change between winter and summer time!
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private fun scheduledUpdates() {
|
|
||||||
val currentTime = System.currentTimeMillis()
|
|
||||||
val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
|
|
||||||
val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
|
|
||||||
val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000
|
|
||||||
|
|
||||||
// update courseList every 24 hours (time in ms)
|
|
||||||
Timer().scheduleAtFixedRate(initDelay24h, 86400000) {
|
|
||||||
asyncUpdateCourseList()
|
|
||||||
}
|
|
||||||
|
|
||||||
// update all already existing timetables every 3 hours (time in ms)
|
|
||||||
Timer().scheduleAtFixedRate(initDelay3h, 10800000) {
|
|
||||||
asyncUpdateTimetables()
|
|
||||||
}
|
|
||||||
|
|
||||||
// update courses every hour (time in ms)
|
|
||||||
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
|
||||||
asyncUpdateMensa()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -56,9 +56,9 @@ data class TimetableDay(val timeslots: Array<ArrayList<Lesson>> = Array(6) { Arr
|
|||||||
|
|
||||||
data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
|
data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
|
||||||
|
|
||||||
data class TimetableCourseMeta(var updateTime: Long, val courseName: String, val weekIndex: Int, val weekNumberYear: Int, val link: String)
|
data class TimetableCourseMeta(var updateTime: Long = 0, val courseName: String = "", val weekIndex: Int = 0, val weekNumberYear: Int = 0, val link: String = "")
|
||||||
|
|
||||||
data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: TimetableWeek)
|
data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeek = TimetableWeek())
|
||||||
|
|
||||||
|
|
||||||
// data classes for the status part
|
// data classes for the status part
|
||||||
|
@ -0,0 +1,278 @@
|
|||||||
|
/**
|
||||||
|
* TheCitadelofRicks
|
||||||
|
*
|
||||||
|
* Copyright 2019-2020 <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.controller
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.mosad.thecitadelofricks.*
|
||||||
|
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
|
||||||
|
import org.mosad.thecitadelofricks.hsoparser.MensaParser
|
||||||
|
import org.mosad.thecitadelofricks.hsoparser.TimetableParser
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.io.BufferedWriter
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileWriter
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import kotlin.collections.ArrayList
|
||||||
|
import kotlin.collections.HashSet
|
||||||
|
import kotlin.concurrent.scheduleAtFixedRate
|
||||||
|
|
||||||
|
class CacheController {
|
||||||
|
|
||||||
|
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
|
||||||
|
|
||||||
|
init {
|
||||||
|
initUpdates()
|
||||||
|
scheduledUpdates()
|
||||||
|
}
|
||||||
|
|
||||||
|
// cache objects
|
||||||
|
companion object{
|
||||||
|
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
|
||||||
|
|
||||||
|
lateinit var courseList: CourseList
|
||||||
|
lateinit var mensaMenu: MensaMenu
|
||||||
|
var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get a timetable, since they may not cached, we need to make sure it's cached, otherwise download
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return timetable of the course (Type: [TimetableCourseWeek])
|
||||||
|
*/
|
||||||
|
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
||||||
|
val currentTime = System.currentTimeMillis() / 1000
|
||||||
|
var timetable = TimetableWeek()
|
||||||
|
var weekNumberYear = 0
|
||||||
|
|
||||||
|
// check if the timetable already exists and is up to date
|
||||||
|
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
|
||||||
|
// there is no such course yet, create one
|
||||||
|
null -> {
|
||||||
|
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
|
||||||
|
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
|
||||||
|
|
||||||
|
val jobTimetable = GlobalScope.async {
|
||||||
|
timetable = TimetableParser().getTimeTable(timetableLink)
|
||||||
|
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
|
||||||
|
}
|
||||||
|
|
||||||
|
jobTimetable.await()
|
||||||
|
|
||||||
|
timetableList.add(
|
||||||
|
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink),
|
||||||
|
timetable
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info("added new timetable for $courseName, week $weekIndex")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get every explicit lesson in a week
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return a HashSet of explicit lessons for one week
|
||||||
|
*/
|
||||||
|
fun getLessonSubjectList(courseName: String, weekIndex: Int): HashSet<String> = runBlocking {
|
||||||
|
val lessonSubjectList = ArrayList<String>()
|
||||||
|
|
||||||
|
// get every lesson subject for the given week
|
||||||
|
val flatMap = getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||||
|
flatMap.forEach {
|
||||||
|
it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return@runBlocking HashSet(lessonSubjectList)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get every lesson of a subject in a week
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param lessonSubject the lesson subject to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return a ArrayList<[Lesson]> of every lesson with lessonSubject for one week
|
||||||
|
*/
|
||||||
|
fun getLesson(courseName: String, lessonSubject: String, weekIndex: Int): ArrayList<Lesson> {
|
||||||
|
val lessonList = ArrayList<Lesson>()
|
||||||
|
|
||||||
|
// get all lessons from the weeks timetable
|
||||||
|
val flatMap = getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||||
|
flatMap.forEach {
|
||||||
|
it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> lessonList.add(x) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return lessonList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this function updates the courseList
|
||||||
|
* during the update process the old data will be returned for a API request
|
||||||
|
*/
|
||||||
|
private fun asyncUpdateCourseList() = GlobalScope.launch {
|
||||||
|
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
|
||||||
|
courseList = CourseList(
|
||||||
|
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this function updates the mensa menu list
|
||||||
|
* during the update process the old data will be returned for a API request
|
||||||
|
*/
|
||||||
|
private fun asyncUpdateMensa() = GlobalScope.launch {
|
||||||
|
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.mensaMenuURL)
|
||||||
|
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.mensaMenuURL))
|
||||||
|
|
||||||
|
// only update if we get valid data
|
||||||
|
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
||||||
|
mensaMenu = MensaMenu(
|
||||||
|
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this function updates all existing timetables
|
||||||
|
* during the update process the old data will be returned for a API request
|
||||||
|
* a FixedThreadPool is used to make parallel requests for faster updates
|
||||||
|
*/
|
||||||
|
private fun asyncUpdateTimetables() = GlobalScope.launch {
|
||||||
|
logger.info("updating ${timetableList.size} timetables ...")
|
||||||
|
|
||||||
|
// create a new ThreadPool with 5 threads
|
||||||
|
val executor = Executors.newFixedThreadPool(5)
|
||||||
|
|
||||||
|
try {
|
||||||
|
timetableList.forEach { timetableCourse ->
|
||||||
|
executor.execute {
|
||||||
|
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
|
||||||
|
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
||||||
|
|
||||||
|
saveTimetableToCache(timetableCourse) // save the updated timetable to the cache directory
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
logger.error("error while updating the timetables", ex)
|
||||||
|
} finally {
|
||||||
|
executor.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* save a timetable to the cache directory
|
||||||
|
* this is only call on async updates, it is NOT call when first getting the timetable
|
||||||
|
* @param timetable a timetable of the type [TimetableCourseWeek]
|
||||||
|
*/
|
||||||
|
private fun saveTimetableToCache(timetable: TimetableCourseWeek) {
|
||||||
|
println(timetable.timetable.toString())
|
||||||
|
|
||||||
|
val file = File(StartupController.dirTcorCache, "timetable-${timetable.meta.courseName}-${timetable.meta.weekIndex}.json")
|
||||||
|
val writer = BufferedWriter(FileWriter(file))
|
||||||
|
writer.write(Gson().toJson(timetable))
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* before the APIController is up, get the data fist
|
||||||
|
* runBlocking: otherwise the api would return no data to requests for a few seconds after startup
|
||||||
|
*/
|
||||||
|
private fun initUpdates() = runBlocking {
|
||||||
|
// get all courses on startup
|
||||||
|
val jobCourseUpdate = GlobalScope.async {
|
||||||
|
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
|
||||||
|
courseList = CourseList(
|
||||||
|
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the current and next weeks mensa menus
|
||||||
|
val jobMensa = GlobalScope.async{
|
||||||
|
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.mensaMenuURL)
|
||||||
|
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.mensaMenuURL))
|
||||||
|
|
||||||
|
// only update if we get valid data
|
||||||
|
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
||||||
|
mensaMenu = MensaMenu(
|
||||||
|
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jobCourseUpdate.await()
|
||||||
|
jobMensa.await()
|
||||||
|
|
||||||
|
logger.info("init updates successful")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* update the CourseList every 24h, the Timetables every 3h and the Mensa Menu every hour
|
||||||
|
* doesn't account the change between winter and summer time!
|
||||||
|
*/
|
||||||
|
private fun scheduledUpdates() {
|
||||||
|
val currentTime = System.currentTimeMillis()
|
||||||
|
val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
|
||||||
|
val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
|
||||||
|
val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000
|
||||||
|
|
||||||
|
// update courseList every 24 hours (time in ms)
|
||||||
|
Timer().scheduleAtFixedRate(initDelay24h, 86400000) {
|
||||||
|
asyncUpdateCourseList()
|
||||||
|
}
|
||||||
|
|
||||||
|
// update all already existing timetables every 3 hours (time in ms)
|
||||||
|
Timer().scheduleAtFixedRate(initDelay3h, 10800000) {
|
||||||
|
asyncUpdateTimetables()
|
||||||
|
}
|
||||||
|
|
||||||
|
// update courses every hour (time in ms)
|
||||||
|
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
||||||
|
asyncUpdateMensa()
|
||||||
|
}
|
||||||
|
|
||||||
|
// post to status.mosad.xyz every hour, if an API key is present
|
||||||
|
if (StartupController.cachetAPIKey != "0") {
|
||||||
|
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
||||||
|
CachetAPIController.postTotalRequests()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* TheCitadelofRicks
|
||||||
|
*
|
||||||
|
* Copyright 2019-2020 <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.controller
|
||||||
|
|
||||||
|
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getTotalRequests
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.io.BufferedReader
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
class CachetAPIController {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private val logger: Logger = LoggerFactory.getLogger(CachetAPIController::class.java)
|
||||||
|
|
||||||
|
private var oldTotalRequests = 0
|
||||||
|
|
||||||
|
fun postTotalRequests() {
|
||||||
|
try {
|
||||||
|
val url = URL("${StartupController.cachetBaseURL}/api/v1/metrics/1/points")
|
||||||
|
val jsonInputString = "{\"value\": ${getTotalRequests() -oldTotalRequests}, \"timestamp\": \"${(System.currentTimeMillis() / 1000)}\"}"
|
||||||
|
oldTotalRequests = getTotalRequests()
|
||||||
|
|
||||||
|
val con = url.openConnection() as HttpURLConnection
|
||||||
|
con.requestMethod = "POST"
|
||||||
|
con.setRequestProperty("Content-Type", "application/json; utf-8")
|
||||||
|
con.setRequestProperty("Accept", "application/json")
|
||||||
|
con.setRequestProperty("X-Cachet-Token", StartupController.cachetAPIKey)
|
||||||
|
con.doOutput = true
|
||||||
|
|
||||||
|
val os = con.outputStream
|
||||||
|
val input = jsonInputString.toByteArray(charset("utf-8"))
|
||||||
|
os.write(input, 0, input.size)
|
||||||
|
|
||||||
|
val br = BufferedReader(InputStreamReader(con.inputStream, "utf-8"))
|
||||||
|
val response = StringBuilder()
|
||||||
|
var responseLine: String?
|
||||||
|
|
||||||
|
while (br.readLine().also { responseLine = it } != null) {
|
||||||
|
response.append(responseLine!!.trim { it <= ' ' })
|
||||||
|
}
|
||||||
|
logger.info(response.toString())
|
||||||
|
|
||||||
|
} catch (e1: IOException) {
|
||||||
|
e1.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
/**
|
||||||
|
* TheCitadelofRicks
|
||||||
|
*
|
||||||
|
* Copyright 2019-2020 <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.controller
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonParser
|
||||||
|
import org.mosad.thecitadelofricks.TimetableCourseWeek
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.io.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class StartupController {
|
||||||
|
|
||||||
|
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val userHome: String = System.getProperty("user.dir")
|
||||||
|
val tcorHome = "$userHome/tcor"
|
||||||
|
val dirTcorHome = File(tcorHome)
|
||||||
|
val dirTcorCache = File("$tcorHome/cache")
|
||||||
|
val fileConfig = File("$tcorHome/config.xml")
|
||||||
|
|
||||||
|
var cachetAPIKey = "0"
|
||||||
|
var cachetBaseURL = "https://status.mosad.xyz"
|
||||||
|
var courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
|
||||||
|
var mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
|
||||||
|
var mensaName = "Offenburg"
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
// if the tcor directory doesn't exist, create it
|
||||||
|
if (!dirTcorHome.exists()) {
|
||||||
|
dirTcorHome.mkdir()
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the cache directory doesn't exist, create it
|
||||||
|
if (!dirTcorCache.exists()) {
|
||||||
|
dirTcorCache.mkdir()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the config file exist, if so load it
|
||||||
|
if (fileConfig.exists()) {
|
||||||
|
loadConfig()
|
||||||
|
} else {
|
||||||
|
createConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
// read cached timetable files, as they are not initially cached
|
||||||
|
readCachedTimetables()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load the config stored in the config.xml file
|
||||||
|
*/
|
||||||
|
private fun loadConfig() = try {
|
||||||
|
val properties = Properties()
|
||||||
|
properties.loadFromXML(FileInputStream(fileConfig))
|
||||||
|
|
||||||
|
cachetAPIKey = try {
|
||||||
|
properties.getProperty("cachetAPIKey")
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
"0"
|
||||||
|
}
|
||||||
|
|
||||||
|
cachetBaseURL = try {
|
||||||
|
properties.getProperty("cachetBaseURL")
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
"https://status.mosad.xyz"
|
||||||
|
}
|
||||||
|
|
||||||
|
mensaMenuURL = try {
|
||||||
|
properties.getProperty("mensaMenuURL")
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
"https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
|
||||||
|
}
|
||||||
|
|
||||||
|
mensaName = try {
|
||||||
|
properties.getProperty("mensaName")
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
"Offenburg"
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
logger.error("error while loading config", ex)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create an initial config file
|
||||||
|
*/
|
||||||
|
private fun createConfig() = try {
|
||||||
|
val properties = Properties()
|
||||||
|
|
||||||
|
properties.setProperty("cachetAPIKey", "0")
|
||||||
|
properties.setProperty("cachetBaseURL", "https://status.mosad.xyz")
|
||||||
|
properties.setProperty("mensaMenuURL", "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/")
|
||||||
|
properties.setProperty("mensaName", "Offenburg")
|
||||||
|
|
||||||
|
val outputStream = FileOutputStream(fileConfig)
|
||||||
|
properties.storeToXML(outputStream, "tcor configuration")
|
||||||
|
outputStream.close()
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
logger.error("error while creating config", ex)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* read all previously cached timetables
|
||||||
|
*/
|
||||||
|
private fun readCachedTimetables() {
|
||||||
|
dirTcorCache.walkTopDown().forEach {
|
||||||
|
if (it.isFile && it.name.endsWith(".json")) {
|
||||||
|
try {
|
||||||
|
val fileReader = FileReader(it)
|
||||||
|
val bufferedReader = BufferedReader(fileReader)
|
||||||
|
val timetableObject = JsonParser.parseString(bufferedReader.readLine()).asJsonObject
|
||||||
|
|
||||||
|
CacheController.timetableList.add(Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass))
|
||||||
|
bufferedReader.close()
|
||||||
|
fileReader.close()
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
logger.error("error while reading cache", ex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
/**
|
||||||
|
* TheCitadelofRicks
|
||||||
|
*
|
||||||
|
* Copyright 2019-2020 <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.controller
|
||||||
|
|
||||||
|
import org.mosad.thecitadelofricks.APIController.Companion.apiVersion
|
||||||
|
import org.mosad.thecitadelofricks.APIController.Companion.softwareVersion
|
||||||
|
import org.mosad.thecitadelofricks.APIController.Companion.startTime
|
||||||
|
import org.mosad.thecitadelofricks.Status
|
||||||
|
import org.mosad.thecitadelofricks.TimetableCounter
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
|
class StatusController {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger: Logger = LoggerFactory.getLogger(StatusController::class.java)
|
||||||
|
|
||||||
|
private var totalRequests = 0
|
||||||
|
private var mensaMenuRequests = 0
|
||||||
|
private var timetableRequests = ArrayList<TimetableCounter>()
|
||||||
|
|
||||||
|
fun updateTotalRequests() {
|
||||||
|
totalRequests++
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateMensaMenuRequests() {
|
||||||
|
mensaMenuRequests++
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if a timetable is requested update the request counter
|
||||||
|
*/
|
||||||
|
fun updateTimetableRequests(courseName: String) {
|
||||||
|
timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
|
||||||
|
it.requests++
|
||||||
|
}, {
|
||||||
|
timetableRequests.add(TimetableCounter(courseName, 1))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTotalRequests(): Int {
|
||||||
|
return totalRequests
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMensaMenuRequests(): Int {
|
||||||
|
return mensaMenuRequests
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTimetableRequests(): ArrayList<TimetableCounter> {
|
||||||
|
return timetableRequests
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getStatus(): Status {
|
||||||
|
val currentTime = System.currentTimeMillis() / 1000
|
||||||
|
val minutes = (currentTime - startTime) % 3600 / 60
|
||||||
|
val hours = (currentTime - startTime) % 86400 / 3600
|
||||||
|
val days = (currentTime - startTime) / 86400
|
||||||
|
|
||||||
|
var hsoCode = 999
|
||||||
|
var swfrCode = 999
|
||||||
|
|
||||||
|
try {
|
||||||
|
val hsoURL = URL("https://www.hs-offenburg.de/")
|
||||||
|
val swfrURL = URL("https://www.swfr.de/")
|
||||||
|
|
||||||
|
var connection = hsoURL.openConnection() as HttpURLConnection
|
||||||
|
connection.requestMethod = "HEAD"
|
||||||
|
|
||||||
|
connection.connectTimeout = 15000
|
||||||
|
hsoCode = connection.responseCode
|
||||||
|
|
||||||
|
connection = swfrURL.openConnection() as HttpURLConnection
|
||||||
|
connection.connectTimeout = 15000
|
||||||
|
swfrCode = connection.responseCode
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.error("Error while fetching url response codes!", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status(
|
||||||
|
LocalDateTime.now(),
|
||||||
|
"$days days, $hours:$minutes",
|
||||||
|
apiVersion,
|
||||||
|
softwareVersion,
|
||||||
|
getTotalRequests(),
|
||||||
|
getMensaMenuRequests(),
|
||||||
|
getTimetableRequests(),
|
||||||
|
CacheController.timetableList.size,
|
||||||
|
Date(CacheController.courseList.meta.updateTime * 1000),
|
||||||
|
Date(CacheController.mensaMenu.meta.updateTime * 1000),
|
||||||
|
hsoCode,
|
||||||
|
swfrCode
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* TheCitadelofRicks
|
* TheCitadelofRicks
|
||||||
*
|
*
|
||||||
* Copyright 2019 <seil0@mosad.xyz>
|
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
Reference in New Issue
Block a user