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 1d2e634e..8c5c2416 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 @@ -132,6 +132,9 @@ object Regexes { val EDUDZIENNIK_ANNOUNCEMENT_ID by lazy { """/Announcement/([\w-_]+?)/""".toRegex() } + val EDUDZIENNIK_HOMEWORK_ID by lazy { + """/Homework/([\w-_]+?)/""".toRegex() + } val EDUDZIENNIK_SCHOOL_DETAIL_ID by lazy { """ Unit) { data.startProgress(R.string.edziennik_progress_endpoint_announcements) EdudziennikWebAnnouncements(data, onSuccess) } + ENDPOINT_EDUDZIENNIK_WEB_HOMEWORK -> { + data.startProgress(R.string.edziennik_progress_endpoint_homework) + EdudziennikWebHomework(data, onSuccess) + } ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER -> { data.startProgress(R.string.edziennik_progress_endpoint_lucky_number) EdudziennikWebLuckyNumber(data, onSuccess) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt index 98d95c15..c0c215c6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt @@ -27,43 +27,45 @@ class EdudziennikWebAnnouncements(override val data: DataEdudziennik, webGet(TAG, data.schoolClassEndpoint + "Announcements") { text -> val doc = Jsoup.parse(text) - doc.select("table.list tbody tr").forEach { announcementElement -> - val titleElement = announcementElement.child(0).child(0) + if (doc.getElementsByClass("message").text().trim() != "Brak ogłoszeń.") { + doc.select("table.list tbody tr").forEach { announcementElement -> + val titleElement = announcementElement.child(0).child(0) - val longId = EDUDZIENNIK_ANNOUNCEMENT_ID.find(titleElement.attr("href"))?.get(1) - ?: return@forEach - val id = longId.crc32() - val subject = titleElement.text() + val longId = EDUDZIENNIK_ANNOUNCEMENT_ID.find(titleElement.attr("href"))?.get(1) + ?: return@forEach + val id = longId.crc32() + val subject = titleElement.text() - val teacherName = announcementElement.child(1).text() - val teacher = teacherName.splitName()?.let { (teacherFirstName, teacherLastName) -> - data.getTeacher(teacherFirstName, teacherLastName) - } ?: return@forEach + val teacherName = announcementElement.child(1).text() + val teacher = teacherName.splitName()?.let { (teacherFirstName, teacherLastName) -> + data.getTeacher(teacherFirstName, teacherLastName) + } ?: return@forEach - val dateString = announcementElement.getElementsByClass("datetime").first().text() - val startDate = Date.fromY_m_d(dateString) - val addedDate = Date.fromIsoHm(dateString) + val dateString = announcementElement.getElementsByClass("datetime").first().text() + val startDate = Date.fromY_m_d(dateString) + val addedDate = Date.fromIsoHm(dateString) - val announcementObject = Announcement( - profileId, - id, - subject, - null, - startDate, - null, - teacher.id, - longId - ) + val announcementObject = Announcement( + profileId, + id, + subject, + null, + startDate, + null, + teacher.id, + longId + ) - data.announcementIgnoreList.add(announcementObject) - data.metadataList.add(Metadata( - profileId, - Metadata.TYPE_ANNOUNCEMENT, - id, - profile.empty, - profile.empty, - addedDate - )) + data.announcementIgnoreList.add(announcementObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_ANNOUNCEMENT, + id, + profile.empty, + profile.empty, + addedDate + )) + } } data.setSyncNext(ENDPOINT_EDUDZIENNIK_WEB_ANNOUNCEMENTS, SYNC_ALWAYS) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt index ed86c759..40062335 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt @@ -45,7 +45,7 @@ class EdudziennikWebExams(override val data: DataEdudziennik, val date = Date.fromY_m_d(dateString) val lessons = data.app.db.timetableDao().getForDateNow(profileId, date) - val startTime = lessons.firstOrNull { it.subjectId == subject.id }?.startTime + val startTime = lessons.firstOrNull { it.subjectId == subject.id }?.displayStartTime val eventTypeElement = examElement.child(3).child(0) val eventTypeId = EDUDZIENNIK_EVENT_TYPE_ID.find(eventTypeElement.attr("href"))?.get(1) @@ -78,7 +78,7 @@ class EdudziennikWebExams(override val data: DataEdudziennik, )) } - data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_DEFAULT)) + data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) data.setSyncNext(ENDPOINT_EDUDZIENNIK_WEB_EXAMS, SYNC_ALWAYS) onSuccess() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt new file mode 100644 index 00000000..c77606ec --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt @@ -0,0 +1,87 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-29 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +import org.jsoup.Jsoup +import pl.szczodrzynski.edziennik.crc32 +import pl.szczodrzynski.edziennik.data.api.Regexes.EDUDZIENNIK_HOMEWORK_ID +import pl.szczodrzynski.edziennik.data.api.Regexes.EDUDZIENNIK_SUBJECT_ID +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_HOMEWORK +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikWeb +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.events.Event +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.get +import pl.szczodrzynski.edziennik.splitName +import pl.szczodrzynski.edziennik.utils.models.Date + +class EdudziennikWebHomework(override val data: DataEdudziennik, + val onSuccess: () -> Unit) : EdudziennikWeb(data) { + companion object { + const val TAG = "EdudziennikWebHomework" + } + + init { data.profile?.also { profile -> + webGet(TAG, data.courseStudentEndpoint + "Homework", xhr = true) { text -> + val doc = Jsoup.parseBodyFragment("" + text.trim() + "
") + + if (doc.getElementsByClass("message").text().trim() != "Brak prac domowych") { + doc.select("tr").forEach { homeworkElement -> + val dateElement = homeworkElement.getElementsByClass("date").first().child(0) + val id = EDUDZIENNIK_HOMEWORK_ID.find(dateElement.attr("href"))?.get(1)?.crc32() + ?: return@forEach + val date = Date.fromY_m_d(dateElement.text()) + + val subjectElement = homeworkElement.child(1).child(0) + val subjectId = EDUDZIENNIK_SUBJECT_ID.find(subjectElement.attr("href"))?.get(1) + ?: return@forEach + val subjectName = subjectElement.text() + val subject = data.getSubject(subjectId, subjectName) + + val lessons = data.app.db.timetableDao().getForDateNow(profileId, date) + val startTime = lessons.firstOrNull { it.subjectId == subject.id }?.displayStartTime + + val teacherName = homeworkElement.child(2).text() + val teacher = teacherName.splitName()?.let { (teacherFirstName, teacherLastName) -> + data.getTeacher(teacherFirstName, teacherLastName) + } ?: return@forEach + + val topic = homeworkElement.child(4).text() + + val eventObject = Event( + profileId, + id, + date, + startTime, + topic, + -1, + Event.TYPE_HOMEWORK, + false, + teacher.id, + subject.id, + data.teamClass?.id ?: -1 + ) + + data.eventList.add(eventObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_HOMEWORK, + id, + profile.empty, + profile.empty, + System.currentTimeMillis() + )) + } + } + + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_HOMEWORK)) + + data.setSyncNext(ENDPOINT_EDUDZIENNIK_WEB_HOMEWORK, SYNC_ALWAYS) + onSuccess() + } + } ?: onSuccess() } +}