From 48a6c72d00dea32bf731fdf6781aa0e7ab466b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 11 May 2024 16:56:53 +0200 Subject: [PATCH] Migrate rest of ApiEndpoints usages to ApiEndpoints2 with map-based mapping --- .../wulkanowy/sdk/scrapper/ApiEndpoints.kt | 49 --------------- .../io/github/wulkanowy/sdk/scrapper/Utils.kt | 4 +- .../interceptor/AutoLoginInterceptor.kt | 11 +--- .../scrapper/repository/RegisterRepository.kt | 60 ++++++++++++------- 4 files changed, 45 insertions(+), 79 deletions(-) delete mode 100644 sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/ApiEndpoints.kt diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/ApiEndpoints.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/ApiEndpoints.kt deleted file mode 100644 index f2e42f07..00000000 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/ApiEndpoints.kt +++ /dev/null @@ -1,49 +0,0 @@ -package io.github.wulkanowy.sdk.scrapper - -internal object ApiEndpoints : IApiEndpoints { - - var currentVersion = 58698 - - private val endpoints - get() = when (currentVersion) { - 58698 -> ApiEndpoints_24_4_3_58698 - in 58666..58697 -> ApiEndpoints_24_4_2_58666 - else -> ApiEndpoints_24_4_1_58566 - } - override val UczenCache get() = endpoints.UczenCache - override val UczenDziennik get() = endpoints.UczenDziennik - override val PlusContext get() = endpoints.PlusContext - override val PlusOkresyKlasyfikacyjne get() = endpoints.PlusOkresyKlasyfikacyjne -} - -internal interface IApiEndpoints { - val UczenCache: String - val UczenDziennik: String - val PlusContext: String - val PlusOkresyKlasyfikacyjne: String -} - -internal object ApiEndpoints_24_4_1_58566 : IApiEndpoints { - override val UczenCache = "UczenCache" - override val UczenDziennik = "UczenDziennik" - - // uczenplus - override val PlusContext = "Context" - override val PlusOkresyKlasyfikacyjne = "OkresyKlasyfikacyjne" -} - -internal object ApiEndpoints_24_4_2_58666 : IApiEndpoints { - override val UczenCache = "f18ddca0-400e-47cc-89a1-4bbe6685810b" - override val UczenDziennik = "bef3daf1-07cd-4de6-b059-ee5909a7beb9" - override val PlusContext = "7fbcc3fc-1021-444e-86ec-506683e02337" - override val PlusOkresyKlasyfikacyjne = "0669f1fd-e6f0-4007-ba4a-1d99c9107bb4" -} - -internal object ApiEndpoints_24_4_3_58698 : IApiEndpoints { - override val UczenCache = "21a5186d-2aab-4123-bad7-269aa7173bb2" - override val UczenDziennik = "a01ea13f-14f0-4c56-8b91-790e5aeecdf1" - - // uczenplus - override val PlusContext = "Context" - override val PlusOkresyKlasyfikacyjne = "OkresyKlasyfikacyjne" -} diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt index 03060fc8..b5b06241 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt @@ -201,13 +201,13 @@ internal fun String.md5(): String { return digest.toHexString() } -internal fun HttpUrl.mapModuleUrls(moduleHost: String, url: HttpUrl, appVersion: String?): HttpUrl { +internal fun HttpUrl.mapModuleUrls(moduleHost: String, appVersion: String?): HttpUrl { val pathSegmentIndex = when (moduleHost) { StudentPlusModuleHost -> 3 StudentModuleHost, MessagesModuleHost -> 2 else -> error("Not supported!") } - val pathKey = url.pathSegments.getOrNull(pathSegmentIndex) + val pathKey = pathSegments.getOrNull(pathSegmentIndex) val mappedPath = ApiEndpointsMap[appVersion] ?.get(moduleHost) ?.get(pathKey?.substringBefore(".mvc")) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt index 5df79458..337df2be 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt @@ -1,6 +1,5 @@ package io.github.wulkanowy.sdk.scrapper.interceptor -import io.github.wulkanowy.sdk.scrapper.ApiEndpoints import io.github.wulkanowy.sdk.scrapper.ApiResponse import io.github.wulkanowy.sdk.scrapper.CookieJarCabinet import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType @@ -156,10 +155,6 @@ internal class AutoLoginInterceptor( return } - moduleHeaders.appVersion.substringAfterLast(".").toIntOrNull()?.let { - ApiEndpoints.currentVersion = it - } - when { MessagesModuleHost in url.host -> headersByHost[MessagesModuleHost] = moduleHeaders StudentPlusModuleHost in url.host -> headersByHost[StudentPlusModuleHost] = moduleHeaders @@ -177,9 +172,9 @@ internal class AutoLoginInterceptor( logger.info("X-V-AppVersion: ${headers?.appVersion}") val mappedUrl = when { - MessagesModuleHost in url.host -> url.mapModuleUrls(MessagesModuleHost, url, headers?.appVersion) - StudentPlusModuleHost in url.host -> url.mapModuleUrls(StudentPlusModuleHost, url, headers?.appVersion) - StudentModuleHost in url.host -> url.mapModuleUrls(StudentModuleHost, url, headers?.appVersion) + MessagesModuleHost in url.host -> url.mapModuleUrls(MessagesModuleHost, headers?.appVersion) + StudentPlusModuleHost in url.host -> url.mapModuleUrls(StudentPlusModuleHost, headers?.appVersion) + StudentModuleHost in url.host -> url.mapModuleUrls(StudentModuleHost, headers?.appVersion) else -> url } 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 962aceb9..e0c4541e 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 @@ -1,11 +1,12 @@ package io.github.wulkanowy.sdk.scrapper.repository -import io.github.wulkanowy.sdk.scrapper.ApiEndpoints import io.github.wulkanowy.sdk.scrapper.Scrapper import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException import io.github.wulkanowy.sdk.scrapper.exception.StudentGraduateException import io.github.wulkanowy.sdk.scrapper.getNormalizedSymbol import io.github.wulkanowy.sdk.scrapper.getScriptParam +import io.github.wulkanowy.sdk.scrapper.interceptor.StudentModuleHost +import io.github.wulkanowy.sdk.scrapper.interceptor.StudentPlusModuleHost import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors import io.github.wulkanowy.sdk.scrapper.isCurrentLoginHasEduOne import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse @@ -13,6 +14,7 @@ import io.github.wulkanowy.sdk.scrapper.login.InvalidSymbolException import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator +import io.github.wulkanowy.sdk.scrapper.mapModuleUrls import io.github.wulkanowy.sdk.scrapper.register.Diary import io.github.wulkanowy.sdk.scrapper.register.HomePageResponse import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent @@ -178,8 +180,8 @@ internal class RegisterRepository( val registerStudents = runCatching { when { isEduOne -> { - val (baseStudentPlus, _) = loginResult.getOrThrow() - getEduOneDiaries(baseStudentPlus) + val (baseStudentPlus, homepage) = loginResult.getOrThrow() + getEduOneDiaries(baseStudentPlus = baseStudentPlus, homepage = homepage) } else -> getStudentsFromOldModule( @@ -227,7 +229,7 @@ internal class RegisterRepository( ): List { val (_, startPage) = loginResult val isParent = isStudentFromParentAccount(startPage) - val diaries = getStudentDiaries() + val diaries = getStudentDiaries(startPage) return diaries.getStudentsFromDiaries( isParent = isParent, isEduOne = false, @@ -241,10 +243,19 @@ internal class RegisterRepository( } } - private suspend fun getStudentDiaries(): List = student - .getSchoolInfo(url.generate(UrlGenerator.Site.STUDENT) + "${ApiEndpoints.UczenDziennik}.mvc/Get") - .handleErrors() - .data.orEmpty() + private suspend fun getStudentDiaries(startPage: String): List { + val appVersion = getScriptParam("version", startPage).ifBlank { + getScriptParam("appVersion", startPage) + } + val diaryUrl = (url.generate(UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get") + .toHttpUrl() + .mapModuleUrls(StudentModuleHost, appVersion) + + return student + .getSchoolInfo(url = diaryUrl.toString()) + .handleErrors() + .data.orEmpty() + } private suspend fun getLoginType(symbol: String): Scrapper.LoginType { runCatching { symbolService.getSymbolPage(symbol) } @@ -312,8 +323,15 @@ internal class RegisterRepository( // used only for check is student from parent account private suspend fun isStudentFromParentAccount(startPage: String): Boolean? { + val appVersion = getScriptParam("version", startPage).ifBlank { + getScriptParam("appVersion", startPage) + } + val cacheUrl = (url.generate(UrlGenerator.Site.STUDENT) + "UczenCache.mvc/Get") + .toHttpUrl() + .mapModuleUrls(StudentModuleHost, appVersion) + val userCache = student.getUserCache( - url = url.generate(UrlGenerator.Site.STUDENT) + "${ApiEndpoints.UczenCache}.mvc/Get", + url = cacheUrl.toString(), token = getScriptParam("antiForgeryToken", startPage), appGuid = getScriptParam("appGuid", startPage), appVersion = getScriptParam("version", startPage), @@ -322,15 +340,25 @@ internal class RegisterRepository( return userCache?.isParent } - private suspend fun getEduOneDiaries(baseStudentPlus: String): List { + private suspend fun getEduOneDiaries(baseStudentPlus: String, homepage: String): List { + val appVersion = getScriptParam("version", homepage).ifBlank { + getScriptParam("appVersion", homepage) + } + val contextUrl = (baseStudentPlus + "api/Context") + .toHttpUrl() + .mapModuleUrls(StudentPlusModuleHost, appVersion) + val semestersUrl = (baseStudentPlus + "api/OkresyKlasyfikacyjne") + .toHttpUrl() + .mapModuleUrls(StudentModuleHost, appVersion) + return studentPlus - .getContextByUrl(url = baseStudentPlus + "api/${ApiEndpoints.PlusContext}").students + .getContextByUrl(url = contextUrl.toString()).students .map { contextStudent -> val semesters = runCatching { when { contextStudent.isAuthorizationRequired -> emptyList() else -> studentPlus.getSemestersByUrl( - url = baseStudentPlus + "api/${ApiEndpoints.PlusOkresyKlasyfikacyjne}", + url = semestersUrl.toString(), key = contextStudent.key, diaryId = contextStudent.registerId, ) @@ -364,14 +392,6 @@ internal class RegisterRepository( } else { start } - - val appVersion = getScriptParam("version", homepage).ifBlank { - getScriptParam("appVersion", homepage) - } - appVersion.substringAfterLast(".").toIntOrNull()?.let { - ApiEndpoints.currentVersion = it - } - return baseStudentPlus to homepage } }