From 51662289157c0e581a659287fbad103c33d843f7 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Fri, 25 Oct 2019 00:12:23 +0200 Subject: [PATCH] [APIv2/Librus] Parse document in LibrusSynergia instead of endpoints --- .../edziennik/api/v2/Constants.kt | 4 +-- .../api/v2/librus/data/LibrusSynergia.kt | 17 ++++++++--- .../data/synergia/LibrusSynergiaGrades.kt | 19 ------------ .../data/synergia/LibrusSynergiaHomework.kt | 6 ++-- .../data/synergia/LibrusSynergiaInfo.kt | 4 +-- .../data/synergia/LibrusSynergiaTemplate.kt | 4 +-- .../edziennik/api/v2/vulcan/DataVulcan.kt | 30 +++++++++---------- 7 files changed, 34 insertions(+), 50 deletions(-) delete mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaGrades.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt index 576b4912..371e3c58 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt @@ -38,11 +38,11 @@ const val LIBRUS_API_CLIENT_ID_JST = "49" const val LIBRUS_JST_DEMO_CODE = "68656A21" const val LIBRUS_JST_DEMO_PIN = "1290" -const val LIBRUS_SYNERGIA_URL = "https://synergia.librus.pl/" +const val LIBRUS_SYNERGIA_URL = "https://synergia.librus.pl" /** https://synergia.librus.pl/loguj/token/TOKEN/przenies */ const val LIBRUS_SYNERGIA_TOKEN_LOGIN_URL = "https://synergia.librus.pl/loguj/token/TOKEN/przenies" -const val LIBRUS_MESSAGES_URL = "https://wiadomosci.librus.pl/module/" +const val LIBRUS_MESSAGES_URL = "https://wiadomosci.librus.pl/module" const val LIBRUS_SANDBOX_URL = "https://sandbox.librus.pl/index.php?action=" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt index 32e1bc78..e16f00e7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt @@ -8,6 +8,8 @@ import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.TextCallbackHandler import okhttp3.Cookie +import org.jsoup.Jsoup +import org.jsoup.nodes.Document import pl.szczodrzynski.edziennik.api.v2.* import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError @@ -25,8 +27,8 @@ open class LibrusSynergia(open val data: DataLibrus) { get() = data.profile fun synergiaGet(tag: String, endpoint: String, method: Int = GET, - parameters: Map = emptyMap(), onSuccess: (text: String) -> Unit) { - d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL$endpoint") + parameters: Map = emptyMap(), onSuccess: (doc: Document) -> Unit) { + d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL/$endpoint") val callback = object : TextCallbackHandler() { override fun onSuccess(text: String?, response: Response?) { @@ -39,7 +41,8 @@ open class LibrusSynergia(open val data: DataLibrus) { // TODO: Error handling try { - onSuccess(text) + val doc = Jsoup.parse(text) + onSuccess(doc) } catch (e: Exception) { data.error(ApiError(tag, EXCEPTION_LIBRUS_SYNERGIA_REQUEST) .withResponse(response) @@ -47,6 +50,12 @@ open class LibrusSynergia(open val data: DataLibrus) { .withApiResponse(text)) } } + + override fun onFailure(response: Response?, throwable: Throwable?) { + data.error(ApiError(tag, ERROR_REQUEST_FAILURE) + .withResponse(response) + .withThrowable(throwable)) + } } data.app.cookieJar.saveFromResponse(null, listOf( @@ -58,7 +67,7 @@ open class LibrusSynergia(open val data: DataLibrus) { )) Request.builder() - .url("$LIBRUS_SYNERGIA_URL$endpoint") + .url("$LIBRUS_SYNERGIA_URL/$endpoint") .userAgent(LIBRUS_USER_AGENT) .apply { when (method) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaGrades.kt deleted file mode 100644 index d553d127..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaGrades.kt +++ /dev/null @@ -1,19 +0,0 @@ -package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia - -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.api.v2.models.Data -import pl.szczodrzynski.edziennik.data.api.interfaces.ProgressCallback -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile - -class LibrusSynergiaGrades(val app: App, - val profile: Profile, - val loginStore: LoginStore, - val data: Data, - val callback: ProgressCallback, - val onSuccess: () -> Unit) { - - init { - - } -} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt index 5f63bcad..ee77b19a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt @@ -4,7 +4,6 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia -import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.HOUR import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_HOMEWORK import pl.szczodrzynski.edziennik.api.v2.POST @@ -32,8 +31,7 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () -> "dataDo" to profile!!.getSemesterEnd(profile?.currentSemester ?: 2).stringY_m_d, "przedmiot" to -1 - )) { text -> - val doc = Jsoup.parse(text) + )) { doc -> doc.select("table.myHomeworkTable > tbody").firstOrNull()?.also { homeworkTable -> val homeworkElements = homeworkTable.children() @@ -93,7 +91,7 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () -> } // because this requires a synergia login (2 more requests) sync this every two hours or if explicit :D - data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK, 2*HOUR, DRAWER_ITEM_HOMEWORK) + data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK, 2 * HOUR, DRAWER_ITEM_HOMEWORK) onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt index 7777cf6a..cb638523 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt @@ -4,7 +4,6 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia -import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.MONTH import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_SYNERGIA_INFO @@ -16,8 +15,7 @@ class LibrusSynergiaInfo(override val data: DataLibrus, val onSuccess: () -> Uni } init { - synergiaGet(TAG, "informacja") { text -> - val doc = Jsoup.parse(text) + synergiaGet(TAG, "informacja") { doc -> doc.select("table.form tbody").firstOrNull()?.children()?.also { info -> val studentNumber = info[2].select("td").text().trim().toIntOrNull() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt index 1cc0097e..0aa850b6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt @@ -4,7 +4,6 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia -import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusSynergia import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS @@ -15,8 +14,7 @@ class LibrusSynergiaTemplate(override val data: DataLibrus, val onSuccess: () -> } init { - /* synergiaGet(TAG, "") { text -> - val doc = Jsoup.parse(text) + /* synergiaGet(TAG, "") { doc -> data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_, SYNC_ALWAYS) onSuccess() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt index 6e647a87..9b79713c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt @@ -158,26 +158,26 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app val apiUrl: String? get() { val url = when (apiToken?.substring(0, 3)) { - "3S1" -> "https://lekcjaplus.vulcan.net.pl/" - "TA1" -> "https://uonetplus-komunikacja.umt.tarnow.pl/" - "OP1" -> "https://uonetplus-komunikacja.eszkola.opolskie.pl/" - "RZ1" -> "https://uonetplus-komunikacja.resman.pl/" - "GD1" -> "https://uonetplus-komunikacja.edu.gdansk.pl/" - "KA1" -> "https://uonetplus-komunikacja.mcuw.katowice.eu/" - "KA2" -> "https://uonetplus-komunikacja-test.mcuw.katowice.eu/" - "P03" -> "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl/" - "P01" -> "http://efeb-komunikacja.pro-hudson.win.vulcan.pl/" - "P02" -> "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl/" - "P90" -> "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl/" - "FK1", "FS1" -> "http://api.fakelog.cf/" - "SZ9" -> "http://vulcan.szkolny.eu/" + "3S1" -> "https://lekcjaplus.vulcan.net.pl" + "TA1" -> "https://uonetplus-komunikacja.umt.tarnow.pl" + "OP1" -> "https://uonetplus-komunikacja.eszkola.opolskie.pl" + "RZ1" -> "https://uonetplus-komunikacja.resman.pl" + "GD1" -> "https://uonetplus-komunikacja.edu.gdansk.pl" + "KA1" -> "https://uonetplus-komunikacja.mcuw.katowice.eu" + "KA2" -> "https://uonetplus-komunikacja-test.mcuw.katowice.eu" + "P03" -> "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl" + "P01" -> "http://efeb-komunikacja.pro-hudson.win.vulcan.pl" + "P02" -> "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl" + "P90" -> "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl" + "FK1", "FS1" -> "http://api.fakelog.cf" + "SZ9" -> "http://vulcan.szkolny.eu" else -> null } - return if (url != null) "$url$symbol/" else null + return if (url != null) "$url/$symbol" else null } val fullApiUrl: String? get() { - return "${apiUrl}${schoolSymbol}/" + return "${apiUrl}/${schoolSymbol}/" } }