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 d4567e4d..62a09881 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 @@ -86,6 +86,6 @@ object Regexes { val EDUDZIENNIK_STUDENT_ID by lazy { - """""".toRegex() + """/Students/(\w+?)/""".toRegex() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/DataEdudziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/DataEdudziennik.kt index 894a0c79..01da3a35 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/DataEdudziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/DataEdudziennik.kt @@ -48,10 +48,10 @@ class DataEdudziennik(app: App, profile: Profile?, loginStore: LoginStore) : Dat get() { mSchoolId = mSchoolId ?: profile?.getStudentData("schoolId", null); return mSchoolId } set(value) { profile?.putStudentData("schoolId", value) ?: return; mSchoolId = value } - private var mCourseId: String? = null - var courseId: String? - get() { mCourseId = mCourseId ?: profile?.getStudentData("courseId", null); return mCourseId } - set(value) { profile?.putStudentData("courseId", value) ?: return; mCourseId = value } + private var mClassId: String? = null + var classId: String? + get() { mClassId = mClassId ?: profile?.getStudentData("classId", null); return mClassId } + set(value) { profile?.putStudentData("classId", value) ?: return; mClassId = value } /* __ __ _ \ \ / / | | @@ -75,6 +75,12 @@ class DataEdudziennik(app: App, profile: Profile?, loginStore: LoginStore) : Dat val schoolEndpoint: String get() = "Schools/$schoolId/" + val schoolClassEndpoint: String + get() = "Schools/$classId/" + + val studentAndClassEndpoint: String + get() = "Students/$studentId/Klass/$classId/" + val courseEndpoint: String - get() = "Schools/$courseId/" + get() = "Course/$studentId/" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/EdudziennikFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/EdudziennikFeatures.kt index 92eb2936..6cffe7d0 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/EdudziennikFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/EdudziennikFeatures.kt @@ -4,21 +4,21 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik -import pl.szczodrzynski.edziennik.data.api.* +import pl.szczodrzynski.edziennik.data.api.FEATURE_LUCKY_NUMBER +import pl.szczodrzynski.edziennik.data.api.FEATURE_STUDENT_INFO +import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_EDUDZIENNIK_WEB +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_EDUDZIENNIK import pl.szczodrzynski.edziennik.data.api.models.Feature -const val ENDPOINT_TEMPLATE_WEB_SAMPLE = 9991 -const val ENDPOINT_TEMPLATE_WEB_SAMPLE_2 = 9992 -const val ENDPOINT_TEMPLATE_API_SAMPLE = 9993 +const val ENDPOINT_EDUDZIENNIK_WEB_START = 1000 +const val ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER = 1010 val EdudziennikFeatures = listOf( - Feature(LOGIN_TYPE_TEMPLATE, FEATURE_STUDENT_INFO, listOf( - ENDPOINT_TEMPLATE_WEB_SAMPLE to LOGIN_METHOD_TEMPLATE_WEB - ), listOf(LOGIN_METHOD_TEMPLATE_WEB)), - Feature(LOGIN_TYPE_TEMPLATE, FEATURE_SCHOOL_INFO, listOf( - ENDPOINT_TEMPLATE_WEB_SAMPLE_2 to LOGIN_METHOD_TEMPLATE_WEB - ), listOf(LOGIN_METHOD_TEMPLATE_WEB)), - Feature(LOGIN_TYPE_TEMPLATE, FEATURE_GRADES, listOf( - ENDPOINT_TEMPLATE_API_SAMPLE to LOGIN_METHOD_TEMPLATE_API - ), listOf(LOGIN_METHOD_TEMPLATE_API)) + Feature(LOGIN_TYPE_EDUDZIENNIK, FEATURE_STUDENT_INFO, listOf( + ENDPOINT_EDUDZIENNIK_WEB_START to LOGIN_METHOD_EDUDZIENNIK_WEB + ), listOf(LOGIN_METHOD_EDUDZIENNIK_WEB)), + + Feature(LOGIN_TYPE_EDUDZIENNIK, FEATURE_LUCKY_NUMBER, listOf( + ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER to LOGIN_METHOD_EDUDZIENNIK_WEB + ), listOf(LOGIN_METHOD_EDUDZIENNIK_WEB)) ) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt index 30b562c1..653b9e55 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt @@ -4,7 +4,12 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data +import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_START +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web.EdudziennikWebLuckyNumber +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web.EdudziennikWebStart import pl.szczodrzynski.edziennik.utils.Utils class EdudziennikData(val data: DataEdudziennik, val onSuccess: () -> Unit) { @@ -34,7 +39,14 @@ class EdudziennikData(val data: DataEdudziennik, val onSuccess: () -> Unit) { private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) { Utils.d(TAG, "Using endpoint $endpointId") when (endpointId) { - // TODO + ENDPOINT_EDUDZIENNIK_WEB_START -> { + data.startProgress(R.string.edziennik_progress_endpoint_data) + EdudziennikWebStart(data, onSuccess) + } + ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER -> { + data.startProgress(R.string.edziennik_progress_endpoint_lucky_number) + EdudziennikWebLuckyNumber(data, onSuccess) + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikWeb.kt index b783bc5b..2538106c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikWeb.kt @@ -26,7 +26,7 @@ open class EdudziennikWeb(open val data: DataEdudziennik) { val profile get() = data.profile - fun webGet(tag: String, endpoint: String, onSuccess: (text: String) -> Unit) { + fun webGet(tag: String, endpoint: String, xhr: Boolean = false, onSuccess: (text: String) -> Unit) { val url = "https://dziennikel.appspot.com/" + when (endpoint.endsWith('/') || endpoint.isEmpty()) { true -> endpoint else -> "$endpoint/" @@ -68,6 +68,9 @@ open class EdudziennikWeb(open val data: DataEdudziennik) { Request.builder() .url(url) .userAgent(EDUDZIENNIK_USER_AGENT) + .apply { + if (xhr) header("X-Requested-With", "XMLHttpRequest") + } .get() .callback(callback) .build() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebLuckyNumber.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebLuckyNumber.kt new file mode 100644 index 00000000..65eb0c31 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebLuckyNumber.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-23 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikWeb +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date + +class EdudziennikWebLuckyNumber(override val data: DataEdudziennik, + val onSuccess: () -> Unit) : EdudziennikWeb(data) { + companion object { + private const val TAG = "EdudziennikWebLuckyNumber" + } + + init { data.profile?.also { profile -> + webGet(TAG, data.schoolEndpoint + "Lucky", xhr = true) { text -> + val luckyNumber = text.toInt() + + val luckyNumberObject = LuckyNumber( + profileId, + Date.getToday(), + luckyNumber + ) + + data.luckyNumberList.add(luckyNumberObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_LUCKY_NUMBER, + luckyNumberObject.date.value.toLong(), + profile.empty, + profile.empty, + System.currentTimeMillis() + )) + + data.setSyncNext(ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER, SYNC_ALWAYS) + onSuccess() + } + } ?: onSuccess() } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebStart.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebStart.kt new file mode 100644 index 00000000..fb018b6a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebStart.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-23 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_START +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikWeb +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.get + +class EdudziennikWebStart(override val data: DataEdudziennik, + val onSuccess: () -> Unit) : EdudziennikWeb(data) { + companion object { + private const val TAG = "EdudziennikWebStart" + } + + init { data.profile?.also { profile -> + webGet(TAG, data.studentEndpoint + "start") { text -> + val schoolId = """ Unit doc.select("ul ul > li").first().children().forEach { val studentId = EDUDZIENNIK_STUDENT_ID.find(it.attr("href"))?.get(1) + if (studentId.isNullOrBlank()) return@forEach + val studentName = it.text().fixName() val profile = Profile()