PARSING HTML WORKS AND IT GETS SAVED TO THE DATABASE WOOO

This commit is contained in:
denizk0461
2023-04-10 20:54:15 +02:00
parent 97bc2b1068
commit cab50b5f2d
13 changed files with 161 additions and 58 deletions
@@ -13,4 +13,45 @@ object DummyData {
StudIPEvent(6, "Gesellschaft und Raum", "Lossau, Mossig", "GW2 B1820", 3, 1, 1),
StudIPEvent(7, "Gesellschaft und Raum2", "Lossau, Mossig", "GW2 B1820", 4, 4, 2),
)
val timeslotStartMap: Map<String, Int> = mapOf(
"6:00" to 0,
"6:15" to 0,
"8:00" to 1,
"8:15" to 1,
"10:00" to 2,
"10:15" to 2,
"12:00" to 3,
"12:15" to 3,
"14:00" to 4,
"14:15" to 4,
"16:00" to 5,
"16:15" to 5,
"18:00" to 6,
"18:15" to 6,
)
val timeslotEndMap: Map<String, Int> = mapOf(
"8:00" to 0,
"7:45" to 0,
"10:00" to 1,
"9:45" to 1,
"12:00" to 2,
"11:45" to 2,
"14:00" to 3,
"13:45" to 3,
"16:00" to 4,
"15:45" to 4,
"18:00" to 5,
"17:45" to 5,
"20:00" to 6,
"19:45" to 6,
)
fun parseTimeslot(time: String): Pair<Int, Int> {
val splitTime = time.split(" - ")
val start = timeslotStartMap[splitTime[0]] ?: 0
val length = (timeslotEndMap[splitTime[1]] ?: 0)
return Pair(start, length)
}
}
@@ -0,0 +1,49 @@
package com.denizk0461.studip.data
import android.util.Log
import com.denizk0461.studip.model.StudIPEvent
import org.jsoup.Jsoup
class StudIPParser {
fun parse(html: String, insert: (events: List<StudIPEvent>) -> Unit) {
var id = 0
val doc = Jsoup.parse(html)
Log.d("HELLO2", "doc: $doc")
val newEvents = mutableListOf<StudIPEvent>()
val columns = arrayOf(
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"),
)
Log.d("HELLO2", "days found: ${columns.size}, element 1: ${columns[0]}")
columns.forEachIndexed { index, element ->
element?.getElementsByClass("schedule_entry")?.forEachIndexed { entryIndex, entry ->
Log.d("HELLO2", "element - $entryIndex - found: $entry")
val entryHeader = entry.getElementsByTag("dt")[0].text()
val delimiter = entryHeader.lastIndexOf('(')
val parsedTitle = entryHeader.substring(0 until delimiter)
val parsedLecturers = entryHeader.substring(delimiter + 1 until entryHeader.length - 1)
val entryInfos = entry.getElementsByTag("dd")[0].text().split(", ")
val timeSlot = DummyData.parseTimeslot(entryInfos[0])
val event = StudIPEvent(
id = id,
title = parsedTitle,
lecturer = parsedLecturers,
room = entryInfos[1],
day = index,
timeslotStart = timeSlot.first,
timeslotEnd = timeSlot.second,
)
newEvents.add(event)
id += 1
Log.d("HELLO2", event.toString())
}
}
insert(newEvents)
}
}
@@ -1,29 +0,0 @@
package com.denizk0461.studip.data
import android.content.Context
import android.graphics.Bitmap
import android.webkit.WebView
import android.webkit.WebViewClient
class StudIPScraper : WebViewClient() {
fun parse(context: Context) {
// val res = Jsoup.connect("https://elearning.uni-bremen.de/index.php?again=yes")
// .data("loginname", "deniz7", "password", "xSh4d0wZ3r0_+nbrmn")
// .method(Connection.Method.POST)
// .execute()
// val doc = Jsoup.connect("https://elearning.uni-bremen.de/dispatch.php/calendar/schedule").get()
// Log.d("HELLO", doc.toString())
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
}