From cff72ef4ceab67ce98be7e590ca7cd05835f42e4 Mon Sep 17 00:00:00 2001 From: kasperskier <95685115+kasperskier@users.noreply.github.com> Date: Sun, 12 Jun 2022 22:17:41 +0800 Subject: [PATCH] Roumanwu: chapter upload date logic and cleanup (#12169) --- src/zh/roumanwu/build.gradle | 2 +- .../extension/zh/roumanwu/Roumanwu.kt | 11 +++++---- .../extension/zh/roumanwu/RoumanwuDto.kt | 24 +++++++++++++++---- .../zh/roumanwu/ScrambledImageInterceptor.kt | 9 ++++--- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/zh/roumanwu/build.gradle b/src/zh/roumanwu/build.gradle index 6244ab8cc..ba1639f7a 100644 --- a/src/zh/roumanwu/build.gradle +++ b/src/zh/roumanwu/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'Roumanwu' pkgNameSuffix = 'zh.roumanwu' extClass = '.Roumanwu' - extVersionCode = 1 + extVersionCode = 2 isNsfw = true } diff --git a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/Roumanwu.kt b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/Roumanwu.kt index 4c5813d7a..f4c05c865 100644 --- a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/Roumanwu.kt +++ b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/Roumanwu.kt @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.util.asJsoup import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import kotlinx.serialization.json.decodeFromStream import okhttp3.Response import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -23,15 +24,14 @@ class Roumanwu : HttpSource(), ConfigurableSource { override val lang = "zh" override val supportsLatest = true - private val preferences: SharedPreferences by lazy { + private val preferences: SharedPreferences = Injekt.get().getSharedPreferences("source_$id", 0x0000) - } override val baseUrl = MIRRORS[ max(MIRRORS.size - 1, preferences.getString(MIRROR_PREF, MIRROR_DEFAULT)!!.toInt()) ] - override val client = network.client.newBuilder().addInterceptor(ScrambledImageInterceptor()).build() + override val client = network.client.newBuilder().addInterceptor(ScrambledImageInterceptor).build() private val json: Json by injectLazy() @@ -60,9 +60,10 @@ class Roumanwu : HttpSource(), ConfigurableSource { return if (chapter.images != null) { chapter.getPageList() } else { + @Suppress("NAME_SHADOWING") val response = client.newCall(GET(baseUrl + chapter.chapterAPIPath!!, headers)).execute() if (!response.isSuccessful) throw Exception("服务器错误: ${response.code}") - json.decodeFromString(response.body!!.string()).chapter.getPageList() + response.parseAs().chapter.getPageList() } } @@ -125,6 +126,8 @@ class Roumanwu : HttpSource(), ConfigurableSource { private val TAGS = arrayOf("全部", "正妹", "恋爱", "出版漫画", "肉慾", "浪漫", "大尺度", "巨乳", "有夫之婦", "女大生", "狗血劇", "同居", "好友", "調教", "动作", "後宮", "不倫") } + private inline fun Response.parseAs(): T = json.decodeFromStream(this.body!!.byteStream()) + private inline fun Response.nextjsData() = json.decodeFromString>(this.asJsoup().select("#__NEXT_DATA__").html()).props.pageProps } diff --git a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/RoumanwuDto.kt b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/RoumanwuDto.kt index b309f8998..6dca9d58e 100644 --- a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/RoumanwuDto.kt +++ b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/RoumanwuDto.kt @@ -1,10 +1,13 @@ package eu.kanade.tachiyomi.extension.zh.roumanwu +import eu.kanade.tachiyomi.AppInfo import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import kotlinx.serialization.Serializable +import java.text.SimpleDateFormat +import java.util.Locale import java.util.UUID @Serializable @@ -23,7 +26,7 @@ data class Book( val author: String, val continued: Boolean, val tags: List, - val updatedAt: String? = null, // TODO: 2022-06-02T00:00:00.000Z + val updatedAt: String? = null, val activeResource: Resource? = null, ) { fun toSManga() = SManga.create().apply { @@ -42,11 +45,26 @@ data class Book( url = "/books/$id/$i" name = it } + }.apply { + if (isNewDateLogic && !updatedAt.isNullOrBlank()) { + this[lastIndex].date_upload = DATE_FORMAT.parse(updatedAt)?.time ?: 0L + } } private val uuid by lazy { UUID.fromString(id) } override fun hashCode() = uuid.hashCode() override fun equals(other: Any?) = other is Book && uuid == other.uuid + + companion object { + private val DATE_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH) + private val isNewDateLogic = run { + val commitCount = AppInfo.getVersionName().substringAfter('-', "") + if (commitCount.isNotEmpty()) // Preview + commitCount.toInt() >= 4442 + else // Stable + AppInfo.getVersionCode() >= 81 + } + } } @Serializable @@ -81,7 +99,7 @@ data class Chapter( val chapterAPIPath: String? = null, ) { fun getPageList() = images!!.mapIndexed { i, it -> - Page(i, imageUrl = it.src + if (it.scramble) SCRAMBLED_SUFFIX else "") + Page(i, imageUrl = it.src + if (it.scramble) ScrambledImageInterceptor.SCRAMBLED_SUFFIX else "") } } @@ -90,5 +108,3 @@ data class ChapterWrapper(val chapter: Chapter) @Serializable data class Image(val src: String, val scramble: Boolean) - -const val SCRAMBLED_SUFFIX = "?scrambled" diff --git a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/ScrambledImageInterceptor.kt b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/ScrambledImageInterceptor.kt index a87222682..d4c775865 100644 --- a/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/ScrambledImageInterceptor.kt +++ b/src/zh/roumanwu/src/eu/kanade/tachiyomi/extension/zh/roumanwu/ScrambledImageInterceptor.kt @@ -12,7 +12,7 @@ import okhttp3.ResponseBody.Companion.toResponseBody import java.io.ByteArrayOutputStream import java.security.MessageDigest -class ScrambledImageInterceptor : Interceptor { +object ScrambledImageInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) @@ -48,8 +48,7 @@ class ScrambledImageInterceptor : Interceptor { return response.newBuilder().body(responseBody).build() } - companion object { - private val jpegMediaType = "image/jpeg".toMediaType() - private fun Byte.toPositiveInt() = toInt() and 0xFF - } + private val jpegMediaType = "image/jpeg".toMediaType() + private fun Byte.toPositiveInt() = toInt() and 0xFF + const val SCRAMBLED_SUFFIX = "#scrambled" }