io.github.wulkanowy.sdk.scrapper.repository.StudentRepository.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-scrapper Show documentation
Show all versions of sdk-scrapper Show documentation
Unified way of retrieving data from the UONET+ register through mobile api and scraping api
package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.attendance.Absent
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance
import io.github.wulkanowy.sdk.scrapper.attendance.AttendanceExcuseRequest
import io.github.wulkanowy.sdk.scrapper.attendance.AttendanceRequest
import io.github.wulkanowy.sdk.scrapper.attendance.AttendanceSummary
import io.github.wulkanowy.sdk.scrapper.attendance.AttendanceSummaryRequest
import io.github.wulkanowy.sdk.scrapper.attendance.Subject
import io.github.wulkanowy.sdk.scrapper.attendance.mapAttendanceList
import io.github.wulkanowy.sdk.scrapper.attendance.mapAttendanceSummaryList
import io.github.wulkanowy.sdk.scrapper.conferences.Conference
import io.github.wulkanowy.sdk.scrapper.conferences.mapConferences
import io.github.wulkanowy.sdk.scrapper.exams.Exam
import io.github.wulkanowy.sdk.scrapper.exams.ExamRequest
import io.github.wulkanowy.sdk.scrapper.exams.mapExamsList
import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException
import io.github.wulkanowy.sdk.scrapper.getSchoolYear
import io.github.wulkanowy.sdk.scrapper.grades.GradePointsSummary
import io.github.wulkanowy.sdk.scrapper.grades.GradeRequest
import io.github.wulkanowy.sdk.scrapper.grades.Grades
import io.github.wulkanowy.sdk.scrapper.grades.GradesStatisticsPartial
import io.github.wulkanowy.sdk.scrapper.grades.GradesStatisticsRequest
import io.github.wulkanowy.sdk.scrapper.grades.GradesStatisticsSemester
import io.github.wulkanowy.sdk.scrapper.grades.mapGradesList
import io.github.wulkanowy.sdk.scrapper.grades.mapGradesStatisticsPartial
import io.github.wulkanowy.sdk.scrapper.grades.mapGradesStatisticsSemester
import io.github.wulkanowy.sdk.scrapper.grades.mapGradesSummary
import io.github.wulkanowy.sdk.scrapper.homework.Homework
import io.github.wulkanowy.sdk.scrapper.homework.HomeworkRequest
import io.github.wulkanowy.sdk.scrapper.homework.mapHomework
import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors
import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import io.github.wulkanowy.sdk.scrapper.menu.Menu
import io.github.wulkanowy.sdk.scrapper.menu.MenuRequest
import io.github.wulkanowy.sdk.scrapper.mobile.Device
import io.github.wulkanowy.sdk.scrapper.mobile.TokenResponse
import io.github.wulkanowy.sdk.scrapper.mobile.UnregisterDeviceRequest
import io.github.wulkanowy.sdk.scrapper.notes.Note
import io.github.wulkanowy.sdk.scrapper.register.AuthorizePermission
import io.github.wulkanowy.sdk.scrapper.register.AuthorizePermissionRequest
import io.github.wulkanowy.sdk.scrapper.school.School
import io.github.wulkanowy.sdk.scrapper.school.Teacher
import io.github.wulkanowy.sdk.scrapper.school.mapToSchool
import io.github.wulkanowy.sdk.scrapper.school.mapToTeachers
import io.github.wulkanowy.sdk.scrapper.service.StudentService
import io.github.wulkanowy.sdk.scrapper.student.StudentInfo
import io.github.wulkanowy.sdk.scrapper.student.StudentPhoto
import io.github.wulkanowy.sdk.scrapper.timetable.CacheResponse
import io.github.wulkanowy.sdk.scrapper.timetable.CompletedLesson
import io.github.wulkanowy.sdk.scrapper.timetable.CompletedLessonsRequest
import io.github.wulkanowy.sdk.scrapper.timetable.Timetable
import io.github.wulkanowy.sdk.scrapper.timetable.TimetableRequest
import io.github.wulkanowy.sdk.scrapper.timetable.mapCompletedLessonsList
import io.github.wulkanowy.sdk.scrapper.timetable.mapTimetableAdditional
import io.github.wulkanowy.sdk.scrapper.timetable.mapTimetableHeaders
import io.github.wulkanowy.sdk.scrapper.timetable.mapTimetableList
import io.github.wulkanowy.sdk.scrapper.toFormat
import org.jsoup.Jsoup
import org.slf4j.LoggerFactory
import pl.droidsonroids.jspoon.Jspoon
import java.io.IOException
import java.time.LocalDate
internal class StudentRepository(
private val api: StudentService,
private val urlGenerator: UrlGenerator,
) {
private val certificateAdapter by lazy {
Jspoon.create().adapter(CertificateResponse::class.java)
}
companion object {
@JvmStatic
private val logger = LoggerFactory.getLogger(this::class.java)
}
private fun LocalDate.toISOFormat(): String = toFormat("yyyy-MM-dd'T00:00:00'")
private suspend fun getCache(): CacheResponse {
loginModule()
return api.getUserCache().handleErrors().let {
requireNotNull(it.data)
}
}
suspend fun authorizePermission(pesel: String): Boolean {
return api.authorizePermission(
body = AuthorizePermissionRequest(AuthorizePermission(pesel)),
).handleErrors().data?.success ?: false
}
suspend fun getAttendance(startDate: LocalDate, endDate: LocalDate?): List {
val lessonTimes = getCache().times
return api.getAttendance(AttendanceRequest(startDate.atStartOfDay()))
.handleErrors()
.data?.mapAttendanceList(startDate, endDate, lessonTimes).orEmpty()
}
suspend fun getAttendanceSummary(subjectId: Int?): List {
return api.getAttendanceStatistics(AttendanceSummaryRequest(subjectId))
.handleErrors()
.data?.mapAttendanceSummaryList().orEmpty()
}
suspend fun excuseForAbsence(absents: List, content: String?): Boolean {
loginModule()
return api.excuseForAbsence(
attendanceExcuseRequest = AttendanceExcuseRequest(
AttendanceExcuseRequest.Excuse(
absents = absents.map { absence ->
AttendanceExcuseRequest.Excuse.Absent(
date = absence.date.toFormat("yyyy-MM-dd'T'HH:mm:ss"),
timeId = absence.timeId,
)
},
content = content,
),
),
).handleErrors().success
}
suspend fun getSubjects(): List {
return api.getAttendanceSubjects().handleErrors().data.orEmpty()
}
suspend fun getExams(startDate: LocalDate, endDate: LocalDate? = null): List {
return api.getExams(ExamRequest(startDate.atStartOfDay(), startDate.getSchoolYear()))
.handleErrors()
.data.orEmpty().mapExamsList(startDate, endDate)
}
suspend fun getGrades(semesterId: Int): Grades {
val data = api.getGrades(GradeRequest(semesterId)).handleErrors().data
return Grades(
details = data?.mapGradesList().orEmpty(),
summary = data?.mapGradesSummary().orEmpty(),
descriptive = data?.gradesDescriptive.orEmpty(),
isAverage = data?.isAverage ?: false,
isPoints = data?.isPoints ?: false,
isForAdults = data?.isForAdults ?: false,
type = data?.type ?: -1,
)
}
suspend fun getGradesPartialStatistics(semesterId: Int): List {
return api.getGradesPartialStatistics(GradesStatisticsRequest(semesterId))
.handleErrors()
.data.orEmpty().mapGradesStatisticsPartial()
}
suspend fun getGradesPointsStatistics(semesterId: Int): List {
return api.getGradesPointsStatistics(GradesStatisticsRequest(semesterId))
.handleErrors()
.data?.items.orEmpty()
}
suspend fun getGradesAnnualStatistics(semesterId: Int): List {
return api.getGradesAnnualStatistics(GradesStatisticsRequest(semesterId))
.handleErrors()
.data.orEmpty().mapGradesStatisticsSemester()
}
suspend fun getHomework(startDate: LocalDate, endDate: LocalDate? = null): List {
return api.getHomework(HomeworkRequest(startDate.atStartOfDay(), startDate.getSchoolYear(), -1))
.handleErrors()
.data.orEmpty().mapHomework(startDate, endDate)
}
suspend fun getNotes(): List {
return api.getNotes().handleErrors().data?.notes.orEmpty().map {
it.copy(
teacher = it.teacher.split(" [").first(),
).apply {
teacherSymbol = it.teacher.split(" [").last().removeSuffix("]")
}
}.sortedWith(compareBy({ it.date }, { it.category }))
}
suspend fun getConferences(): List {
return api.getConferences()
.handleErrors().data.orEmpty()
.mapConferences()
}
suspend fun getMenu(date: LocalDate): List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy