From a5b84c4224e5880767cc7a7647bae1791e47e45e Mon Sep 17 00:00:00 2001 From: beerpsi <92439990+beerpiss@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:22:06 +0700 Subject: [PATCH] ComicGamma: Use SpeedBinb reader (#1317) * ComicGamma: Use SpeedBinb reader * Update lib-multisrc/comicgamma/build.gradle.kts Co-authored-by: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> --------- Co-authored-by: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> --- lib-multisrc/comicgamma/build.gradle.kts | 6 ++- .../multisrc/comicgamma/ComicGamma.kt | 19 ++++--- .../tachiyomi/multisrc/comicgamma/PtImg.kt | 26 ---------- .../multisrc/comicgamma/PtImgInterceptor.kt | 49 ------------------- 4 files changed, 18 insertions(+), 82 deletions(-) delete mode 100644 lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImg.kt delete mode 100644 lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImgInterceptor.kt diff --git a/lib-multisrc/comicgamma/build.gradle.kts b/lib-multisrc/comicgamma/build.gradle.kts index ede652be5..881d317e2 100644 --- a/lib-multisrc/comicgamma/build.gradle.kts +++ b/lib-multisrc/comicgamma/build.gradle.kts @@ -2,4 +2,8 @@ plugins { id("lib-multisrc") } -baseVersionCode = 6 +baseVersionCode = 7 + +dependencies { + api(project(":lib:speedbinb")) +} diff --git a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/ComicGamma.kt b/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/ComicGamma.kt index 70697caba..8a14314a6 100644 --- a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/ComicGamma.kt +++ b/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/ComicGamma.kt @@ -1,16 +1,20 @@ package eu.kanade.tachiyomi.multisrc.comicgamma +import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbInterceptor +import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbReader import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.FilterList 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 eu.kanade.tachiyomi.source.online.ParsedHttpSource +import kotlinx.serialization.json.Json import org.jsoup.nodes.Document import org.jsoup.nodes.Element import org.jsoup.select.Evaluator import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.text.SimpleDateFormat import java.util.Locale import java.util.TimeZone @@ -22,7 +26,11 @@ open class ComicGamma( ) : ParsedHttpSource() { override val supportsLatest = false - override val client = network.client.newBuilder().addInterceptor(PtImgInterceptor).build() + private val json = Injekt.get() + + override val client = network.client.newBuilder() + .addInterceptor(SpeedBinbInterceptor(json)) + .build() override fun popularMangaRequest(page: Int) = GET("$baseUrl/manga/", headers) override fun popularMangaNextPageSelector(): String? = null @@ -54,10 +62,9 @@ open class ComicGamma( override fun searchMangaRequest(page: Int, query: String, filters: FilterList) = throw UnsupportedOperationException() - override fun pageListParse(document: Document) = - document.select("#content > div[data-ptimg]").mapIndexed { i, e -> - Page(i, imageUrl = e.attr("abs:data-ptimg")) - } + private val reader by lazy { SpeedBinbReader(client, headers, json) } + + override fun pageListParse(document: Document) = reader.pageListParse(document) override fun mangaDetailsParse(document: Document): SManga { val titleElement = document.selectFirst(Evaluator.Class("manga__title"))!! diff --git a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImg.kt b/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImg.kt deleted file mode 100644 index 055b73e0b..000000000 --- a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImg.kt +++ /dev/null @@ -1,26 +0,0 @@ -package eu.kanade.tachiyomi.multisrc.comicgamma - -import kotlinx.serialization.Serializable - -val COORD_REGEX = Regex("""^i:(\d+),(\d+)\+(\d+),(\d+)>(\d+),(\d+)$""") - -@Serializable -class PtImg(val resources: Resource, val views: List) { - fun getFilename() = resources.i.src - fun getViewSize() = Pair(views[0].width, views[0].height) - fun getTranslations() = views[0].coords.map { coord -> - val v = COORD_REGEX.matchEntire(coord)!!.groupValues.drop(1).map { it.toInt() } - Translation(v[0], v[1], v[2], v[3], v[4], v[5]) - } -} - -@Serializable -class Resource(val i: Image) - -@Serializable -class Image(val src: String, val width: Int, val height: Int) - -@Serializable -class View(val width: Int, val height: Int, val coords: List) - -class Translation(val ix: Int, val iy: Int, val w: Int, val h: Int, val vx: Int, val vy: Int) diff --git a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImgInterceptor.kt b/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImgInterceptor.kt deleted file mode 100644 index fde055bf6..000000000 --- a/lib-multisrc/comicgamma/src/eu/kanade/tachiyomi/multisrc/comicgamma/PtImgInterceptor.kt +++ /dev/null @@ -1,49 +0,0 @@ -package eu.kanade.tachiyomi.multisrc.comicgamma - -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.graphics.Canvas -import android.graphics.Rect -import kotlinx.serialization.decodeFromString -import kotlinx.serialization.json.Json -import okhttp3.Interceptor -import okhttp3.MediaType.Companion.toMediaType -import okhttp3.Response -import okhttp3.ResponseBody.Companion.toResponseBody -import uy.kohesive.injekt.injectLazy -import java.io.ByteArrayOutputStream - -object PtImgInterceptor : Interceptor { - - private val json: Json by injectLazy() - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - val response = chain.proceed(request) - val url = request.url - val path = url.pathSegments - if (!path.last().endsWith(".ptimg.json")) return response - - val metadata = json.decodeFromString(response.body.string()) - val imageUrl = url.newBuilder().setEncodedPathSegment(path.size - 1, metadata.getFilename()).build() - val imgRequest = request.newBuilder().url(imageUrl).build() - val imgResponse = chain.proceed(imgRequest) - val image = BitmapFactory.decodeStream(imgResponse.body.byteStream()) - val (width, height) = metadata.getViewSize() - val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) - val view = Canvas(result) - - metadata.getTranslations().forEach { - val src = Rect(it.ix, it.iy, it.ix + it.w, it.iy + it.h) - val dst = Rect(it.vx, it.vy, it.vx + it.w, it.vy + it.h) - view.drawBitmap(image, src, dst, null) - } - - val output = ByteArrayOutputStream() - result.compress(Bitmap.CompressFormat.JPEG, 90, output) - val responseBody = output.toByteArray().toResponseBody(jpegMediaType) - return imgResponse.newBuilder().body(responseBody).build() - } - - private val jpegMediaType = "image/jpeg".toMediaType() -}