NHentai: fix thumbnails (#9961)

* NHentai: fix thumbnails

closes https://github.com/keiyoushi/extensions-source/issues/9897

* rename
This commit is contained in:
AwkwardPeak7 2025-08-03 22:24:26 +05:00 committed by Draff
parent c2d47af025
commit 853a801f2f
Signed by: Draff
GPG Key ID: E8A89F3211677653
3 changed files with 38 additions and 32 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'NHentai' extName = 'NHentai'
extClass = '.NHFactory' extClass = '.NHFactory'
extVersionCode = 52 extVersionCode = 53
isNsfw = true isNsfw = true
} }

View File

@ -27,8 +27,15 @@ class Images(
@Serializable @Serializable
class Image( class Image(
val t: String, private val t: String,
) ) {
val extension get() = when (t) {
"w" -> "webp"
"p" -> "png"
"g" -> "gif"
else -> "jpg"
}
}
@Serializable @Serializable
class Tag( class Tag(

View File

@ -208,14 +208,11 @@ open class NHentai(
override fun searchMangaNextPageSelector() = latestUpdatesNextPageSelector() override fun searchMangaNextPageSelector() = latestUpdatesNextPageSelector()
override fun mangaDetailsParse(document: Document): SManga { override fun mangaDetailsParse(document: Document): SManga {
val script = document.selectFirst(hentaiSelector)!!.data() val data = document.getHentaiData()
val cdnUrl = document.getCdnUrls(thumbnail = true).random()
val json = dataRegex.find(script)?.groupValues!![1]
val data = json.parseAs<Hentai>()
return SManga.create().apply { return SManga.create().apply {
title = if (displayFullTitle) data.title.english ?: data.title.japanese ?: data.title.pretty!! else data.title.pretty ?: (data.title.english ?: data.title.japanese)!!.shortenTitle() title = if (displayFullTitle) data.title.english ?: data.title.japanese ?: data.title.pretty!! else data.title.pretty ?: (data.title.english ?: data.title.japanese)!!.shortenTitle()
thumbnail_url = document.select("#cover > a > img").attr("data-src") thumbnail_url = "https://$cdnUrl/galleries/${data.media_id}/1t.${data.images.pages[0].extension}"
status = SManga.COMPLETED status = SManga.COMPLETED
artist = getArtists(data) artist = getArtists(data)
author = getGroups(data) ?: getArtists(data) author = getGroups(data) ?: getArtists(data)
@ -235,12 +232,7 @@ open class NHentai(
override fun chapterListRequest(manga: SManga): Request = GET("$baseUrl${manga.url}", headers) override fun chapterListRequest(manga: SManga): Request = GET("$baseUrl${manga.url}", headers)
override fun chapterListParse(response: Response): List<SChapter> { override fun chapterListParse(response: Response): List<SChapter> {
val document = response.asJsoup() val data = response.asJsoup().getHentaiData()
val script = document.selectFirst(hentaiSelector)!!.data()
val json = dataRegex.find(script)?.groupValues!![1]
val data = json.parseAs<Hentai>()
return listOf( return listOf(
SChapter.create().apply { SChapter.create().apply {
name = "Chapter" name = "Chapter"
@ -255,30 +247,37 @@ open class NHentai(
override fun chapterListSelector() = throw UnsupportedOperationException() override fun chapterListSelector() = throw UnsupportedOperationException()
override fun pageListParse(document: Document) = throw UnsupportedOperationException() override fun pageListParse(document: Document): List<Page> {
val data = document.getHentaiData()
override fun pageListParse(response: Response): List<Page> { val cdnUrls = document.getCdnUrls(thumbnail = false)
val html = response.body.string()
val json = dataRegex.find(html)?.groupValues!![1]
val data = json.parseAs<Hentai>()
val cdnJson = Regex("""image_cdn_urls:\s*(\[.*])""").find(html)?.groupValues!![1]
val cdnList = cdnJson.parseAs<List<String>>()
return data.images.pages.mapIndexed { i, image -> return data.images.pages.mapIndexed { i, image ->
Page( Page(
i, index = i,
imageUrl = "https://${cdnList.random()}/galleries/${data.media_id}/${i + 1}" + imageUrl = "https://${cdnUrls.random()}/galleries/${data.media_id}/${i + 1}.${image.extension}",
when (image.t) {
"w" -> ".webp"
"p" -> ".png"
"g" -> ".gif"
else -> ".jpg"
},
) )
} }
} }
private fun Document.getHentaiData(): Hentai {
val script = selectFirst(hentaiSelector)!!.data()
return dataRegex.find(script)!!.groupValues[1].parseAs()
}
private fun Document.getCdnUrls(thumbnail: Boolean): List<String> {
val regex = Regex(
if (thumbnail) {
"""thumb_cdn_urls:\s*(\[.*])"""
} else {
"""image_cdn_urls:\s*(\[.*])"""
},
)
val html = body().html()
val cdnJson = regex.find(html)!!.groupValues[1]
return cdnJson.parseAs<List<String>>()
}
override fun getFilterList(): FilterList = FilterList( override fun getFilterList(): FilterList = FilterList(
Filter.Header("Separate tags with commas (,)"), Filter.Header("Separate tags with commas (,)"),
Filter.Header("Prepend with dash (-) to exclude"), Filter.Header("Prepend with dash (-) to exclude"),