From 39ff47e866833df7773eb5fe10d76d574179cba7 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 25 Dec 2019 22:19:02 +0100 Subject: [PATCH] [API/Edudziennik] Fix downloading timetable out of the school year. --- .../edziennik/data/api/Errors.kt | 1 + .../api/edziennik/edudziennik/Edudziennik.kt | 8 +- .../edudziennik/data/EdudziennikWeb.kt | 1 + .../data/web/EdudziennikWebTimetable.kt | 127 +++++++++--------- app/src/main/res/values/errors.xml | 4 + 5 files changed, 76 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt index 8ad116fd..24d93481 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt @@ -163,6 +163,7 @@ const val ERROR_LOGIN_EDUDZIENNIK_WEB_OTHER = 510 const val ERROR_LOGIN_EDUDZIENNIK_WEB_NO_SESSION_ID = 511 const val ERROR_EDUDZIENNIK_WEB_TIMETABLE_NOT_PUBLIC = 520 const val ERROR_EDUDZIENNIK_WEB_LIMITED_ACCESS = 521 +const val ERROR_EDUDZIENNIK_WEB_SESSION_EXPIRED = 522 const val ERROR_EDUDZIENNIK_WEB_TEAM_MISSING = 530 const val ERROR_TEMPLATE_WEB_OTHER = 801 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt index 3317b348..44b8f7c0 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt @@ -6,16 +6,13 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik import com.google.gson.JsonObject import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.data.api.ERROR_EDUDZIENNIK_WEB_TIMETABLE_NOT_PUBLIC -import pl.szczodrzynski.edziennik.data.api.ERROR_LOGIN_EDUDZIENNIK_WEB_NO_SESSION_ID -import pl.szczodrzynski.edziennik.data.api.edudziennikLoginMethods +import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikData import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.firstlogin.EdudziennikFirstLogin import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.login.EdudziennikLogin import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.models.ApiError -import pl.szczodrzynski.edziennik.data.api.prepare import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -131,6 +128,9 @@ class Edudziennik(val app: App, val profile: Profile?, val loginStore: LoginStor } internalErrorList.add(apiError.errorCode) when (apiError.errorCode) { + ERROR_EDUDZIENNIK_WEB_SESSION_EXPIRED -> { + login() + } ERROR_LOGIN_EDUDZIENNIK_WEB_NO_SESSION_ID -> { login() } 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 ba6e92c7..2f36a4a8 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 @@ -53,6 +53,7 @@ open class EdudziennikWeb(open val data: DataEdudziennik) { override fun onFailure(response: Response?, throwable: Throwable?) { val error = when (response?.code()) { 402 -> ERROR_EDUDZIENNIK_WEB_LIMITED_ACCESS + 403 -> ERROR_EDUDZIENNIK_WEB_SESSION_EXPIRED else -> ERROR_REQUEST_FAILURE } data.error(ApiError(tag, error) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebTimetable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebTimetable.kt index d0b8984d..cf66c212 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebTimetable.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebTimetable.kt @@ -57,82 +57,87 @@ class EdudziennikWebTimetable(override val data: DataEdudziennik, val table = doc.select("#Schedule tbody").first() if (table.text().trim() == "Brak planu lekcji.") { - data.error(ApiError(TAG, ERROR_EDUDZIENNIK_WEB_TIMETABLE_NOT_PUBLIC) - .withApiResponse(text)) - onSuccess() - return@webGet - } + val today = Date.getToday() + val schoolYearStart = if (today.month >= 9) today.year else today.year - 1 - table.children().forEach { row -> - val rowElements = row.children() - - val lessonNumber = rowElements[0].text().toInt() - - val times = rowElements[1].text().split('-') - val startTime = Time.fromH_m(times[0].trim()) - val endTime = Time.fromH_m(times[1].trim()) - - data.lessonRanges.singleOrNull { - it.lessonNumber == lessonNumber && it.startTime == startTime && it.endTime == endTime - } ?: run { - data.lessonRanges.put(lessonNumber, LessonRange(profileId, lessonNumber, startTime, endTime)) + if (weekStart >= Date(schoolYearStart, 9, 1)) { + data.error(ApiError(TAG, ERROR_EDUDZIENNIK_WEB_TIMETABLE_NOT_PUBLIC) + .withApiResponse(text)) + onSuccess() + return@webGet } + } else { + table.children().forEach { row -> + val rowElements = row.children() - rowElements.subList(2, rowElements.size).forEachIndexed { index, lesson -> - val course = lesson.select(".course").firstOrNull() ?: return@forEachIndexed - val info = course.select("span > span") + val lessonNumber = rowElements[0].text().toInt() - if (info.isEmpty()) return@forEachIndexed + val times = rowElements[1].text().split('-') + val startTime = Time.fromH_m(times[0].trim()) + val endTime = Time.fromH_m(times[1].trim()) - val type = when (course.hasClass("substitute")) { - true -> Lesson.TYPE_CHANGE - else -> Lesson.TYPE_NORMAL + data.lessonRanges.singleOrNull { + it.lessonNumber == lessonNumber && it.startTime == startTime && it.endTime == endTime + } ?: run { + data.lessonRanges.put(lessonNumber, LessonRange(profileId, lessonNumber, startTime, endTime)) } - /* Getting subject */ + rowElements.subList(2, rowElements.size).forEachIndexed { index, lesson -> + val course = lesson.select(".course").firstOrNull() ?: return@forEachIndexed + val info = course.select("span > span") - val subjectElement = info[0].child(0) - val subjectId = EDUDZIENNIK_SUBJECT_ID.find(subjectElement.attr("href"))?.get(1) - ?: return@forEachIndexed - val subjectName = subjectElement.text().trim() - val subject = data.getSubject(subjectId, subjectName) + if (info.isEmpty()) return@forEachIndexed - /* Getting teacher */ + val type = when (course.hasClass("substitute")) { + true -> Lesson.TYPE_CHANGE + else -> Lesson.TYPE_NORMAL + } - val teacherId = if (info.size >= 2) { - val teacherElement = info[1].child(0) - val teacherName = teacherElement.text().trim() - teacherName.splitName()?.let { (teacherLastName, teacherFirstName) -> - data.getTeacher(teacherFirstName, teacherLastName) - }?.id ?: -1 - } else -1 + /* Getting subject */ - val lessonObject = Lesson(profileId, -1).also { - it.type = type - it.date = weekStart.clone().stepForward(0, 0, index) - it.lessonNumber = lessonNumber - it.startTime = startTime - it.endTime = endTime - it.subjectId = subject.id - it.teacherId = teacherId + val subjectElement = info[0].child(0) + val subjectId = EDUDZIENNIK_SUBJECT_ID.find(subjectElement.attr("href"))?.get(1) + ?: return@forEachIndexed + val subjectName = subjectElement.text().trim() + val subject = data.getSubject(subjectId, subjectName) - it.id = it.buildId() - } + /* Getting teacher */ - data.lessonNewList.add(lessonObject) - dataDays.remove(lessonObject.date!!.value) + val teacherId = if (info.size >= 2) { + val teacherElement = info[1].child(0) + val teacherName = teacherElement.text().trim() + teacherName.splitName()?.let { (teacherLastName, teacherFirstName) -> + data.getTeacher(teacherFirstName, teacherLastName) + }?.id ?: -1 + } else -1 - if (type != Lesson.TYPE_NORMAL) { - val seen = profile.empty || lessonObject.date!! < Date.getToday() + val lessonObject = Lesson(profileId, -1).also { + it.type = type + it.date = weekStart.clone().stepForward(0, 0, index) + it.lessonNumber = lessonNumber + it.startTime = startTime + it.endTime = endTime + it.subjectId = subject.id + it.teacherId = teacherId - data.metadataList.add(Metadata( - profileId, - Metadata.TYPE_LESSON_CHANGE, - lessonObject.id, - seen, - seen, - System.currentTimeMillis() - )) + it.id = it.buildId() + } + + data.lessonNewList.add(lessonObject) + dataDays.remove(lessonObject.date!!.value) + + if (type != Lesson.TYPE_NORMAL) { + val seen = profile.empty || lessonObject.date!! < Date.getToday() + + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_LESSON_CHANGE, + lessonObject.id, + seen, + seen, + System.currentTimeMillis() + )) + } } } } diff --git a/app/src/main/res/values/errors.xml b/app/src/main/res/values/errors.xml index 1282aab1..334e0d25 100644 --- a/app/src/main/res/values/errors.xml +++ b/app/src/main/res/values/errors.xml @@ -19,6 +19,7 @@ ERROR_INVALID_LOGIN_MODE ERROR_LOGIN_METHOD_NOT_SATISFIED ERROR_NOT_IMPLEMENTED + ERROR_FILE_DOWNLOAD ERROR_NO_STUDENTS_IN_ACCOUNT @@ -132,6 +133,7 @@ ERROR_LOGIN_EDUDZIENNIK_WEB_NO_SESSION_ID ERROR_EDUDZIENNIK_WEB_TIMETABLE_NOT_PUBLIC ERROR_EDUDZIENNIK_WEB_LIMITED_ACCESS + ERROR_EDUDZIENNIK_WEB_SESSION_EXPIRED ERROR_EDUDZIENNIK_WEB_TEAM_MISSING ERROR_TEMPLATE_WEB_OTHER @@ -168,6 +170,7 @@ Nieprawidłowy sposób logowania Nie można wywołać metody logowania Nie zaimplementowano + Wystąpił błąd podczas pobierania pliku Brak uczniów przypisanych do konta @@ -281,6 +284,7 @@ Brak ID sesji Plan lekcji niedostępny Ograniczony dostęp do dziennika + Sesja wygasła Nie można pobrać informacji o klasie i szkole ERROR_TEMPLATE_WEB_OTHER