2023-04-10 20:54:15 +02:00
|
|
|
package com.denizk0461.studip.data
|
|
|
|
|
|
|
|
|
|
import com.denizk0461.studip.model.StudIPEvent
|
|
|
|
|
import org.jsoup.Jsoup
|
2023-04-25 12:02:25 +02:00
|
|
|
import java.io.IOException
|
2023-04-10 20:54:15 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Parser class used for fetching and collecting scheduled events from a Stud.IP timetable.
|
|
|
|
|
*/
|
2023-04-10 20:54:15 +02:00
|
|
|
class StudIPParser {
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Parse a given HTML string. HTML must be of a Stud.IP timetable.
|
|
|
|
|
*
|
2023-04-25 12:02:25 +02:00
|
|
|
* @param html website content of the Stud.IP timetable
|
|
|
|
|
* @param insert action call to save the contents to persistent storage
|
|
|
|
|
* @throws IOException when an error in fetching or the database transaction occurs
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
2023-04-25 12:02:25 +02:00
|
|
|
@kotlin.jvm.Throws(IOException::class)
|
2023-04-10 20:54:15 +02:00
|
|
|
fun parse(html: String, insert: (events: List<StudIPEvent>) -> Unit) {
|
2023-04-23 11:57:03 +02:00
|
|
|
// Primary key value to uniquely identify entries in the database
|
2023-04-10 20:54:15 +02:00
|
|
|
var id = 0
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Parse the HTML using Jsoup to traverse the document
|
2023-04-10 20:54:15 +02:00
|
|
|
val doc = Jsoup.parse(html)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create the list that all events will be added to temporarily before saving them to
|
|
|
|
|
* persistent storage.
|
|
|
|
|
*/
|
2023-04-10 20:54:15 +02:00
|
|
|
val newEvents = mutableListOf<StudIPEvent>()
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fetch all columns from Monday through Friday to parse and assign the events of each day
|
|
|
|
|
* individually and accordingly.
|
|
|
|
|
* TODO this must be dynamic. As it is, it assumes that the first day is always Monday,
|
|
|
|
|
* which needs not be the case. The timetable can be customised by the user by removing
|
|
|
|
|
* days. Also, Sunday is treated as the first day in Stud.IP FOR SOME REASON, so the app
|
|
|
|
|
* needs to handle this appropriately.
|
|
|
|
|
*/
|
|
|
|
|
val columns = arrayOf(
|
2023-04-10 20:54:15 +02:00
|
|
|
doc.getElementById("calendar_view_1_column_0"),
|
|
|
|
|
doc.getElementById("calendar_view_1_column_1"),
|
|
|
|
|
doc.getElementById("calendar_view_1_column_2"),
|
|
|
|
|
doc.getElementById("calendar_view_1_column_3"),
|
|
|
|
|
doc.getElementById("calendar_view_1_column_4"),
|
|
|
|
|
)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Iterate through each day
|
2023-04-10 20:54:15 +02:00
|
|
|
columns.forEachIndexed { index, element ->
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Iterate through all events scheduled for a given day
|
|
|
|
|
element?.getElementsByClass("schedule_entry")?.forEach { entry ->
|
|
|
|
|
/*
|
|
|
|
|
* Retrieve the event's header. This will contain the event's title as well as the
|
|
|
|
|
* lecturers holding the event.
|
|
|
|
|
*/
|
2023-04-10 20:54:15 +02:00
|
|
|
val entryHeader = entry.getElementsByTag("dt")[0].text()
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Find out the index before which the title ends and after which the lecturer
|
|
|
|
|
* name(s) begin.
|
|
|
|
|
*/
|
2023-04-10 20:54:15 +02:00
|
|
|
val delimiter = entryHeader.lastIndexOf('(')
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Retrieve the title of the event from the header string
|
|
|
|
|
val parsedTitle = entryHeader.substring(0 until delimiter).trim()
|
2023-04-10 20:54:15 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Retrieve the lecturer name(s) from the header string
|
|
|
|
|
val parsedLecturers = entryHeader.substring(delimiter + 1 until entryHeader.length - 1).trim()
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Retrieve further event information. This will contain both the time slot the
|
|
|
|
|
* event is assigned to (index 0), as well as the room the event will primarily take
|
|
|
|
|
* place in (index 1).
|
|
|
|
|
*/
|
|
|
|
|
val entryInfo = entry.getElementsByTag("dd")[0].text().split(", ", limit = 2)
|
|
|
|
|
|
|
|
|
|
// Retrieve the time slot and split it into start and end time stamps
|
|
|
|
|
val timeSlot = entryInfo[0].split(" - ")
|
|
|
|
|
|
2023-04-24 15:43:42 +02:00
|
|
|
// Time the event starts at
|
|
|
|
|
val eventStart = timeSlot[0]
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Construct the newly scraped Stud.IP event
|
2023-04-10 20:54:15 +02:00
|
|
|
val event = StudIPEvent(
|
2023-04-23 11:57:03 +02:00
|
|
|
id = id,
|
|
|
|
|
title = parsedTitle,
|
|
|
|
|
lecturer = parsedLecturers,
|
|
|
|
|
room = entryInfo[1],
|
|
|
|
|
day = index,
|
2023-04-24 15:43:42 +02:00
|
|
|
timeslotStart = eventStart,
|
2023-04-23 11:57:03 +02:00
|
|
|
timeslotEnd = timeSlot[1],
|
2023-04-24 15:43:42 +02:00
|
|
|
timeslotId = eventStart.parseToMinutes(),
|
2023-04-10 20:54:15 +02:00
|
|
|
)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Add the new element to the temporary list
|
2023-04-10 20:54:15 +02:00
|
|
|
newEvents.add(event)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Raise the primary key value by 1 to avoid not fulfilling the unique constraint
|
2023-04-10 20:54:15 +02:00
|
|
|
id += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// After fetching has finished, save the list of new items into persistent storage
|
2023-04-10 20:54:15 +02:00
|
|
|
insert(newEvents)
|
|
|
|
|
}
|
|
|
|
|
}
|