[APIv2/Mobidziennik] Add Messages All+Inbox

This commit is contained in:
Kuba Szczodrzyński 2019-10-11 23:00:19 +02:00
parent 93fcc0deb7
commit 7594fdd578
4 changed files with 183 additions and 3 deletions

View File

@ -47,11 +47,14 @@ val MobidziennikFeatures = listOf(
Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_ATTENDANCE, listOf(
ENDPOINT_MOBIDZIENNIK_WEB_ATTENDANCE to LOGIN_METHOD_MOBIDZIENNIK_WEB
), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)),
// messages
Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_MESSAGES_INBOX, listOf(
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX to LOGIN_METHOD_MOBIDZIENNIK_WEB
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX to LOGIN_METHOD_MOBIDZIENNIK_WEB,
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL to LOGIN_METHOD_MOBIDZIENNIK_WEB
), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)),
Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_MESSAGES_SENT, listOf(
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT to LOGIN_METHOD_MOBIDZIENNIK_WEB,
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL to LOGIN_METHOD_MOBIDZIENNIK_WEB
), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)),

View File

@ -9,6 +9,8 @@ import pl.szczodrzynski.edziennik.api.v2.mobidziennik.*
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.api.MobidziennikApi
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebCalendar
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebGrades
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebMessagesAll
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebMessagesInbox
import pl.szczodrzynski.edziennik.utils.Utils
class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) {
@ -40,7 +42,7 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) {
ENDPOINT_MOBIDZIENNIK_API_MAIN -> {
data.startProgress(R.string.edziennik_progress_endpoint_data)
MobidziennikApi(data) { onSuccess() }
}/*
}
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX -> {
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
MobidziennikWebMessagesInbox(data) { onSuccess() }
@ -48,7 +50,7 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) {
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL -> {
data.startProgress(R.string.edziennik_progress_endpoint_messages)
MobidziennikWebMessagesAll(data) { onSuccess() }
}*/
}
ENDPOINT_MOBIDZIENNIK_WEB_CALENDAR -> {
data.startProgress(R.string.edziennik_progress_endpoint_calendar)
MobidziennikWebCalendar(data) { onSuccess() }

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-11.
*/
package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web
import org.jsoup.Jsoup
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.singleOrNull
import pl.szczodrzynski.edziennik.utils.models.Date
class MobidziennikWebMessagesAll(override val data: DataMobidziennik,
val onSuccess: () -> Unit) : MobidziennikWeb(data) {
companion object {
private const val TAG = "MobidziennikWebMessagesAll"
}
init {
webGet(TAG, "/dziennik/wyszukiwarkawiadomosci?q=+") { text ->
MobidziennikLuckyNumberExtractor(data, text)
val doc = Jsoup.parse(text)
val listElement = doc.getElementsByClass("spis").first()
if (listElement == null) {
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL, SYNC_ALWAYS)
onSuccess()
return@webGet
}
val list = listElement.getElementsByClass("podswietl")
for (item in list) {
val id = item.attr("rel").replace("[^\\d]".toRegex(), "").toLongOrNull() ?: continue
val subjectEl = item.select("td:eq(0) div").first()
val subject = subjectEl.text()
val addedDateEl = item.select("td:eq(1)").first()
val addedDate = Date.fromIsoHm(addedDateEl.text())
val typeEl = item.select("td:eq(2) img").first()
var type = TYPE_RECEIVED
if (typeEl.outerHtml().contains("mail_send.png"))
type = TYPE_SENT
val senderEl = item.select("td:eq(3) div").first()
var senderId: Long = -1
if (type == TYPE_RECEIVED) {
// search sender teacher
val senderName = senderEl.text()
senderId = data.teacherList.singleOrNull { it.fullNameLastFirst == senderName }?.id ?: -1
data.messageRecipientList.add(MessageRecipient(profileId, -1, id))
} else {
// TYPE_SENT, so multiple recipients possible
val recipientNames = senderEl.text().split(", ")
for (recipientName in recipientNames) {
val recipientId = data.teacherList.singleOrNull { it.fullNameLastFirst == recipientName }?.id ?: -1
data.messageRecipientIgnoreList.add(MessageRecipient(profileId, recipientId, id))
}
}
val message = Message(
profileId,
id,
subject,
null,
type,
senderId,
-1
)
data.messageList.add(message)
data.metadataList.add(Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, true, true, addedDate))
}
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL, SYNC_ALWAYS)
onSuccess()
}
}
}

View File

@ -0,0 +1,86 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-11.
*/
package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web
import org.jsoup.Jsoup
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.singleOrNull
import pl.szczodrzynski.edziennik.utils.models.Date
class MobidziennikWebMessagesInbox(override val data: DataMobidziennik,
val onSuccess: () -> Unit) : MobidziennikWeb(data) {
companion object {
private const val TAG = "MobidziennikWebMessagesInbox"
}
init {
webGet(TAG, "/dziennik/wiadomosci") { text ->
MobidziennikLuckyNumberExtractor(data, text)
if (text.contains("Brak wiadomości odebranych.")) {
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX, SYNC_ALWAYS)
onSuccess()
return@webGet
}
val doc = Jsoup.parse(text)
val list = doc.getElementsByClass("spis").first().getElementsByClass("podswietl")
for (item in list) {
val id = item.attr("rel").toLongOrNull() ?: continue
val subjectEl = item.select("td:eq(0)").first()
var hasAttachments = false
if (subjectEl.getElementsByTag("a").size != 0) {
hasAttachments = true
}
val subject = subjectEl.ownText()
val addedDateEl = item.select("td:eq(1) small").first()
val addedDate = Date.fromIsoHm(addedDateEl.text())
val senderEl = item.select("td:eq(2)").first()
val senderName = senderEl.ownText()
val senderId = data.teacherList.singleOrNull { it.fullNameLastFirst == senderName }?.id ?: -1
data.messageRecipientIgnoreList.add(MessageRecipient(profileId, -1, id))
val isRead = item.select("td:eq(3) span").first().hasClass("wiadomosc_przeczytana")
val message = Message(
profileId,
id,
subject,
null,
Message.TYPE_RECEIVED,
senderId,
-1
)
if (hasAttachments)
message.setHasAttachments()
data.messageList.add(message)
data.messageMetadataList.add(
Metadata(
profileId,
Metadata.TYPE_MESSAGE,
message.id,
isRead,
isRead || profile?.empty ?: false,
addedDate
))
}
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX, SYNC_ALWAYS)
onSuccess()
}
}
}