Skip to content

Commit

Permalink
fix: Nhentai cover (#587)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
cuong-tran and az4521 authored Dec 18, 2024
1 parent 573f1cf commit 7c2467a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
41 changes: 35 additions & 6 deletions app/src/main/java/eu/kanade/tachiyomi/source/online/all/NHentai.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonResponse>(json)
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 7c2467a

Please sign in to comment.