From 8ffd7b0d3511e67f83b70035988730a82b1022ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 2 Mar 2024 10:45:42 +0100 Subject: [PATCH] Add last announcement feature --- sdk-scrapper/api/sdk-scrapper.api | 18 ++++++++ .../github/wulkanowy/sdk/scrapper/Scrapper.kt | 3 ++ .../sdk/scrapper/home/LastAnnouncement.kt | 10 +++++ .../scrapper/repository/HomepageRepository.kt | 19 ++++++++- .../sdk/scrapper/service/HomepageService.kt | 4 ++ .../sdk/scrapper/homepage/HomepageTest.kt | 27 ++++++++++++ .../homepage/GetLastAnnouncements.json | 41 +++++++++++++++++++ sdk/api/sdk.api | 16 ++++++++ .../kotlin/io/github/wulkanowy/sdk/Sdk.kt | 9 ++++ .../wulkanowy/sdk/mapper/HomePageMapper.kt | 10 +++++ .../wulkanowy/sdk/pojo/LastAnnouncement.kt | 9 ++++ 11 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/home/LastAnnouncement.kt create mode 100644 sdk-scrapper/src/test/resources/io/github/wulkanowy/sdk/scrapper/homepage/GetLastAnnouncements.json create mode 100644 sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/LastAnnouncement.kt diff --git a/sdk-scrapper/api/sdk-scrapper.api b/sdk-scrapper/api/sdk-scrapper.api index 038de8ce..a3c48881 100644 --- a/sdk-scrapper/api/sdk-scrapper.api +++ b/sdk-scrapper/api/sdk-scrapper.api @@ -38,6 +38,7 @@ public final class io/github/wulkanowy/sdk/scrapper/Scrapper { public final fun getKidsLessonPlan (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getKidsLuckyNumbers (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getKindergartenDiaryId ()I + public final fun getLastAnnouncements (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastGrades (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastHomework (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastStudentLessons (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -836,6 +837,23 @@ public final class io/github/wulkanowy/sdk/scrapper/home/GovernmentUnit$Companio public final fun serializer ()Lkotlinx/serialization/KSerializer; } +public final class io/github/wulkanowy/sdk/scrapper/home/LastAnnouncement { + public fun (Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V + public final fun component1 ()Ljava/time/LocalDate; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/lang/String; + public final fun component4 ()Ljava/lang/String; + public final fun copy (Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lio/github/wulkanowy/sdk/scrapper/home/LastAnnouncement; + public static synthetic fun copy$default (Lio/github/wulkanowy/sdk/scrapper/home/LastAnnouncement;Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lio/github/wulkanowy/sdk/scrapper/home/LastAnnouncement; + public fun equals (Ljava/lang/Object;)Z + public final fun getAuthor ()Ljava/lang/String; + public final fun getContent ()Ljava/lang/String; + public final fun getDate ()Ljava/time/LocalDate; + public final fun getSubject ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class io/github/wulkanowy/sdk/scrapper/home/LuckyNumber { public fun (Ljava/lang/String;Ljava/lang/String;I)V public final fun component1 ()Ljava/lang/String; diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt index 4f585fe5..370d7224 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt @@ -13,6 +13,7 @@ import io.github.wulkanowy.sdk.scrapper.grades.GradesStatisticsPartial import io.github.wulkanowy.sdk.scrapper.grades.GradesStatisticsSemester import io.github.wulkanowy.sdk.scrapper.home.DirectorInformation import io.github.wulkanowy.sdk.scrapper.home.GovernmentUnit +import io.github.wulkanowy.sdk.scrapper.home.LastAnnouncement import io.github.wulkanowy.sdk.scrapper.home.LuckyNumber import io.github.wulkanowy.sdk.scrapper.homework.Homework import io.github.wulkanowy.sdk.scrapper.login.LoginHelper @@ -495,6 +496,8 @@ class Scrapper { suspend fun getDirectorInformation(): List = homepage.getDirectorInformation() + suspend fun getLastAnnouncements(): List = homepage.getLastAnnouncements() + suspend fun getSelfGovernments(): List = homepage.getSelfGovernments() suspend fun getStudentThreats(): List = homepage.getStudentThreats() diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/home/LastAnnouncement.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/home/LastAnnouncement.kt new file mode 100644 index 00000000..0a6356c7 --- /dev/null +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/home/LastAnnouncement.kt @@ -0,0 +1,10 @@ +package io.github.wulkanowy.sdk.scrapper.home + +import java.time.LocalDate + +data class LastAnnouncement( + val date: LocalDate, + val subject: String, + val author: String, + val content: String, +) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/HomepageRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/HomepageRepository.kt index 0a344b57..389f981b 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/HomepageRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/HomepageRepository.kt @@ -3,11 +3,12 @@ package io.github.wulkanowy.sdk.scrapper.repository import io.github.wulkanowy.sdk.scrapper.getScriptParam import io.github.wulkanowy.sdk.scrapper.home.DirectorInformation import io.github.wulkanowy.sdk.scrapper.home.GovernmentUnit +import io.github.wulkanowy.sdk.scrapper.home.LastAnnouncement import io.github.wulkanowy.sdk.scrapper.home.LuckyNumber import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors import io.github.wulkanowy.sdk.scrapper.service.HomepageService -import io.github.wulkanowy.sdk.scrapper.toDate import io.github.wulkanowy.sdk.scrapper.toLocalDate +import java.time.LocalDate internal class HomepageRepository(private val api: HomepageService) { @@ -27,7 +28,7 @@ internal class HomepageRepository(private val api: HomepageService) { return requireNotNull(res).flatMap { wrapper -> wrapper.content.map { DirectorInformation( - date = it.name.substringBefore(" ").toDate("dd.MM.yyyy").toLocalDate(), + date = it.name.substringBefore(" ").toLocalDate("dd.MM.yyyy"), subject = it.name.substringAfter(" "), content = it.data.orEmpty(), ) @@ -35,6 +36,20 @@ internal class HomepageRepository(private val api: HomepageService) { }.sortedBy { it.date } } + suspend fun getLastAnnouncements(): List { + val res = api.getLastAnnouncements(getToken()).handleErrors().data + return requireNotNull(res).flatMap { wrapper -> + wrapper.content.map { + LastAnnouncement( + subject = it.name.trim(), + date = it.symbol?.substringBefore(", ")?.toLocalDate("dd.MM.yyyy") ?: LocalDate.EPOCH, + author = it.symbol?.substringAfter(", ").orEmpty(), + content = it.data.orEmpty(), + ) + } + } + } + suspend fun getSelfGovernments(): List { val res = api.getSelfGovernments(getToken()).handleErrors().data return requireNotNull(res) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/HomepageService.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/HomepageService.kt index 4decd193..06a3ea9f 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/HomepageService.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/HomepageService.kt @@ -17,6 +17,10 @@ internal interface HomepageService { @POST("Start.mvc/GetStudentDirectorInformations") suspend fun getDirectorInformation(@Field("permissions") token: String): ApiResponse> + @FormUrlEncoded + @POST("Start.mvc/GetLastAnnouncements") + suspend fun getLastAnnouncements(@Field("permissions") token: String): ApiResponse> + @FormUrlEncoded @POST("Start.mvc/GetSelfGovernments") suspend fun getSelfGovernments(@Field("permissions") token: String): ApiResponse> diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/homepage/HomepageTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/homepage/HomepageTest.kt index f448ae0a..b18541fc 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/homepage/HomepageTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/homepage/HomepageTest.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.sdk.scrapper.home.LuckyNumber import io.github.wulkanowy.sdk.scrapper.repository.HomepageRepository import io.github.wulkanowy.sdk.scrapper.service.HomepageService import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test @@ -40,6 +41,32 @@ class HomepageTest : BaseLocalTest() { } } + @Test + fun getLastAnnouncements() = runTest { + with(server) { + enqueue("Index.html") + enqueue("GetLastAnnouncements.json") + start(3000) + } + + val infos = repo.getLastAnnouncements() + assertEquals(2, infos.size) + + with(infos[0]) { + assertEquals(of(2024, 2, 27), date) + assertEquals("Wydarzenia", subject) + assertEquals("Jan kowalski", author) + assertEquals("

W dniu 29 lutego 2024 r. uczniowie klasy 3d biorą udział w wyjściu na strzelnicę.

", content) + } + + with(infos[1]) { + assertEquals(of(2023, 12, 5), date) + assertEquals("Platformy", subject) + assertEquals("Malwina Czerwieńska", author) + assertEquals("

Platformy do wykorzystania dla uczniów w zakresie kompetencji cyfrowych:

", content) + } + } + @Test fun getSelfGovernments() { with(server) { diff --git a/sdk-scrapper/src/test/resources/io/github/wulkanowy/sdk/scrapper/homepage/GetLastAnnouncements.json b/sdk-scrapper/src/test/resources/io/github/wulkanowy/sdk/scrapper/homepage/GetLastAnnouncements.json new file mode 100644 index 00000000..0d254be4 --- /dev/null +++ b/sdk-scrapper/src/test/resources/io/github/wulkanowy/sdk/scrapper/homepage/GetLastAnnouncements.json @@ -0,0 +1,41 @@ +{ + "data": [ + { + "IkonkaNazwa": null, + "Num": null, + "Zawartosc": [ + { + "IkonkaNazwa": null, + "Num": null, + "Zawartosc": [], + "Rozszerzenie": false, + "Nazwa": "Wydarzenia ", + "Url": null, + "Dane": "

W dniu 29 lutego 2024 r. uczniowie klasy 3d biorą udział w wyjściu na strzelnicę.

", + "Symbol": "27.02.2024, Jan kowalski", + "Nieaktywny": false + }, + { + "IkonkaNazwa": null, + "Num": null, + "Zawartosc": [], + "Rozszerzenie": false, + "Nazwa": "Platformy", + "Url": null, + "Dane": "

Platformy do wykorzystania dla uczniów w zakresie kompetencji cyfrowych:

", + "Symbol": "05.12.2023, Malwina Czerwieńska", + "Nieaktywny": false + } + ], + "Rozszerzenie": false, + "Nazwa": "", + "Url": null, + "Dane": null, + "Symbol": null, + "Nieaktywny": false + } + ], + "success": true, + "errorMessage": null, + "feedback": null +} diff --git a/sdk/api/sdk.api b/sdk/api/sdk.api index f9793f19..94a96d75 100644 --- a/sdk/api/sdk.api +++ b/sdk/api/sdk.api @@ -35,6 +35,7 @@ public final class io/github/wulkanowy/sdk/Sdk { public final fun getKidsLuckyNumbers (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getKidsTimetable (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getKindergartenDiaryId ()I + public final fun getLastAnnouncements (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastExams (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastGrades (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLastHomework (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -589,6 +590,21 @@ public final class io/github/wulkanowy/sdk/pojo/HomeworkAttachment { public fun toString ()Ljava/lang/String; } +public final class io/github/wulkanowy/sdk/pojo/LastAnnouncement { + public fun (Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;)V + public final fun component1 ()Ljava/time/LocalDate; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/lang/String; + public final fun copy (Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;)Lio/github/wulkanowy/sdk/pojo/LastAnnouncement; + public static synthetic fun copy$default (Lio/github/wulkanowy/sdk/pojo/LastAnnouncement;Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lio/github/wulkanowy/sdk/pojo/LastAnnouncement; + public fun equals (Ljava/lang/Object;)Z + public final fun getAuthor ()Ljava/lang/String; + public final fun getContent ()Ljava/lang/String; + public final fun getDate ()Ljava/time/LocalDate; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class io/github/wulkanowy/sdk/pojo/Lesson { public fun (ILjava/time/ZonedDateTime;Ljava/time/ZonedDateTime;Ljava/time/LocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZ)V public final fun component1 ()I diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt index 486e2cc2..db739fa0 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt @@ -14,6 +14,7 @@ import io.github.wulkanowy.sdk.mapper.mapGrades import io.github.wulkanowy.sdk.mapper.mapGradesSemesterStatistics import io.github.wulkanowy.sdk.mapper.mapHebeUser import io.github.wulkanowy.sdk.mapper.mapHomework +import io.github.wulkanowy.sdk.mapper.mapLastAnnouncements import io.github.wulkanowy.sdk.mapper.mapLuckyNumbers import io.github.wulkanowy.sdk.mapper.mapMailboxes import io.github.wulkanowy.sdk.mapper.mapMenu @@ -47,6 +48,7 @@ import io.github.wulkanowy.sdk.pojo.GradeStatisticsSemester import io.github.wulkanowy.sdk.pojo.GradeStatisticsSubject import io.github.wulkanowy.sdk.pojo.Grades import io.github.wulkanowy.sdk.pojo.Homework +import io.github.wulkanowy.sdk.pojo.LastAnnouncement import io.github.wulkanowy.sdk.pojo.LuckyNumber import io.github.wulkanowy.sdk.pojo.Mailbox import io.github.wulkanowy.sdk.pojo.Menu @@ -649,6 +651,13 @@ class Sdk { } } + suspend fun getLastAnnouncements(): List = withContext(Dispatchers.IO) { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getLastAnnouncements().mapLastAnnouncements() + Mode.HEBE -> throw NotImplementedError("Not available in HEBE mode") + } + } + suspend fun getSelfGovernments(): List = withContext(Dispatchers.IO) { when (mode) { Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSelfGovernments().mapToUnits() diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/HomePageMapper.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/HomePageMapper.kt index d93daa3d..7e4a59b1 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/HomePageMapper.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/HomePageMapper.kt @@ -1,7 +1,9 @@ package io.github.wulkanowy.sdk.mapper import io.github.wulkanowy.sdk.pojo.DirectorInformation +import io.github.wulkanowy.sdk.pojo.LastAnnouncement import io.github.wulkanowy.sdk.scrapper.home.DirectorInformation as ScrapperDirectorInformation +import io.github.wulkanowy.sdk.scrapper.home.LastAnnouncement as ScrapperLastAnnouncement internal fun List.mapDirectorInformation() = map { DirectorInformation( @@ -10,3 +12,11 @@ internal fun List.mapDirectorInformation() = map { content = it.content, ) } + +internal fun List.mapLastAnnouncements() = map { + LastAnnouncement( + date = it.date, + author = it.author, + content = it.content, + ) +} diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/LastAnnouncement.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/LastAnnouncement.kt new file mode 100644 index 00000000..a85aa4c2 --- /dev/null +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/LastAnnouncement.kt @@ -0,0 +1,9 @@ +package io.github.wulkanowy.sdk.pojo + +import java.time.LocalDate + +data class LastAnnouncement( + val date: LocalDate, + val author: String, + val content: String, +)