From 7c2467aed5866601bb53958a510ad0c30ed6781c Mon Sep 17 00:00:00 2001 From: Cuong-Tran Date: Wed, 18 Dec 2024 12:26:20 +0700 Subject: [PATCH] fix: Nhentai cover (#587) * fix nh images not loading (cherry picked from commit 3278e2fe918de87056528eb4aeaf57be6d71d35a) * fix thumbs on nh (cherry picked from commit aa295377e78a0d5a658ec983f7a28b6345d86c09) * default to media server 1 for old nh entries (cherry picked from commit 2990a5e7ab98de5dc89c0affe7e265827d3244a3) --------- Co-authored-by: az4521 <18432684+az4521@users.noreply.github.com> --- .../tachiyomi/source/online/all/NHentai.kt | 41 ++++++++++++++++--- .../metadata/NHentaiSearchMetadata.kt | 9 +++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/online/all/NHentai.kt b/app/src/main/java/eu/kanade/tachiyomi/source/online/all/NHentai.kt index 3e97ffaccb..754894b7df 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/online/all/NHentai.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/online/all/NHentai.kt @@ -71,7 +71,11 @@ class NHentai(delegate: HttpSource, val context: Context) : } override suspend fun parseIntoMetadata(metadata: NHentaiSearchMetadata, input: Response) { - val json = GALLERY_JSON_REGEX.find(input.body.string())!!.groupValues[1].replace( + // AZ --> + val strdata = input.body.string() + val server = MEDIA_SERVER_REGEX.find(strdata)?.groupValues?.get(1)?.toInt() ?: 1 + // AZ <-- + val json = GALLERY_JSON_REGEX.find(strdata)!!.groupValues[1].replace( UNICODE_ESCAPE_REGEX, ) { it.groupValues[1].toInt(radix = 16).toChar().toString() } val jsonResponse = jsonParser.decodeFromString(json) @@ -85,6 +89,10 @@ class NHentai(delegate: HttpSource, val context: Context) : mediaId = jsonResponse.mediaId + // AZ --> + mediaServer = server + // AZ <-- + jsonResponse.title?.let { title -> japaneseTitle = title.japanese shortTitle = title.pretty @@ -191,17 +199,33 @@ class NHentai(delegate: HttpSource, val context: Context) : return PagePreviewPage( page, metadata.pageImageTypes.mapIndexed { index, s -> - PagePreviewInfo(index + 1, imageUrl = thumbnailUrlFromType(metadata.mediaId!!, index + 1, s)!!) + PagePreviewInfo( + index + 1, + imageUrl = thumbnailUrlFromType( + metadata.mediaId!!, + // AZ --> + metadata.mediaServer ?: 1, + // AZ <-- + index + 1, + s, + )!!, + ) }, false, 1, ) } - private fun thumbnailUrlFromType(mediaId: String, page: Int, t: String) = - NHentaiSearchMetadata.typeToExtension(t)?.let { - "https://t3.nhentai.net/galleries/$mediaId/${page}t.$it" - } + private fun thumbnailUrlFromType( + mediaId: String, + // AZ --> + mediaServer: Int, + // AZ <-- + page: Int, + t: String, + ) = NHentaiSearchMetadata.typeToExtension(t)?.let { + "https://t$mediaServer.nhentai.net/galleries/$mediaId/${page}t.$it" + } override suspend fun fetchPreviewImage(page: PagePreviewInfo, cacheControl: CacheControl?): Response { return client.newCachelessCallWithProgress( @@ -222,6 +246,11 @@ class NHentai(delegate: HttpSource, val context: Context) : } private val GALLERY_JSON_REGEX = Regex(".parse\\(\"(.*)\"\\);") + + // AZ --> + private val MEDIA_SERVER_REGEX = Regex("media_server\\s*:\\s*(\\d+)") + + // AZ <-- private val UNICODE_ESCAPE_REGEX = Regex("\\\\u([0-9a-fA-F]{4})") private const val TITLE_PREF = "Display manga title as:" } diff --git a/source-api/src/commonMain/kotlin/exh/metadata/metadata/NHentaiSearchMetadata.kt b/source-api/src/commonMain/kotlin/exh/metadata/metadata/NHentaiSearchMetadata.kt index 1885dd6de3..1be880c10c 100644 --- a/source-api/src/commonMain/kotlin/exh/metadata/metadata/NHentaiSearchMetadata.kt +++ b/source-api/src/commonMain/kotlin/exh/metadata/metadata/NHentaiSearchMetadata.kt @@ -29,6 +29,10 @@ class NHentaiSearchMetadata : RaisedSearchMetadata() { var mediaId: String? = null + // AZ --> + var mediaServer: Int? = null + // AZ <-- + var japaneseTitle by titleDelegate(TITLE_TYPE_JAPANESE) var englishTitle by titleDelegate(TITLE_TYPE_ENGLISH) var shortTitle by titleDelegate(TITLE_TYPE_SHORT) @@ -45,8 +49,11 @@ class NHentaiSearchMetadata : RaisedSearchMetadata() { val key = nhId?.let { nhIdToPath(it) } val cover = if (mediaId != null) { + // AZ --> + val server = mediaServer ?: 1 + // AZ <-- typeToExtension(coverImageType)?.let { - "https://t.nhentai.net/galleries/$mediaId/cover.$it" + "https://t$server.nhentai.net/galleries/$mediaId/cover.$it" } } else { null