From 06eb277b34f6c2ef72620e3baf05383d61bb8f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=99=BD-=E7=99=BD?= Date: Fri, 25 Oct 2024 16:00:04 +0800 Subject: [PATCH] Fix preview parser --- .../client/parser/GalleryDetailParser.kt | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt index 71aac79fe..824b471a4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt @@ -72,8 +72,12 @@ object GalleryDetailParser { Regex("]+>]+>([\\d,]+)]+>(?:]+>)?>(?:)?") private val PATTERN_NORMAL_PREVIEW = Regex("
]*>]*width:(\\d+)[^<>]*height:(\\d+)[^<>]*\\((.+?)\\)[^<>]*-(\\d+)px[^<>]*>]*href=\"(.+?)\"[^<>]*>\"([\\d,]+)\"")]*title=\"Page (\\d+):[^<>]*width:(\\d+)[^<>]*height:(\\d+)[^<>]*\\((.+?)\\)[^<>]*-(\\d+)px[^<>]*>") private val PATTERN_LARGE_PREVIEW = Regex("
\"([\\d,]+)\".+?src=\"(.+?)\"")]*title=\"Page (\\d+):[^<>]*\\((.+?)\\)[^<>]*0 0[^<>]*>") private val PATTERN_NEWER_DATE = Regex(", added (.+?)
") private val PATTERN_FAVORITE_SLOT = Regex("/fav.png\\); background-position:0px -(\\d+)px") @@ -440,8 +444,10 @@ object GalleryDetailParser { fun parsePreviewList(body: String): Pair, 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 isOld = PATTERN_LARGE_PREVIEW.containsMatchIn(body) + check(isOld || PATTERN_LARGE_PREVIEW_NEW.containsMatchIn(body)) + val patternLarge = if (isOld) PATTERN_LARGE_PREVIEW else PATTERN_LARGE_PREVIEW_NEW + return patternLarge.findAll(body).unzip { val position = it.groupValues[2].toInt() - 1 val imageKey = getThumbKey(it.groupValues[3].trim()) val pageUrl = it.groupValues[1].trim() @@ -452,16 +458,30 @@ object GalleryDetailParser { } private fun parseNormalPreview(body: String): Pair, List> { - check(PATTERN_NORMAL_PREVIEW.containsMatchIn(body)) - return 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 + 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 + } + } 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() }