Add attendance

This commit is contained in:
Mikołaj Pich 2018-08-20 20:31:27 +02:00
parent 57a5588fb7
commit acf55de975
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
6 changed files with 107 additions and 13 deletions

View file

@ -1,3 +1,3 @@
package io.github.wulkanowy.api
const val DATE_FORMAT = "dd.MM.yyyy"
const val DATE_FORMAT = "dd.MM.yyyy"

View file

@ -0,0 +1,28 @@
package io.github.wulkanowy.api.attendance
import pl.droidsonroids.jspoon.annotation.Selector
import java.util.*
class Attendance {
var number: Int = 0
lateinit var date: Date
@Selector("span")
lateinit var subject: String
@Selector("div", attr = "class")
lateinit var type: String
object Types {
const val NOT_EXIST = "x-sp-nieobecny-w-oddziale"
const val PRESENCE = "x-obecnosc"
const val ABSENCE_UNEXCUSED = "x-nieobecnosc-nieuspr"
const val ABSENCE_EXCUSED = "x-nieobecnosc-uspr"
const val ABSENCE_FOR_SCHOOL_REASONS = "x-nieobecnosc-przycz-szkol"
const val UNEXCUSED_LATENESS = "x-sp-nieusprawiedliwione"
const val EXCUSED_LATENESS = "x-sp-spr"
const val EXEMPTION = "x-sp-zwolnienie"
}
}

View file

@ -0,0 +1,25 @@
package io.github.wulkanowy.api.attendance
import io.github.wulkanowy.api.DATE_FORMAT
import pl.droidsonroids.jspoon.annotation.Format
import pl.droidsonroids.jspoon.annotation.Selector
import java.util.*
class AttendanceResponse {
@Selector(".presentData thead th:not(:first-of-type)", regex = "\\s(.*)")
@Format(value = DATE_FORMAT)
lateinit var days: List<Date>
@Selector(".presentData tbody tr")
lateinit var rows: List<AttendanceRow>
class AttendanceRow {
@Selector("td", index = 0)
var number: Int = 0
@Selector("td:not(:first-of-type)")
lateinit var lessons: List<Attendance>
}
}

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.api.interfaces
import io.github.wulkanowy.api.attendance.AttendanceResponse
import io.github.wulkanowy.api.grades.GradesResponse
import io.github.wulkanowy.api.notes.NotesResponse
import io.reactivex.Observable
@ -9,7 +10,7 @@ import retrofit2.http.Query
interface StudentAndParentApi {
@GET("Frekwencja.mvc")
fun getAttendance(@Query("data") date: String)
fun getAttendance(@Query("data") date: String): Observable<AttendanceResponse>
@GET("Sprawdziany.mvc/Terminarz?rodzajWidoku=2")
fun getExams(@Query("date") date: String)

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.api.repository
import io.github.wulkanowy.api.attendance.Attendance
import io.github.wulkanowy.api.grades.Grade
import io.github.wulkanowy.api.interfaces.StudentAndParentApi
import io.github.wulkanowy.api.notes.Note
@ -40,7 +41,17 @@ class StudentAndParentRepository(private val host: String,
}
}
fun getAttendance(startDate: String) = api.getAttendance(startDate)
fun getAttendance(startDate: String): Observable<List<Attendance>> {
return api.getAttendance(startDate).map { res ->
res.rows.map { row ->
row.lessons.mapIndexed { i, it ->
it.date = res.days[i]
it.number = row.number
it
}
}.flatten()
}
}
fun getHomework(startDate: String) = api.getHomework(startDate)

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.api
import io.github.wulkanowy.api.attendance.Attendance
import io.github.wulkanowy.api.grades.Grade
import io.github.wulkanowy.api.interceptor.LoginInterceptor
import io.github.wulkanowy.api.notes.Note
@ -47,13 +48,37 @@ class VulcanTest {
)
}
@Test fun attendanceTest() {
val attendance = snp.getAttendance("636697786721570000")
val attendanceObserver = TestObserver<List<Attendance>>()
attendance.subscribe(attendanceObserver)
attendanceObserver.assertComplete()
val values = attendanceObserver.values()[0]
assertEquals(0, values[0].number)
assertEquals("Fizyka", values[0].subject)
assertEquals(getDate(2018, 8, 13), values[0].date)
assertEquals(Attendance.Types.PRESENCE, values[0].type)
assertEquals(Attendance.Types.ABSENCE_UNEXCUSED, values[1].type)
assertEquals(Attendance.Types.ABSENCE_EXCUSED, values[2].type)
assertEquals(Attendance.Types.ABSENCE_FOR_SCHOOL_REASONS, values[3].type)
assertEquals(Attendance.Types.UNEXCUSED_LATENESS, values[4].type)
assertEquals(Attendance.Types.EXCUSED_LATENESS, values[5].type)
assertEquals(Attendance.Types.EXEMPTION, values[6].type)
assertEquals(1, values[5].number)
}
@Test fun notesTest() {
val notes = snp.getNotes()
val notesSubscriber = TestObserver<List<Note>>()
notes.subscribe(notesSubscriber)
notesSubscriber.assertComplete()
val notesObserver = TestObserver<List<Note>>()
notes.subscribe(notesObserver)
notesObserver.assertComplete()
val values = notesSubscriber.values()[0]
val values = notesObserver.values()[0]
assertEquals("Janusz Tracz", values[0].teacher)
assertEquals("Udział w konkursie szkolnym +20 pkt", values[0].category)
@ -62,11 +87,11 @@ class VulcanTest {
@Test fun gradesTest() {
val grades = snp.getGrades(123)
val gradesSubscriber = TestObserver<List<Grade>>()
grades.subscribe(gradesSubscriber)
gradesSubscriber.assertComplete()
val gradesObserver = TestObserver<List<Grade>>()
grades.subscribe(gradesObserver)
gradesObserver.assertComplete()
val values = gradesSubscriber.values()[0]
val values = gradesObserver.values()[0]
assertEquals("Zajęcia z wychowawcą", values[0].subject)
assertEquals("5", values[0].value)
@ -74,12 +99,16 @@ class VulcanTest {
assertEquals("A1", values[0].symbol)
assertEquals("Dzień Kobiet w naszej klasie", values[0].description)
assertEquals("1.00", values[0].weight)
assertEquals(Date.from(LocalDate.of(2017, 3, 21)
.atStartOfDay(ZoneId.systemDefault()).toInstant()), values[0].date)
assertEquals(getDate(2017, 3, 21), values[0].date)
assertEquals("Patryk Maciejewski", values[0].teacher)
assertEquals("STR", values[4].symbol)
assertEquals("", values[4].description)
}
private fun getDate(year: Int, month: Int, day: Int): Date {
return Date.from(LocalDate.of(year, month, day)
.atStartOfDay(ZoneId.systemDefault()).toInstant())
}
}