From aadeb64a9fcc1f6518cb12042f35af8dab1566de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Mon, 1 Jun 2020 02:22:33 +0200 Subject: [PATCH] Create registers selector consts in account repo --- .../interceptor/NotLoggedInErrorInterceptor.kt | 12 ++++++++---- .../sdk/scrapper/repository/AccountRepository.kt | 15 +++++++++++---- .../sdk/scrapper/repository/RegisterRepository.kt | 12 ++++++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/NotLoggedInErrorInterceptor.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/NotLoggedInErrorInterceptor.kt index 467e1be7..0a76d13d 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/NotLoggedInErrorInterceptor.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/NotLoggedInErrorInterceptor.kt @@ -7,6 +7,10 @@ import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLight import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLightScoped import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.STANDARD import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException +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 +import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_STANDARD import okhttp3.Interceptor import okhttp3.Response import org.jsoup.Jsoup @@ -26,10 +30,10 @@ class NotLoggedInErrorInterceptor(private val loginType: LoginType) : Intercepto } if (when (loginType) { - STANDARD -> doc.select(".loginButton, .LogOnBoard input[type=submit]") - ADFS -> doc.select("form[name=form1] #SubmitButton") - ADFSLight, ADFSLightScoped -> doc.select(".submit-button, form #SubmitButton") - ADFSCards -> doc.select("#PassiveSignInButton") + STANDARD -> doc.select(SELECTOR_STANDARD) + ADFS -> doc.select(SELECTOR_ADFS) + ADFSLight, ADFSLightScoped -> doc.select(SELECTOR_ADFS_LIGHT) + ADFSCards -> doc.select(SELECTOR_ADFS_CARDS) else -> Elements() }.isNotEmpty() ) { 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 46ac4e57..200afa06 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 @@ -20,6 +20,13 @@ import java.net.URL class AccountRepository(private val account: AccountService) { + companion object { + const val SELECTOR_STANDARD = ".loginButton, .LogOnBoard input[type=submit]" // remove second selector? + const val SELECTOR_ADFS = "form[name=form1] #SubmitButton" + const val SELECTOR_ADFS_LIGHT = ".submit-button, form #SubmitButton" + const val SELECTOR_ADFS_CARDS = "#PassiveSignInButton" + } + fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String): Single> { return getPasswordResetUrl(registerBaseUrl, symbol.trim()).flatMap { (_, resetUrl) -> account.getPasswordResetPageWithCaptcha(resetUrl) @@ -82,9 +89,9 @@ class AccountRepository(private val account: AccountService) { private fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Single { return account.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").map { it.page }.map { when { - it.select(".LogOnBoard input[type=submit]").isNotEmpty() -> STANDARD - it.select("form[name=form1] #SubmitButton").isNotEmpty() -> ADFS - it.select(".submit-button, form #SubmitButton").isNotEmpty() -> { + it.select(SELECTOR_STANDARD).isNotEmpty() -> STANDARD + it.select(SELECTOR_ADFS).isNotEmpty() -> ADFS + it.select(SELECTOR_ADFS_LIGHT).isNotEmpty() -> { it.selectFirst("form").attr("action").run { when { contains("cufs.edu.lublin.eu") -> ADFSLightCufs @@ -94,7 +101,7 @@ class AccountRepository(private val account: AccountService) { } } } - it.select("#PassiveSignInButton").isNotEmpty() -> ADFSCards + it.select(SELECTOR_ADFS_CARDS).isNotEmpty() -> ADFSCards else -> throw ScrapperException("Nieznany typ dziennika '${it.select("title")}") } } 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 87202f33..4671d568 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,10 @@ import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.register.SendCertificateResponse import io.github.wulkanowy.sdk.scrapper.register.Student import io.github.wulkanowy.sdk.scrapper.register.StudentAndParentResponse +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 +import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_STANDARD import io.github.wulkanowy.sdk.scrapper.service.RegisterService import io.github.wulkanowy.sdk.scrapper.service.ServiceManager import io.github.wulkanowy.sdk.scrapper.service.StudentAndParentService @@ -100,9 +104,9 @@ class RegisterRepository( private fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Single { return register.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").map { it.page }.map { when { - it.select(".LogOnBoard input[type=submit]").isNotEmpty() -> Scrapper.LoginType.STANDARD - it.select("form[name=form1] #SubmitButton").isNotEmpty() -> Scrapper.LoginType.ADFS - it.select(".submit-button, form #SubmitButton").isNotEmpty() -> { + it.select(SELECTOR_STANDARD).isNotEmpty() -> Scrapper.LoginType.STANDARD + it.select(SELECTOR_ADFS).isNotEmpty() -> Scrapper.LoginType.ADFS + it.select(SELECTOR_ADFS_LIGHT).isNotEmpty() -> { it.selectFirst("form").attr("action").run { when { contains("cufs.edu.lublin.eu") -> Scrapper.LoginType.ADFSLightCufs @@ -112,7 +116,7 @@ class RegisterRepository( } } } - it.select("#PassiveSignInButton").isNotEmpty() -> Scrapper.LoginType.ADFSCards + it.select(SELECTOR_ADFS_CARDS).isNotEmpty() -> Scrapper.LoginType.ADFSCards else -> throw ScrapperException("Nieznany typ dziennika") } }