parse the week number of the year too

This commit is contained in:
Jannik 2019-09-06 23:10:35 +02:00
parent ea6be1db33
commit 5e220225f6
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
8 changed files with 1824 additions and 16 deletions

View File

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

View File

@ -44,8 +44,8 @@ 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 cache = CacheController()
private val apiVersion = "1.1.1" private val apiVersion = "1.1.2"
private val softwareVersion = "1.1.3" private val softwareVersion = "1.1.4"
private val startTime = System.currentTimeMillis() / 1000 private val startTime = System.currentTimeMillis() / 1000
private var requestCount = 0 private var requestCount = 0

View File

@ -58,29 +58,31 @@ class CacheController {
scheduledUpdates() scheduledUpdates()
} }
fun getTimetable(courseName: String, week: Int): TimetableCourseWeek = runBlocking { fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
val currentTime = System.currentTimeMillis() / 1000 val currentTime = System.currentTimeMillis() / 1000
var timetable = TimetableWeek() var timetable = TimetableWeek()
var weekNumberYear = 0
// check if the timetable already exists and is up to date // 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 // there is no such course yet, create one
null -> { null -> {
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink 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 { val jobTimetable = GlobalScope.async {
timetable = TimetableParser().getTimeTable(timetableMeta.link) timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
} }
jobTimetable.await() jobTimetable.await()
timetableList.add(TimetableCourseWeek(timetableMeta, timetable)) timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
logger.info("added new timetable for $courseName, week $week") 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)
} }
/** /**

View File

@ -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 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) data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: TimetableWeek)

View File

@ -23,6 +23,7 @@
package org.mosad.thecitadelofricks.hsoparser package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek import org.mosad.thecitadelofricks.TimetableWeek
@ -39,9 +40,6 @@ class TimetableParser {
val timetableWeek = TimetableWeek() val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a try catch block to cover timeouts 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\"]") val rows = scheduleHTML.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1 var sDay = -1
var sRow = -1 var sRow = -1
@ -105,11 +103,26 @@ class TimetableParser {
} }
//printTimetableWeek(timetableWeek)
return 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") @Suppress("unused")
/** /**
* print a timetable * print a timetable

View File

@ -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 package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup import org.jsoup.Jsoup

View File

@ -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)
}
}

File diff suppressed because one or more lines are too long