From b04c0fb2962ae4f79cd90a1bd8fd5c5082f7951e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 2 Aug 2020 17:43:15 +0200 Subject: [PATCH] Remove indents from timetable parser --- .../sdk/scrapper/timetable/TimetableParser.kt | 158 ++++++++---------- 1 file changed, 71 insertions(+), 87 deletions(-) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/timetable/TimetableParser.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/timetable/TimetableParser.kt index 9fbb2988..3a1393d5 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/timetable/TimetableParser.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/timetable/TimetableParser.kt @@ -63,40 +63,36 @@ class TimetableParser { } } divs.size == 2 -> getLessonInfo(lesson, divs[0]) - divs.size == 3 -> { // TODO: refactor this - when { - divs[0]?.selectFirst("span")?.hasClass(CLASS_CHANGES) == true && - divs[1]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && - divs[2]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true -> { - getLessonInfo(lesson, divs[0]).run { - val old = getLessonInfo(lesson, divs[1]) - copy( - changes = true, - canceled = false, - subjectOld = old.subject, - teacherOld = old.teacher, - roomOld = old.room - ) - } - } - divs[0]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && - divs[1]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && - divs[2]?.selectFirst("span")?.hasClass(CLASS_CHANGES) == true -> { - getLessonInfo(lesson, divs[2]).run { - val old = getLessonInfo(lesson, divs[0]) - copy( - changes = true, - canceled = false, - subjectOld = old.subject, - teacherOld = old.teacher, - roomOld = old.room - ) - } - } - else -> { - getLessonInfo(lesson, divs[1]) + divs.size == 3 -> when { // TODO: refactor this + divs[0]?.selectFirst("span")?.hasClass(CLASS_CHANGES) == true && + divs[1]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && + divs[2]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true -> { + getLessonInfo(lesson, divs[0]).run { + val old = getLessonInfo(lesson, divs[1]) + copy( + changes = true, + canceled = false, + subjectOld = old.subject, + teacherOld = old.teacher, + roomOld = old.room + ) } } + divs[0]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && + divs[1]?.selectFirst("span")?.hasClass(CLASS_MOVED_OR_CANCELED) == true && + divs[2]?.selectFirst("span")?.hasClass(CLASS_CHANGES) == true -> { + getLessonInfo(lesson, divs[2]).run { + val old = getLessonInfo(lesson, divs[0]) + copy( + changes = true, + canceled = false, + subjectOld = old.subject, + teacherOld = old.teacher, + roomOld = old.room + ) + } + } + else -> getLessonInfo(lesson, divs[1]) } else -> null }?.let { @@ -107,17 +103,15 @@ class TimetableParser { } } - private fun getLessonInfo(lesson: Timetable, div: Element): Timetable { - div.select("span").run { - return when { - size == 3 -> getSimpleLesson(lesson, this) - size == 4 && last().hasClass(CLASS_REALIZED) -> getSimpleLesson(lesson, this) - size == 4 -> getGroupLesson(lesson, this) - size == 5 && last().hasClass(CLASS_REALIZED) -> getGroupLesson(lesson, this) - size == 7 -> getSimpleLessonWithReplacement(lesson, this) - size == 9 -> getGroupLessonWithReplacement(lesson, this) - else -> lesson - } + private fun getLessonInfo(lesson: Timetable, div: Element) = div.select("span").run { + when { + size == 3 -> getSimpleLesson(lesson, this) + size == 4 && last().hasClass(CLASS_REALIZED) -> getSimpleLesson(lesson, this) + size == 4 -> getGroupLesson(lesson, this) + size == 5 && last().hasClass(CLASS_REALIZED) -> getGroupLesson(lesson, this) + size == 7 -> getSimpleLessonWithReplacement(lesson, this) + size == 9 -> getGroupLessonWithReplacement(lesson, this) + else -> lesson } } @@ -137,50 +131,40 @@ class TimetableParser { return getLessonWithReplacement(lesson, spans, 1) } - private fun getLesson(lesson: Timetable, spans: Elements, offset: Int = 0): Timetable { - return lesson.copy( - subject = getLessonAndGroupInfoFromSpan(spans[0])[0], - group = getLessonAndGroupInfoFromSpan(spans[0])[1], - teacher = spans[1 + offset].text(), - room = spans[2 + offset].text(), - info = getFormattedLessonInfo(spans.getOrNull(3 + offset)?.text()), - canceled = spans.first().hasClass(CLASS_MOVED_OR_CANCELED), - changes = spans.first().hasClass(CLASS_CHANGES) + private fun getLesson(lesson: Timetable, spans: Elements, offset: Int = 0) = lesson.copy( + subject = getLessonAndGroupInfoFromSpan(spans[0])[0], + group = getLessonAndGroupInfoFromSpan(spans[0])[1], + teacher = spans[1 + offset].text(), + room = spans[2 + offset].text(), + info = getFormattedLessonInfo(spans.getOrNull(3 + offset)?.text()), + canceled = spans.first().hasClass(CLASS_MOVED_OR_CANCELED), + changes = spans.first().hasClass(CLASS_CHANGES) + ) + + private fun getLessonWithReplacement(lesson: Timetable, spans: Elements, o: Int = 0) = lesson.copy( + subject = getLessonAndGroupInfoFromSpan(spans[3 + o])[0], + subjectOld = getLessonAndGroupInfoFromSpan(spans[0])[0], + group = getLessonAndGroupInfoFromSpan(spans[3 + o])[1], + teacher = spans[4 + o * 2].text(), + teacherOld = spans[1 + o].text(), + room = spans[5 + o * 2].text(), + roomOld = spans[2 + o].text(), + info = "${getFormattedLessonInfo(spans.last().text())}, poprzednio: ${getLessonAndGroupInfoFromSpan(spans[0])[0]}", + changes = true + ) + + private fun getFormattedLessonInfo(info: String?) = info?.removeSurrounding("(", ")").orEmpty() + + private fun stripLessonInfo(info: String) = info + .replace("okienko dla uczniów", "") + .replace("zmiana organizacji zajęć", "") + .replace(" ,", "") + .removePrefix(", ") + + private fun getLessonAndGroupInfoFromSpan(span: Element) = span.text().let { + arrayOf( + span.text().substringBefore(" ["), + if (it.contains("[")) span.text().split(" [").last().removeSuffix("]") else "" ) } - - private fun getLessonWithReplacement(lesson: Timetable, spans: Elements, o: Int = 0): Timetable { - return lesson.copy( - subject = getLessonAndGroupInfoFromSpan(spans[3 + o])[0], - subjectOld = getLessonAndGroupInfoFromSpan(spans[0])[0], - group = getLessonAndGroupInfoFromSpan(spans[3 + o])[1], - teacher = spans[4 + o * 2].text(), - teacherOld = spans[1 + o].text(), - room = spans[5 + o * 2].text(), - roomOld = spans[2 + o].text(), - info = "${getFormattedLessonInfo(spans.last().text())}, poprzednio: ${getLessonAndGroupInfoFromSpan(spans[0])[0]}", - changes = true - ) - } - - private fun getFormattedLessonInfo(info: String?): String { - return info?.removeSurrounding("(", ")") ?: "" - } - - private fun stripLessonInfo(info: String): String { - return info - .replace("okienko dla uczniów", "") - .replace("zmiana organizacji zajęć", "") - .replace(" ,", "") - .removePrefix(", ") - } - - private fun getLessonAndGroupInfoFromSpan(span: Element): Array { - return span.text().run { - arrayOf( - span.text().substringBefore(" ["), - if (this.contains("[")) span.text().split(" [").last().removeSuffix("]") else "" - ) - } - } }