From b34dfcfa38eda2f3ebe00eb7746b64c6b3163ee4 Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Tue, 6 Jun 2023 17:18:25 +0800 Subject: [PATCH] DMZJ: tweak comments page (#16672) --- src/zh/dmzj/build.gradle | 2 +- .../kanade/tachiyomi/extension/zh/dmzj/ApiV3.kt | 15 ++++++++++----- .../extension/zh/dmzj/CommentsInterceptor.kt | 4 +--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/zh/dmzj/build.gradle b/src/zh/dmzj/build.gradle index cdb498bb0..d6f44343f 100644 --- a/src/zh/dmzj/build.gradle +++ b/src/zh/dmzj/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'DMZJ' pkgNameSuffix = 'zh.dmzj' extClass = '.Dmzj' - extVersionCode = 40 + extVersionCode = 41 } apply from: "$rootDir/common.gradle" diff --git a/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/ApiV3.kt b/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/ApiV3.kt index be5b63bed..cd8f43263 100644 --- a/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/ApiV3.kt +++ b/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/ApiV3.kt @@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.source.model.SManga import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonPrimitive import okhttp3.Response +import org.jsoup.parser.Parser object ApiV3 { @@ -55,10 +56,14 @@ object ApiV3 { fun chapterCommentsUrl(path: String) = "$v3apiUrl/viewPoint/0/$path.json" - fun parseChapterComments(response: Response): List { + fun parseChapterComments(response: Response, count: Int): List { val result: List = response.parseAs() - (result as MutableList).sort() - return result.map { it.toString() } + if (result.isEmpty()) return listOf("没有吐槽") + val aggregated = result.groupBy({ it.content }, { it.num }).map { (content, likes) -> + ChapterCommentDto(Parser.unescapeEntities(content, false), likes.sum()) + } as ArrayList + aggregated.sort() + return aggregated.take(count).map { it.toString() } } @Serializable @@ -108,8 +113,8 @@ object ApiV3 { @Serializable class ChapterCommentDto( - private val content: String, - private val num: Int, + val content: String, + val num: Int, ) : Comparable { override fun toString() = if (num > 0) "$content [+$num]" else content override fun compareTo(other: ChapterCommentDto) = other.num.compareTo(num) // descending diff --git a/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/CommentsInterceptor.kt b/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/CommentsInterceptor.kt index 245690289..4508db3fa 100644 --- a/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/CommentsInterceptor.kt +++ b/src/zh/dmzj/src/eu/kanade/tachiyomi/extension/zh/dmzj/CommentsInterceptor.kt @@ -26,9 +26,7 @@ object CommentsInterceptor : Interceptor { val response = chain.proceed(request) if (request.tag(Tag::class) == null) return response - val comments = ApiV3.parseChapterComments(response) - .take(MAX_HEIGHT / (UNIT * 2)) - .ifEmpty { listOf("没有吐槽") } + val comments = ApiV3.parseChapterComments(response, MAX_HEIGHT / (UNIT * 2)) val paint = TextPaint().apply { color = Color.BLACK