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>
This commit is contained in:
beerpsi 2024-02-17 18:22:06 +07:00 committed by GitHub
parent 64430ef03c
commit a5b84c4224
4 changed files with 18 additions and 82 deletions

View File

@ -2,4 +2,8 @@ plugins {
id("lib-multisrc")
}
baseVersionCode = 6
baseVersionCode = 7
dependencies {
api(project(":lib:speedbinb"))
}

View File

@ -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<Json>()
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"))!!

View File

@ -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<View>) {
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<String>)
class Translation(val ix: Int, val iy: Int, val w: Int, val h: Int, val vx: Int, val vy: Int)

View File

@ -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<PtImg>(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()
}