Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
9dd875fcb3
|
|||
e2169ba8ab
|
|||
697f5e3167
|
|||
dd064d63af
|
|||
3177be1bf0
|
13
build.gradle
13
build.gradle
@ -1,7 +1,7 @@
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.3.50'
|
||||
ext.spring_boot_version = '2.1.9.RELEASE'
|
||||
ext.kotlin_version = '1.3.60'
|
||||
ext.spring_boot_version = '2.1.10.RELEASE'
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
@ -33,9 +33,10 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
|
||||
implementation 'org.jsoup:jsoup:1.12.1'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2"
|
||||
implementation "org.jsoup:jsoup:1.12.1"
|
||||
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")
|
||||
}
|
||||
@ -48,4 +49,4 @@ compileTestKotlin {
|
||||
}
|
||||
|
||||
group 'org.mosad'
|
||||
version '1.1.6'
|
||||
version '1.2.0'
|
||||
|
@ -28,6 +28,7 @@ import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLesso
|
||||
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
|
||||
@ -39,7 +40,6 @@ import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
@RestController
|
||||
class APIController {
|
||||
@ -48,12 +48,13 @@ class APIController {
|
||||
|
||||
companion object {
|
||||
const val apiVersion = "1.1.4"
|
||||
const val softwareVersion = "1.1.6"
|
||||
const val softwareVersion = "1.2.0"
|
||||
val startTime = System.currentTimeMillis() / 1000
|
||||
}
|
||||
|
||||
init {
|
||||
CacheController() // initialize the CacheController
|
||||
StartupController()
|
||||
CacheController()
|
||||
}
|
||||
|
||||
// TODO remove this with API version 1.2.0
|
||||
|
@ -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 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
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
package org.mosad.thecitadelofricks.controller
|
||||
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
@ -32,19 +33,19 @@ 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)
|
||||
|
||||
// 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"
|
||||
|
||||
init {
|
||||
initUpdates()
|
||||
scheduledUpdates()
|
||||
@ -62,7 +63,7 @@ class CacheController {
|
||||
* 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)
|
||||
* @return timetable of the course (Type: [TimetableCourseWeek])
|
||||
*/
|
||||
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
||||
val currentTime = System.currentTimeMillis() / 1000
|
||||
@ -84,17 +85,11 @@ class CacheController {
|
||||
jobTimetable.await()
|
||||
|
||||
timetableList.add(
|
||||
TimetableCourseWeek(
|
||||
TimetableCourseMeta(
|
||||
currentTime,
|
||||
courseName,
|
||||
weekIndex,
|
||||
weekNumberYear,
|
||||
timetableLink
|
||||
),
|
||||
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink),
|
||||
timetable
|
||||
)
|
||||
)
|
||||
|
||||
logger.info("added new timetable for $courseName, week $weekIndex")
|
||||
}
|
||||
}
|
||||
@ -125,7 +120,7 @@ class CacheController {
|
||||
* @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
|
||||
* @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>()
|
||||
@ -133,14 +128,14 @@ class CacheController {
|
||||
// get all lessons from the weeks timetable
|
||||
val flatMap = getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||
flatMap.forEach {
|
||||
// TODO Java 11
|
||||
//it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> println("${x.lessonSubject}, ${x.lessonTeacher}") }
|
||||
|
||||
it.forEach { lesson ->
|
||||
if(lesson.lessonSubject.contains(lessonSubject)) {
|
||||
lessonList.add(lesson)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Java 11
|
||||
//it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> println("${x.lessonSubject}, ${x.lessonTeacher}") }
|
||||
}
|
||||
|
||||
return lessonList
|
||||
@ -152,14 +147,10 @@ class CacheController {
|
||||
* 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
|
||||
)
|
||||
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)}")
|
||||
@ -170,18 +161,14 @@ class CacheController {
|
||||
* 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))
|
||||
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,
|
||||
mensaName
|
||||
), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
mensaMenu = MensaMenu(
|
||||
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
}
|
||||
|
||||
logger.info("updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
|
||||
@ -190,14 +177,43 @@ class CacheController {
|
||||
/**
|
||||
* 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 {
|
||||
timetableList.forEach { timetableCourse ->
|
||||
val updateURL = timetableCourse.meta.link
|
||||
timetableCourse.timetable = TimetableParser().getTimeTable(updateURL)
|
||||
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
||||
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()
|
||||
}
|
||||
logger.info("updated ${timetableList.size} timetables successful!")
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,31 +223,23 @@ class CacheController {
|
||||
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
|
||||
)
|
||||
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(mensaMenuURL)
|
||||
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaMenuURL))
|
||||
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,
|
||||
mensaName
|
||||
), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
mensaMenu = MensaMenu(
|
||||
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,8 +250,8 @@ class CacheController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
@ -266,9 +274,11 @@ class CacheController {
|
||||
asyncUpdateMensa()
|
||||
}
|
||||
|
||||
// post to status.mosad.xyz every hour
|
||||
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
||||
CachetAPIController.postTotalRequests()
|
||||
// post to status.mosad.xyz every hour, if an API key is present
|
||||
if (StartupController.cachetAPIKey != "0") {
|
||||
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
||||
CachetAPIController.postTotalRequests()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,14 +36,12 @@ class CachetAPIController {
|
||||
companion object {
|
||||
|
||||
private val logger: Logger = LoggerFactory.getLogger(CachetAPIController::class.java)
|
||||
private const val baseURL = "https://status.mosad.xyz"
|
||||
private const val apiKey = "" //TODO
|
||||
|
||||
private var oldTotalRequests = 0
|
||||
|
||||
fun postTotalRequests() {
|
||||
try {
|
||||
val url = URL("$baseURL/api/v1/metrics/1/points")
|
||||
val url = URL("${StartupController.cachetBaseURL}/api/v1/metrics/1/points")
|
||||
val jsonInputString = "{\"value\": ${getTotalRequests() -oldTotalRequests}, \"timestamp\": \"${(System.currentTimeMillis() / 1000)}\"}"
|
||||
oldTotalRequests = getTotalRequests()
|
||||
|
||||
@ -51,7 +49,7 @@ class CachetAPIController {
|
||||
con.requestMethod = "POST"
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8")
|
||||
con.setRequestProperty("Accept", "application/json")
|
||||
con.setRequestProperty("X-Cachet-Token", apiKey)
|
||||
con.setRequestProperty("X-Cachet-Token", StartupController.cachetAPIKey)
|
||||
con.doOutput = true
|
||||
|
||||
val os = con.outputStream
|
||||
|
@ -0,0 +1,146 @@
|
||||
/**
|
||||
* 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.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.home")
|
||||
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()
|
||||
}
|
||||
|
||||
// TODO 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -59,7 +59,8 @@ class StatusController {
|
||||
when (timetableRequests.stream().filter { x -> x.courseName == courseName }.findAny().orElse(null)) {
|
||||
null -> timetableRequests.add(TimetableCounter(courseName, 0))
|
||||
}
|
||||
timetableRequests.stream().filter { x ->x.courseName == courseName }.findFirst().ifPresent { x -> x.requests++ }
|
||||
timetableRequests.stream().filter { x -> x.courseName == courseName }.findFirst()
|
||||
.ifPresent { x -> x.requests++ }
|
||||
|
||||
// TODO Java 11
|
||||
// timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
|
||||
|
Reference in New Issue
Block a user