Browse Source

parse the week number of the year too

pull/18/head
Jannik 3 years ago
parent
commit
5e220225f6
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 2
      build.gradle
  2. 4
      src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt
  3. 16
      src/main/kotlin/org/mosad/thecitadelofricks/CacheController.kt
  4. 2
      src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt
  5. 23
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt
  6. 22
      src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParserTest.kt
  7. 45
      src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt
  8. 1726
      src/test/resources/html/Timetable_normal-week.html

2
build.gradle

@ -44,4 +44,4 @@ compileTestKotlin {
}
group 'org.mosad'
version '1.1.3'
version '1.1.4'

4
src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt

@ -44,8 +44,8 @@ class APIController {
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
private val cache = CacheController()
private val apiVersion = "1.1.1"
private val softwareVersion = "1.1.3"
private val apiVersion = "1.1.2"
private val softwareVersion = "1.1.4"
private val startTime = System.currentTimeMillis() / 1000
private var requestCount = 0

16
src/main/kotlin/org/mosad/thecitadelofricks/CacheController.kt

@ -58,29 +58,31 @@ class CacheController {
scheduledUpdates()
}
fun getTimetable(courseName: String, week: Int): TimetableCourseWeek = runBlocking {
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.week == week }.findAny().orElse(null)) {
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 timetableMeta = TimetableCourseMeta(currentTime, courseName, week, courseLink.replace("week=0","week=$week"))
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
val jobTimetable = GlobalScope.async {
timetable = TimetableParser().getTimeTable(timetableMeta.link)
timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
}
jobTimetable.await()
timetableList.add(TimetableCourseWeek(timetableMeta, timetable))
logger.info("added new timetable for $courseName, week $week")
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.week == week }.findAny().orElse(null)
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
}
/**

2
src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt

@ -56,7 +56,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, val courseName: String, val week: Int, val link: String)
data class TimetableCourseMeta(var updateTime: Long, val courseName: String, val weekIndex: Int, val weekNumberYear: Int, val link: String)
data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: TimetableWeek)

23
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

@ -23,6 +23,7 @@
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
@ -39,9 +40,6 @@ class TimetableParser {
val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a try catch block to cover timeouts
//val week = scheduleHTML.select("h1.timetable-caption").text()
//println("$week successful!\n")
val rows = scheduleHTML.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1
var sRow = -1
@ -105,11 +103,26 @@ class TimetableParser {
}
//printTimetableWeek(timetableWeek)
return timetableWeek
}
/**
* get the week number of the year for the timetable
* @param timetableURL the URL of the timetable you want to get
*/
fun getWeekNumberYear(timetableURL: String): Int {
return try {
parseWeekNumberYear(Jsoup.connect(timetableURL).get())
} catch (ex: Exception) {
0
}
}
fun parseWeekNumberYear(htmlDoc: Document): Int {
return htmlDoc.select("h1.timetable-caption").text().substringAfter("- ")
.substringBefore(".").replace(" ", "").toInt()
}
@Suppress("unused")
/**
* print a timetable

22
src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParserTest.kt

@ -1,3 +1,25 @@
/**
* 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.hsoparser
import org.jsoup.Jsoup

45
src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt

@ -0,0 +1,45 @@
/**
* 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.hsoparser
import org.jsoup.Jsoup
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.io.File
class TimetableParserTest {
@Test
fun parseTimetableNormalWeek() {
// TODO
}
@Test
fun parseWeekNumberYear() {
val htmlFile = File(MensaParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile,"UTF-8", "https://www.hs-offenburg.de/")
val weekNumberYear = TimetableParser().parseWeekNumberYear(htmlDoc)
Assertions.assertEquals(42, weekNumberYear)
}
}

1726
src/test/resources/html/Timetable_normal-week.html

File diff suppressed because one or more lines are too long
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