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 eb796cee..102339f4 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 @@ -191,10 +191,14 @@ class Scrapper { HomepageRepository(serviceManager.getHomepageService()) } - fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String = "Default"): Single { + fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String): Single { return register.getPasswordResetCaptcha(registerBaseUrl, symbol) } + fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single> { + return register.sendPasswordResetRequest(registerBaseUrl, symbol, email, captchaCode) + } + fun getStudents() = register.getStudents() fun getSemesters() = if (useNewStudent) studentStart.getSemesters() else snpStart.getSemesters() diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SentUnlockAccountResponse.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SentUnlockAccountResponse.kt new file mode 100644 index 00000000..794b9985 --- /dev/null +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/SentUnlockAccountResponse.kt @@ -0,0 +1,16 @@ +package io.github.wulkanowy.sdk.scrapper.register + +import org.jsoup.nodes.Element +import pl.droidsonroids.jspoon.annotation.Selector + +class SentUnlockAccountResponse { + + @Selector("title") + lateinit var title: String + + @Selector(".ErrorMessage, #ErrorTextLabel, .UnlockAccountSummary p, #box .box-p") + lateinit var message: String + + @Selector("html") + lateinit var doc: Element +} diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccount.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccountResponse.kt similarity index 86% rename from sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccount.kt rename to sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccountResponse.kt index bdfaef67..35c96140 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccount.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/UnlockAccountResponse.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.sdk.scrapper.register import pl.droidsonroids.jspoon.annotation.Selector -class UnlockAccount { +class UnlockAccountResponse { @Selector(".g-recaptcha", attr = "data-sitekey") lateinit var recaptcha: String 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 eaa37a52..70bea166 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 @@ -41,6 +41,20 @@ class RegisterRepository( } fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String): Single { + return get(registerBaseUrl, symbol) + .flatMap { register.getPasswordResetPageWithCaptcha(it) } + .map { it.recaptcha } + } + + fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single> { + return get(registerBaseUrl, symbol) + .flatMap { register.sendPasswordResetRequest(it, email, captchaCode) } + .map { + (it.title == "Podsumowanie operacji") to it.message + } + } + + fun get(registerBaseUrl: String, symbol: String): Single { val url = URL(registerBaseUrl) return when (url.host) { "fakelog.cf" -> Single.just("https://cufs.fakelog.cf/Default/AccountManage/UnlockAccount") @@ -56,7 +70,7 @@ class RegisterRepository( } } else -> throw ScrapperException("Nieznany dziennik") - }.flatMap { register.getPasswordResetPageWithCaptcha(it) }.map { it.recaptcha } + } } fun getStudents(): Single> { diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/RegisterService.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/RegisterService.kt index b32f473f..559f8268 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/RegisterService.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/service/RegisterService.kt @@ -1,9 +1,13 @@ package io.github.wulkanowy.sdk.scrapper.service import io.github.wulkanowy.sdk.scrapper.register.LoginForm -import io.github.wulkanowy.sdk.scrapper.register.UnlockAccount +import io.github.wulkanowy.sdk.scrapper.register.SentUnlockAccountResponse +import io.github.wulkanowy.sdk.scrapper.register.UnlockAccountResponse import io.reactivex.Single +import retrofit2.http.Field +import retrofit2.http.FormUrlEncoded import retrofit2.http.GET +import retrofit2.http.POST import retrofit2.http.Url interface RegisterService { @@ -12,5 +16,13 @@ interface RegisterService { fun getFormType(@Url url: String): Single @GET - fun getPasswordResetPageWithCaptcha(@Url url: String): Single + fun getPasswordResetPageWithCaptcha(@Url url: String): Single + + @POST + @FormUrlEncoded + fun sendPasswordResetRequest( + @Url registerBaseUrl: String, + @Field("Email") email: String, + @Field("g-recaptcha-response") captchaCode: String + ): Single } diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt index b3b3d14e..33e4337a 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt @@ -73,6 +73,19 @@ class ScrapperRemoteTest : BaseTest() { assertEquals("6LeAGMYUAAAAAMszd5VWZTEb5WQHqsNT1F4GCqUd", codeObserver.values()[0]) } + @Test + fun sendPasswordResetRequest() { + val res = api.sendPasswordResetRequest("https://fakelog.cf", "Default", "jan@fakelog.cf", "03AOLTBLQRPyr0pWvWLRAgD4hRLfxktoqD2IVweeMuXwbkpR_8S9YQtcS3cAXqUOyEw3NxfvwzV0lTjgFWyl8j3UXGQpsc2nvQcqIofj1N8DYfxvtZO-h24W_S0Z9-fDnfXErd7vERS-Ny4d5IU1FupBAEKvT8rrf3OA3GYYbMM7TwB8b_o9Tt192TqYnSxkyIYE4UdaZnLBA0KIXxlBAoqM6QGlPEsSPK9gmCGx-0hn68w-UBQkv_ghRruf4kpv2Shw5emcP-qHBlv3YjAagsb_358K0v8uGJeyLrx4dXN9Ky02TXFMKYWNHz29fjhfunxT73u_PrsLj56f-MjOXrqO894NkUlJ7RkTTclwIsqXtJ794LEBH--mtsqZBND0miR5-odmZszqiNB3V5UsS5ObsqF_fWMl2TCWyNTTvF4elOGwOEeKiumVpjB6e740COxvxN3vbkNWxP9eeghpd5nPN5l2wUV3VL2R5s44TbqHqkrkNpUOd3h7efs3cQtCfGc-tCXoqLC26LxT7aztvKpjXMuqGEf-7wbQ") + val resObserver = TestObserver>() + res.subscribe(resObserver) + resObserver.assertComplete() + + with(resObserver.values()[0]) { + assertEquals(true, first) + assertTrue(second.startsWith("Wysłano wiadomość na zapisany w systemie adres e-mail")) + } + } + @Test fun studentsTest() { val students = api.getStudents() @@ -124,7 +137,7 @@ class ScrapperRemoteTest : BaseTest() { } values[6].run { -// assertEquals(12, semesterId) + // assertEquals(12, semesterId) // assertEquals(2, semesterNumber) } } 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 0c04d114..f41ff456 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt @@ -171,6 +171,10 @@ class Sdk { return scrapper.getPasswordResetCaptcha(registerBaseUrl, symbol) } + fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single> { + return scrapper.sendPasswordResetRequest(registerBaseUrl, symbol, email, captchaCode) + } + fun getStudentsFromMobileApi(token: String, pin: String, symbol: String, apiKey: String = ""): Single> { return mobile.getCertificate(token, pin, symbol, buildTag, androidVersion) .flatMap { mobile.getStudents(it, apiKey) }