)?
, List> = runCatching { parseNormalPreview(body) }.getOrElse { parseLargePreview(body) }
-
- private fun parseLargePreview(body: String): Pair, List> {
- check(PATTERN_LARGE_PREVIEW.containsMatchIn(body))
- return PATTERN_LARGE_PREVIEW.findAll(body).unzip {
- val position = it.groupValues[2].toInt() - 1
- val imageKey = getThumbKey(it.groupValues[3].trim())
- val pageUrl = it.groupValues[1].trim()
- LargeGalleryPreview(imageKey, position) to pageUrl
- }.run {
- first.toList() to second.toList()
- }
- }
-
- private fun parseNormalPreview(body: String): Pair, List> {
- val isOld = PATTERN_NORMAL_PREVIEW.containsMatchIn(body)
- check(isOld || PATTERN_NORMAL_PREVIEW_NEW.containsMatchIn(body))
- return if (isOld) {
- PATTERN_NORMAL_PREVIEW.findAll(body).unzip {
- val position = it.groupValues[6].toInt() - 1
- val url = it.groupValues[3].trim()
- val imageKey = getNormalPreviewKey(url)
- val xOffset = it.groupValues[4].toIntOrNull() ?: 0
- val width = it.groupValues[1].toInt()
- val height = it.groupValues[2].toInt()
- val pageUrl = it.groupValues[5].trim()
- NormalGalleryPreview(url, imageKey, position, xOffset, width, height) to pageUrl
- }
+ fun parsePreviewList(body: String) = PreviewRegex.findAll(body).unzip {
+ val pageUrl = it.groupValues[1]
+ val position = it.groupValues[2].toInt() - 1
+ val url = it.groupValues[5]
+ val offset = it.groupValues[6]
+ if (offset.isEmpty()) {
+ val imageKey = getThumbKey(url)
+ LargeGalleryPreview(imageKey, position)
} else {
- PATTERN_NORMAL_PREVIEW_NEW.findAll(body).unzip {
- val position = it.groupValues[2].toInt() - 1
- val url = it.groupValues[5].trim()
- val imageKey = getNormalPreviewKey(url)
- val xOffset = it.groupValues[6].toIntOrNull() ?: 0
- val width = it.groupValues[3].toInt()
- val height = it.groupValues[4].toInt()
- val pageUrl = it.groupValues[1].trim()
- NormalGalleryPreview(url, imageKey, position, xOffset, width, height) to pageUrl
- }
- }.run {
- first.toList() to second.toList()
- }
+ val imageKey = getNormalPreviewKey(url)
+ val width = it.groupValues[3].toInt()
+ val height = it.groupValues[4].toInt()
+ NormalGalleryPreview(url, imageKey, position, offset.toInt(), width, height)
+ } to pageUrl
+ }.run {
+ first.toList().apply {
+ if (isEmpty()) throw ParseException("Parse preview list error")
+ } to second.toList()
}
}