From 010f7fa1fedd4137a5093709a13f2abe40bf3ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 27 Feb 2020 23:01:47 +0100 Subject: [PATCH] [API/Idziennik] Add getting lucky number from website. Fix API lucky number date. --- .../edziennik/data/api/Regexes.kt | 4 +++ .../data/api/IdziennikApiCurrentRegister.kt | 2 +- .../idziennik/login/IdziennikLoginWeb.kt | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt index 725d4fc7..d44df952 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt @@ -109,6 +109,10 @@ object Regexes { val IDZIENNIK_MESSAGES_RECIPIENT_PARENT by lazy { """(.+?)\s\((.+)\)""".toRegex() } + /*Szczęśliwy los na dzisiaj to 19. Los na jutro to 22*/ + val IDZIENNIK_WEB_LUCKY_NUMBER by lazy { + """dzisiaj to ([0-9]+)""".toRegex() + } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/api/IdziennikApiCurrentRegister.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/api/IdziennikApiCurrentRegister.kt index e23290de..0e000fb1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/api/IdziennikApiCurrentRegister.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/api/IdziennikApiCurrentRegister.kt @@ -69,7 +69,7 @@ class IdziennikApiCurrentRegister(override val data: DataIdziennik, val luckyNumberObject = LuckyNumber( data.profileId, - Date.getToday(), + luckyNumberDate, luckyNumber ) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/login/IdziennikLoginWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/login/IdziennikLoginWeb.kt index 9f10268e..3cfc4899 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/login/IdziennikLoginWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/login/IdziennikLoginWeb.kt @@ -13,9 +13,12 @@ import pl.szczodrzynski.edziennik.MINUTE import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.DataIdziennik import pl.szczodrzynski.edziennik.data.api.models.ApiError +import pl.szczodrzynski.edziennik.data.db.entity.LuckyNumber +import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.get import pl.szczodrzynski.edziennik.getUnixDate import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.models.Date class IdziennikLoginWeb(val data: DataIdziennik, val onSuccess: () -> Unit) { companion object { @@ -69,6 +72,29 @@ class IdziennikLoginWeb(val data: DataIdziennik, val onSuccess: () -> Unit) { data.apiBearer = cookies.singleOrNull { it.name() == "Bearer" }?.value() ?: return@run ERROR_LOGIN_IDZIENNIK_WEB_NO_BEARER data.loginExpiryTime = response.getUnixDate() + 30 * MINUTE /* after about 40 minutes the login didn't work already */ data.apiExpiryTime = response.getUnixDate() + 12 * HOUR /* actually it expires after 24 hours but I'm not sure when does the token refresh. */ + + data.profile?.let { profile -> + Regexes.IDZIENNIK_WEB_LUCKY_NUMBER.find(text)?.also { + val number = it[1].toIntOrNull() ?: return@also + val luckyNumberObject = LuckyNumber( + data.profileId, + Date.getToday(), + number + ) + + data.luckyNumberList.add(luckyNumberObject) + data.metadataList.add( + Metadata( + profile.id, + Metadata.TYPE_LUCKY_NUMBER, + luckyNumberObject.date.value.toLong(), + true, + profile.empty, + System.currentTimeMillis() + )) + } + } + return@run null }?.let { errorCode -> data.error(ApiError(TAG, errorCode)