1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| package main.java.parser
import bean.Course import Common import main.java.bean.TimeDetail import main.java.bean.TimeTable import org.jsoup.Jsoup import parser.Parser
class WISTParser(private val source: String) : Parser() {
override fun generateCourseList(): List<Course> { val courseList = ArrayList<Course>() val doc = Jsoup.parse(source) val classNameSet = HashSet<String>()
val itemCells = doc.select("td[data-role=item]") for (td in itemCells) { val day = td.attr("data-week").toIntOrNull() ?: continue
val beginNodeAttr = td.attr("data-begin-unit").toIntOrNull() val endNodeAttr = td.attr("data-end-unit").toIntOrNull()
val courseDivs = td.select("div.mtt_arrange_item") for (block in courseDivs) { val name = block.selectFirst(".mtt_item_kcmc")?.ownText()?.trim() ?: continue val teacher = block.selectFirst(".mtt_item_jxbmc")?.text()?.trim().orEmpty() val roomInfoRaw = block.selectFirst(".mtt_item_room")?.text()?.trim().orEmpty()
block.selectFirst(".mtt_item_bjmc")?.text()?.trim()?.takeIf { it.isNotEmpty() }?.let { classNameSet += it }
val rawParts = roomInfoRaw.split(Regex("[,,]")).map(String::trim) val weekParts = rawParts.filter { it.contains("周") } if (weekParts.isEmpty()) continue
val fullWeekStr = weekParts.joinToString(",") val isOdd = fullWeekStr.contains("单") val isEven = fullWeekStr.contains("双")
val weekStrCleaned = weekParts.joinToString(",") { it.replace("周", "") }.replace(" ", "")
val (beginNode, endNode) = extractNodeRange(beginNodeAttr, endNodeAttr, rawParts) if (beginNode <= 0 || endNode <= 0) continue val step = endNode - beginNode + 1 val room = extractRoom(rawParts)
for (week in parseWeeks(weekStrCleaned, isOdd, isEven)) { courseList += Course( name = name, teacher = teacher, room = room, day = day, startNode = beginNode, endNode = endNode, step = step, startWeek = week, endWeek = week, type = 0, note = "" ) } } }
val merged = ArrayList<Course>() Common.mergeWeekCourse(courseList, merged)
Common.generateTimeTable(merged, generateTimeTable())
val optimized = mergeAdjacentNodes(merged) this.classNames = classNameSet.toList().sorted() return optimized }
private var classNames: List<String> = emptyList()
override fun getTableName(): String { return if (classNames.isNotEmpty()) { "武船" + classNames.joinToString(",") + "课表" } else { "武船课表" } }
override fun getNodes(): Int = 12
override fun getMaxWeek(): Int = 20
override fun generateTimeTable(): TimeTable = TimeTable( name = "武汉船舶职业技术学院", timeList = listOf( TimeDetail(1, "08:10", "08:55"), TimeDetail(2, "09:05", "09:50"), TimeDetail(3, "10:10", "10:55"), TimeDetail(4, "11:05", "11:50"), TimeDetail(5, "12:30", "13:10"), TimeDetail(6, "13:20", "14:00"), TimeDetail(7, "14:00", "14:45"), TimeDetail(8, "14:55", "15:40"), TimeDetail(9, "16:00", "16:45"), TimeDetail(10, "16:55", "17:40"), TimeDetail(11, "19:00", "19:45"), TimeDetail(12, "19:55", "20:40") ) ) private fun parseWeeks(rawText: String, ignored1: Boolean = false, ignored2: Boolean = false): List<Int> { val weekList = mutableSetOf<Int>() val parts = rawText.split(",")
for (part in parts) { val isOdd = part.contains("单") val isEven = part.contains("双")
val clean = part.replace(Regex("[^0-9\\-]"), "") val weekRange = if ("-" in clean) { val (start, end) = clean.split("-").mapNotNull { it.toIntOrNull() } (start..end).toList() } else { listOfNotNull(clean.toIntOrNull()) }
val filtered = when { isOdd -> weekRange.filter { it % 2 == 1 } isEven -> weekRange.filter { it % 2 == 0 } else -> weekRange }
weekList += filtered }
return weekList.toList().sorted() }
private fun isValidWeek(week: Int, isOdd: Boolean, isEven: Boolean): Boolean { return if (isOdd) week % 2 == 1 else if (isEven) week % 2 == 0 else true }
private fun parseNode(str: String): Int = when { str.contains("中1") -> 5 str.contains("中2") -> 6 else -> str.filter(Char::isDigit).toIntOrNull() ?: -1 }
private fun extractNodeRange(beginAttr: Int?, endAttr: Int?, parts: List<String>): Pair<Int, Int> { return if (beginAttr != null && endAttr != null) { Pair(beginAttr, endAttr) } else { val nodePart = parts.firstOrNull { nodePattern.matches(it) } ?: return Pair(-1, -1) val nodes = nodePart.split("-") Pair(parseNode(nodes.first()), parseNode(nodes.last())) } }
private fun extractRoom(parts: List<String>): String { val roomRegex = Regex("实验室|教室|机房") return parts.firstOrNull { !it.contains("周") && !it.matches(Regex("^(中?[1-9]\\d?)(-(中?[1-9]\\d?))?$")) && roomRegex.containsMatchIn(it) } ?: parts.firstOrNull { !it.contains("周") && !it.matches(Regex("^(中?[1-9]\\d?)(-(中?[1-9]\\d?))?$")) }.orEmpty() }
private fun mergeAdjacentNodes(courses: List<Course>): List<Course> { val sorted = courses.sortedWith( compareBy<Course> { it.name } .thenBy { it.teacher } .thenBy { it.room } .thenBy { it.day } .thenBy { it.startWeek } .thenBy { it.endWeek } .thenBy { it.startNode } ) val result = ArrayList<Course>() var current = sorted[0]
for (i in 1 until sorted.size) { val next = sorted[i] if ( current.name == next.name && current.teacher == next.teacher && current.room == next.room && current.day == next.day && current.startWeek == next.startWeek && current.endWeek == next.endWeek && current.type == next.type && current.endNode + 1 == next.startNode ) { current = current.copy( endNode = next.endNode, step = next.endNode - current.startNode + 1 ) } else { result += current current = next } } result += current return result }
companion object { private val weekPattern = Regex("[^0-9\\-,(单双)]")
private val nodePattern = Regex("^(中?[1-9]\\d?)(-(中?[1-9]\\d?))?$")
fun extractTableHtml(fullHtml: String): String? { return Jsoup.parse(fullHtml).selectFirst("table.wut_table")?.outerHtml() } } }
|