From 5b99fd1d2bfafa65b15912e155a4ed7756c6070d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 16 Aug 2020 17:55:42 +0200 Subject: [PATCH] Add semesters list to student --- .../sdk/scrapper/register/DiaryMapper.kt | 17 +++++++++++ .../register/SendCertificateResponse.kt | 3 -- .../sdk/scrapper/register/Student.kt | 3 +- .../scrapper/repository/RegisterRepository.kt | 13 ++++++--- .../repository/StudentStartRepository.kt | 20 ++----------- .../wulkanowy/sdk/scrapper/login/LoginTest.kt | 15 ++-------- .../repository/RegisterRepositoryTest.kt | 28 +++++++++++++++++++ .../wulkanowy/sdk/mapper/StudentsMapper.kt | 6 ++-- .../io/github/wulkanowy/sdk/pojo/Student.kt | 3 +- 9 files changed, 67 insertions(+), 41 deletions(-) create mode 100644 sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/DiaryMapper.kt diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/DiaryMapper.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/DiaryMapper.kt new file mode 100644 index 00000000..398e2d5b --- /dev/null +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/DiaryMapper.kt @@ -0,0 +1,17 @@ +package io.github.wulkanowy.sdk.scrapper.register + +import io.github.wulkanowy.sdk.scrapper.toLocalDate + +fun Diary.toSemesters() = semesters!!.map { + Semester( + diaryId = diaryId, + diaryName = "$level$symbol", + schoolYear = year, + semesterId = it.id, + semesterNumber = it.number, + start = it.start.toLocalDate(), + end = it.end.toLocalDate(), + classId = it.classId, + unitId = it.unitId + ) +} diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SendCertificateResponse.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SendCertificateResponse.kt index 63a61651..05d4c2da 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SendCertificateResponse.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SendCertificateResponse.kt @@ -5,9 +5,6 @@ import pl.droidsonroids.jspoon.annotation.Selector class SendCertificateResponse { - @Selector(".panel.linkownia.pracownik.klient a[href*=\"uonetplus-opiekun\"]") - var oldStudentSchools: List = emptyList() - @Selector(".panel.linkownia.pracownik.klient a[href*=\"uonetplus-uczen\"]") var studentSchools: List = emptyList() } diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt index d572a6a0..eb93b73e 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt @@ -14,5 +14,6 @@ data class Student( val classId: Int, val baseUrl: String, val loginType: Scrapper.LoginType, - val isParent: Boolean + val isParent: Boolean, + val semesters: List ) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt index 55b26082..ed969c3f 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt @@ -12,6 +12,7 @@ import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.register.Diary import io.github.wulkanowy.sdk.scrapper.register.SendCertificateResponse import io.github.wulkanowy.sdk.scrapper.register.Student +import io.github.wulkanowy.sdk.scrapper.register.toSemesters import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS_CARDS import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS_LIGHT @@ -117,7 +118,11 @@ class RegisterRepository( classId = diary.semesters!![0].classId, baseUrl = url.generate(ServiceManager.UrlGenerator.Site.BASE), loginType = loginType, - isParent = cache?.isParent ?: false + isParent = cache?.isParent == true, + semesters = diaries + .filter { it.studentId == diary.studentId && it.semesters?.getOrNull(0)?.classId == diary.semesters[0].classId } + .map { it.toSemesters() } + .flatten() ) }.ifEmpty { logger.debug("No supported student found in diaries: $diaries") @@ -138,9 +143,9 @@ class RegisterRepository( .data.orEmpty() private fun List.filterDiaries() = this - .filter { diary -> diary.semesters?.isNotEmpty() ?: false } - .sortedByDescending { diary -> diary.level } - .distinctBy { diary -> listOf(diary.studentId, diary.semesters!![0].classId) } + .filter { it.semesters.orEmpty().isNotEmpty() } + .sortedByDescending { it.level } + .distinctBy { listOf(it.studentId, it.semesters!![0].classId) } private fun getExtractedSchoolSymbolFromUrl(snpPageUrl: String): String { val path = URL(snpPageUrl).path.split("/") diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt index e4ae03f6..94cad01c 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt @@ -2,8 +2,8 @@ package io.github.wulkanowy.sdk.scrapper.repository import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors import io.github.wulkanowy.sdk.scrapper.register.Semester +import io.github.wulkanowy.sdk.scrapper.register.toSemesters import io.github.wulkanowy.sdk.scrapper.service.StudentService -import io.github.wulkanowy.sdk.scrapper.toLocalDate import org.slf4j.LoggerFactory class StudentStartRepository( @@ -21,24 +21,10 @@ class StudentStartRepository( val diaries = api.getDiaries().handleErrors().data return diaries.orEmpty() .asSequence() - .filter { it.semesters?.isNotEmpty() ?: false } + .filter { it.semesters.orEmpty().isNotEmpty() } .filter { it.studentId == studentId } .filter { it.semesters!![0].classId == classId } - .map { diary -> - diary.semesters!!.map { - Semester( - diaryId = diary.diaryId, - diaryName = "${diary.level}${diary.symbol}", - schoolYear = diary.year, - semesterId = it.id, - semesterNumber = it.number, - start = it.start.toLocalDate(), - end = it.end.toLocalDate(), - classId = it.classId, - unitId = it.unitId - ) - } - } + .map { it.toSemesters() } .flatten() .sortedByDescending { it.semesterId } .toList() diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/login/LoginTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/login/LoginTest.kt index e39f59d3..d0ccd7ce 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/login/LoginTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/login/LoginTest.kt @@ -49,7 +49,7 @@ class LoginTest : BaseLocalTest() { val res = runBlocking { adfs.login("jan@fakelog.cf", "jan123") } - assertTrue(res.oldStudentSchools.isNotEmpty()) + assertTrue(res.studentSchools.isNotEmpty()) } @Test @@ -60,7 +60,7 @@ class LoginTest : BaseLocalTest() { val res = runBlocking { normal.login("jan@fakelog.cf", "jan123") } - assertTrue(res.oldStudentSchools.isNotEmpty()) + assertTrue(res.studentSchools.isNotEmpty()) } @Test @@ -86,17 +86,6 @@ class LoginTest : BaseLocalTest() { assertEquals("[text=LoginName=jan&Password=jan123]", server.takeRequest().body.toString()) } - @Test - fun normalLogin_beforeNewStudentSite() { - server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) - server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success-old.html").readText())) - server.start(3000) - - val res = runBlocking { normal.login("jan@fakelog.cf", "jan123") } - - assertTrue(res.oldStudentSchools.isNotEmpty()) - } - @Test fun normalLogin_encodingError() { server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet-encoding-error.html").readText())) diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt index fe82afa3..613c4fcb 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt @@ -59,9 +59,37 @@ class RegisterRepositoryTest : BaseLocalTest() { with(students[0]) { assertEquals("012345", schoolSymbol) assertEquals("", schoolShortName) + assertEquals(2, semesters.size) } } + @Test + fun normalLogin_semesters() { + server.enqueue("LoginPage-standard.html", LoginTest::class.java) + server.enqueue("Logowanie-uonet.html", LoginTest::class.java) + server.enqueue("Login-success.html", LoginTest::class.java) + + server.enqueue("LoginPage-standard.html", LoginTest::class.java) + server.enqueue("WitrynaUcznia.html", RegisterTest::class.java) + server.enqueue("UczenCache.json", RegisterTest::class.java) + server.enqueue("UczenDziennik.json", RegisterTest::class.java) + + (0..5).onEach { // 5x symbol + server.enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) + } + server.start(3000) + + val students = runBlocking { getRegisterRepository("Default").getStudents() } + + assertEquals(2, students.size) + with(students[0]) { + assertEquals("012345", schoolSymbol) + assertEquals("", schoolShortName) + assertEquals(6, semesters.size) + } + assertEquals(6, students[1].semesters.size) + } + @Test fun normalLogin_triple() { server.enqueue("LoginPage-standard.html", LoginTest::class.java) diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt index 1652edd4..13790884 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt @@ -23,7 +23,8 @@ fun List.mapStudents(symbol: String) = map { scrapperBaseUrl = "", mobileBaseUrl = it.mobileBaseUrl, privateKey = it.privateKey, - certificateKey = it.certificateKey + certificateKey = it.certificateKey, + semesters = mapSemesters(it.id) ) } @@ -45,6 +46,7 @@ fun List.mapStudents() = map { scrapperBaseUrl = it.baseUrl, mobileBaseUrl = "", certificateKey = "", - privateKey = "" + privateKey = "", + semesters = it.semesters.mapSemesters() ) } diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt index 8f4e4330..55e1da02 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt @@ -19,5 +19,6 @@ data class Student( val scrapperBaseUrl: String, val mobileBaseUrl: String, val certificateKey: String, - val privateKey: String + val privateKey: String, + val semesters: List )