7 Commits

Author SHA1 Message Date
6f4cb9fd66 Small improvements
- Improve formatting
- Fix some typos
- Mini code improvements
2021-10-13 22:21:43 +02:00
1f1374f112 Make the update scheduling more readable (hopefully) 2021-10-13 22:21:43 +02:00
b783fb6c4f StatusController: use properties instead of getters 2021-10-13 22:21:42 +02:00
65dc8ea5d2 Also set JVM target to 11 for Java 2021-10-13 22:21:42 +02:00
97efbd0877 Dependency updates
This also replaces JCenter with Maven Central since JCenter is now read-only
2021-10-13 22:21:42 +02:00
c62f576ace Timetable fixes
- Only one request is made to get the timetable HTML document for parsing the timetable and the weekNumberYear
- On timeouts or other errors, the cached data won't be overwritten with emptiness anymore
- The scheduled updates will now also update the weekNumberYear
2021-10-13 22:21:42 +02:00
8ac78d29b1 Update Kotlin to 1.5.31 2021-10-13 22:21:41 +02:00
7 changed files with 41 additions and 50 deletions

View File

@ -11,11 +11,12 @@ pipeline:
event:
- tag
docker:
image: techknowlogick/drone-docker
privileged: true
image: plugins/docker
repo: mosadxyz/tcor
secrets: [docker_username, docker_password]
tags: latest
secrets:
- username: docker_username
password: docker_password
when:
event:
- tag

View File

@ -6,7 +6,7 @@ plugins {
}
group 'org.mosad'
version '1.3.0'
version '1.2.7'
repositories {
mavenCentral()

View File

@ -48,8 +48,8 @@ class APIController {
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
companion object {
const val apiVersion = "1.3.0"
const val softwareVersion = "1.3.0"
const val apiVersion = "1.2.0"
const val softwareVersion = "1.2.8"
val startTime = System.currentTimeMillis() / 1000
}

View File

@ -46,9 +46,6 @@ data class MensaMeta(val updateTime: Long, val mensaName: String)
data class MensaMenu(val meta: MensaMeta, val currentWeek: MensaWeek, val nextWeek: MensaWeek)
// data classes for the timetable part
data class CalendarWeek(val week: Int, val year: Int)
data class Lesson(
val lessonID: String,
val lessonSubject: String,
@ -61,7 +58,7 @@ 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 = 0, val courseName: String = "", val weekIndex: Int = 0, var weekNumberYear: Int = 0, val year: Int = 0, val link: String = "")
data class TimetableCourseMeta(var updateTime: Long = 0, val courseName: String = "", val weekIndex: Int = 0, var weekNumberYear: Int = 0, val link: String = "")
data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeek = TimetableWeek())

View File

@ -33,9 +33,9 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.*
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet
import kotlin.concurrent.scheduleAtFixedRate
import kotlin.time.Duration
@ -53,7 +53,7 @@ class CacheController {
var courseList = CoursesList(CoursesMeta(), sortedMapOf())
var mensaMenu = MensaMenu(MensaMeta(0, ""), MensaWeek(), MensaWeek())
var timetableList = ConcurrentHashMap<String, TimetableCourseWeek>() // this list contains all timetables
var timetableList = HashMap<String, TimetableCourseWeek>() // this list contains all timetables
/**
* get a timetable, since they may not be cached, we need to make sure it's cached, otherwise download
@ -68,7 +68,6 @@ class CacheController {
val currentTime = System.currentTimeMillis() / 1000
val timetableLink = "https://mosad.xyz"
val weekNumberYear = 0
val year = 0
val instr = CacheController::class.java.getResourceAsStream("/html/Timetable_normal-week.html")
val timetableParser =
@ -81,7 +80,6 @@ class CacheController {
courseName,
weekIndex,
weekNumberYear,
year,
timetableLink
), timetableTest ?: TimetableWeek()
)
@ -97,7 +95,7 @@ class CacheController {
val currentTime = System.currentTimeMillis() / 1000
val timetableParser = TimetableParser(timetableLink)
val calendarWeek = timetableParser.parseCalendarWeek()
val weekNumberYear = timetableParser.parseWeekNumberYear()
val timetable = timetableParser.parseTimeTable()
TimetableCourseWeek(
@ -105,11 +103,10 @@ class CacheController {
currentTime,
courseName,
weekIndex,
calendarWeek?.week ?: 0,
calendarWeek?.year ?: 0,
weekNumberYear ?: 0,
timetableLink
), timetable ?: TimetableWeek()
).also { if (timetable != null) timetableList[key] = it }
).also { timetableList[key] = it }
}
}
@ -201,7 +198,7 @@ class CacheController {
val timetableParser = TimetableParser(timetableCourse.value.meta.link)
timetableCourse.value.timetable = timetableParser.parseTimeTable() ?: return@execute
timetableCourse.value.meta.weekNumberYear =
timetableParser.parseCalendarWeek()?.week ?: return@execute
timetableParser.parseWeekNumberYear() ?: return@execute
timetableCourse.value.meta.updateTime = System.currentTimeMillis() / 1000
saveTimetableToCache(timetableCourse.value) // save the updated timetable to the cache directory

View File

@ -22,11 +22,8 @@
package org.mosad.thecitadelofricks.hsoparser
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.CalendarWeek
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
import org.slf4j.LoggerFactory
@ -39,22 +36,16 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
companion object {
val semaphore = Semaphore(3, 0)
}
private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let {
runBlocking {
private val htmlDoc: Document? =
htmlDoc
?: if (timetableURL == null) {
null
} else {
try {
// Only allow sending a limited amount of requests at the same time
semaphore.acquire()
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
} finally {
semaphore.release()
}
}
}
@ -63,9 +54,13 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
* 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 parseTimeTable(): TimetableWeek? = htmlDoc?.let {
fun parseTimeTable(): TimetableWeek? {
if (htmlDoc == null) {
return null
}
val timetableWeek = TimetableWeek()
val rows = it.select("table.timetable").select("tr[scope=\"row\"]")
val rows = htmlDoc.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1
var sRow = -1
@ -132,13 +127,15 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
}
/**
* parse the calendar week and the associated year for the timetable
* parse the week number of the year for the timetable
*/
fun parseCalendarWeek(): CalendarWeek? = htmlDoc?.let {
val dateStr = it.select("h1.timetable-caption").text().substringAfter("- ")
val week = dateStr.substringBefore(".").replace(" ", "").toInt()
val year = dateStr.substringAfter("Woche ").replace(" ", "").toInt()
CalendarWeek(week, year)
fun parseWeekNumberYear(): Int? {
if (htmlDoc == null) {
return null
}
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
}
@Suppress("unused")

View File

@ -25,7 +25,6 @@ package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mosad.thecitadelofricks.CalendarWeek
import java.io.File
class TimetableParserTest {
@ -51,11 +50,11 @@ class TimetableParserTest {
}
@Test
fun parseCalendarWeek() {
fun parseWeekNumberYear() {
val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile, "UTF-8", "https://www.hs-offenburg.de/")
val actualCalendarWeek = TimetableParser(htmlDoc = htmlDoc).parseCalendarWeek()
val actualWeekNumberYear = TimetableParser(htmlDoc = htmlDoc).parseWeekNumberYear()
Assertions.assertEquals(CalendarWeek(42, 2019), actualCalendarWeek)
Assertions.assertEquals(42, actualWeekNumberYear)
}
}