From 42dbd67a39754f4b9d57959fe03a8b77fbc833e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 22 Dec 2022 01:10:21 +0100 Subject: [PATCH] Extract UrlGenerator to separate file --- build.gradle | 2 +- .../sdk/scrapper/login/UrlGenerator.kt | 33 +++++++++++++++++++ .../scrapper/repository/AccountRepository.kt | 7 ++-- .../scrapper/repository/RegisterRepository.kt | 15 +++++---- .../sdk/scrapper/service/ServiceManager.kt | 26 +-------------- .../sdk/scrapper/register/RegisterTest.kt | 3 +- .../repository/RegisterRepositoryTest.kt | 3 +- 7 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/login/UrlGenerator.kt diff --git a/build.gradle b/build.gradle index 49e1695d..9f999c94 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { } ext { - PUBLISH_VERSION = '1.8.3' + PUBLISH_VERSION = '1.8.4-SNAPSHOT' SITE_URL = 'https://github.com/wulkanowy/sdk' GIT_URL = 'https://github.com/wulkanowy/sdk.git' diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/login/UrlGenerator.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/login/UrlGenerator.kt new file mode 100644 index 00000000..37dcb3dc --- /dev/null +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/login/UrlGenerator.kt @@ -0,0 +1,33 @@ +package io.github.wulkanowy.sdk.scrapper.login + +import java.net.URL + +class UrlGenerator( + private val schema: String, + private val host: String, + var symbol: String, + var schoolId: String, +) { + + constructor(url: URL, symbol: String, schoolId: String) : this(url.protocol, url.host, symbol, schoolId) + + enum class Site { + BASE, LOGIN, HOME, SNP, STUDENT, MESSAGES + } + + fun generate(type: Site): String { + if (type == Site.BASE) return "$schema://$host" + return "$schema://${getSubDomain(type)}.$host/$symbol/${if (type == Site.SNP || type == Site.STUDENT) "$schoolId/" else ""}" + } + + private fun getSubDomain(type: Site): String { + return when (type) { + Site.LOGIN -> "cufs" + Site.HOME -> "uonetplus" + Site.SNP -> "uonetplus-opiekun" + Site.STUDENT -> "uonetplus-uczen" + Site.MESSAGES -> "uonetplus-wiadomosciplus" + else -> error("unknown") + } + } +} diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/AccountRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/AccountRepository.kt index 5238e412..63bc7fc7 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/AccountRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/AccountRepository.kt @@ -13,6 +13,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.InvalidCaptchaException import io.github.wulkanowy.sdk.scrapper.exception.InvalidEmailException import io.github.wulkanowy.sdk.scrapper.exception.NoAccountFoundException import io.github.wulkanowy.sdk.scrapper.exception.PasswordResetErrorException +import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator import io.github.wulkanowy.sdk.scrapper.service.AccountService import io.github.wulkanowy.sdk.scrapper.service.ServiceManager import java.net.URL @@ -83,7 +84,7 @@ class AccountRepository(private val account: AccountService) { } return if (unlockUrl.first == AUTO) { - val loginType = getLoginType(ServiceManager.UrlGenerator(url, symbol, "")) + val loginType = getLoginType(UrlGenerator(url, symbol, "")) loginType to when (loginType) { STANDARD -> "https://cufs.vulcan.net.pl/$symbol/AccountManage/UnlockAccount" ADFSLightScoped -> "https://adfslight.vulcan.net.pl/$symbol/AccountManage/UnlockAccountRequest" @@ -92,8 +93,8 @@ class AccountRepository(private val account: AccountService) { } else unlockUrl } - private suspend fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Scrapper.LoginType { - val page = account.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").page + private suspend fun getLoginType(urlGenerator: UrlGenerator): Scrapper.LoginType { + val page = account.getFormType(urlGenerator.generate(UrlGenerator.Site.LOGIN) + "Account/LogOn").page return when { page.select(SELECTOR_STANDARD).isNotEmpty() -> STANDARD 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 0021d025..ede2003c 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 @@ -10,6 +10,7 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors import io.github.wulkanowy.sdk.scrapper.login.AccountPermissionException import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse import io.github.wulkanowy.sdk.scrapper.login.LoginHelper +import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator import io.github.wulkanowy.sdk.scrapper.register.AuthInfo import io.github.wulkanowy.sdk.scrapper.register.Diary import io.github.wulkanowy.sdk.scrapper.register.Permission @@ -37,7 +38,7 @@ class RegisterRepository( private val loginHelper: LoginHelper, private val register: RegisterService, private val student: StudentService, - private val url: ServiceManager.UrlGenerator + private val url: UrlGenerator ) { companion object { @@ -94,8 +95,8 @@ class RegisterRepository( return getLoginType(url.also { it.symbol = symbol }) } - private suspend fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Scrapper.LoginType { - val page = register.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").page + private suspend fun getLoginType(urlGenerator: UrlGenerator): Scrapper.LoginType { + val page = register.getFormType(urlGenerator.generate(UrlGenerator.Site.LOGIN) + "Account/LogOn").page return when { page.select(SELECTOR_STANDARD).isNotEmpty() -> Scrapper.LoginType.STANDARD page.select(SELECTOR_ADFS).isNotEmpty() -> Scrapper.LoginType.ADFS @@ -126,7 +127,7 @@ class RegisterRepository( url.schoolId = unit.symbol val studentStartPage = try { - student.getStart(url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "Start") + student.getStart(url.generate(UrlGenerator.Site.STUDENT) + "Start") } catch (e: TemporarilyDisabledException) { logger.debug("Start page is unavailable", e) return listOf() @@ -153,7 +154,7 @@ class RegisterRepository( schoolName = getScriptParam("organizationName", studentStartPage, "${unit.name} ${unit.short}"), className = diary.symbol.orEmpty(), classId = classId, - baseUrl = url.generate(ServiceManager.UrlGenerator.Site.BASE), + baseUrl = url.generate(UrlGenerator.Site.BASE), loginType = loginType, isParent = cache?.isParent == true, semesters = diaries.toSemesters(diary.studentId, classId, authInfo?.loginId ?: diary.studentId), @@ -165,14 +166,14 @@ class RegisterRepository( } private suspend fun getStudentCache(startPage: String) = student.getUserCache( - url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "UczenCache.mvc/Get", + url.generate(UrlGenerator.Site.STUDENT) + "UczenCache.mvc/Get", getScriptParam("antiForgeryToken", startPage), getScriptParam("appGuid", startPage), getScriptParam("version", startPage) ).data private suspend fun getStudentDiaries() = student - .getSchoolInfo(url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get") + .getSchoolInfo(url.generate(UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get") .handleErrors() .data.orEmpty() diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManager.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManager.kt index 6fe59e6f..a6164a5c 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManager.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManager.kt @@ -12,6 +12,7 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.HttpErrorInterceptor import io.github.wulkanowy.sdk.scrapper.interceptor.StudentCookieInterceptor import io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor import io.github.wulkanowy.sdk.scrapper.login.LoginHelper +import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.modules.SerializersModule @@ -215,29 +216,4 @@ class ServiceManager( } } } - - class UrlGenerator(private val schema: String, private val host: String, var symbol: String, var schoolId: String) { - - constructor(url: URL, symbol: String, schoolId: String) : this(url.protocol, url.host, symbol, schoolId) - - enum class Site { - BASE, LOGIN, HOME, SNP, STUDENT, MESSAGES - } - - fun generate(type: Site): String { - if (type == Site.BASE) return "$schema://$host" - return "$schema://${getSubDomain(type)}.$host/$symbol/${if (type == Site.SNP || type == Site.STUDENT) "$schoolId/" else ""}" - } - - private fun getSubDomain(type: Site): String { - return when (type) { - Site.LOGIN -> "cufs" - Site.HOME -> "uonetplus" - Site.SNP -> "uonetplus-opiekun" - Site.STUDENT -> "uonetplus-uczen" - Site.MESSAGES -> "uonetplus-wiadomosciplus" - else -> error("unknown") - } - } - } } diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt index 329baed3..e03541e1 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.sdk.scrapper.BaseLocalTest import io.github.wulkanowy.sdk.scrapper.Scrapper import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginTest +import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator import io.github.wulkanowy.sdk.scrapper.repository.RegisterRepository import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.RegisterService @@ -52,7 +53,7 @@ class RegisterTest : BaseLocalTest() { ) ), student = getService(StudentService::class.java, "http://fakelog.localhost:3000", false), - url = ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", "default", "123") + url = UrlGenerator("http", "fakelog.localhost:3000", "default", "123") ) } 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 96b8252b..b861efbc 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 @@ -6,6 +6,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.VulcanException import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginTest +import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator import io.github.wulkanowy.sdk.scrapper.register.RegisterTest import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.RegisterService @@ -39,7 +40,7 @@ class RegisterRepositoryTest : BaseLocalTest() { okHttp = getOkHttp(errorInterceptor = false, autoLoginInterceptorOn = false) ), student = getService(service = StudentService::class.java, html = false), - url = ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", symbol, "") + url = UrlGenerator("http", "fakelog.localhost:3000", symbol, "") ) }