Dsl motto motto

This commit is contained in:
tarsin committed 2023-05-07 23:31:27 +08:00
1 parent 61aab22c02
commit 8b01906f1e
3 files changed
+27 -11

No files matched your search

@@ -18,7 +18,6 @@ package com.hippo.ehviewer.client
import android.util.Log
import arrow.fx.coroutines.parZip
import com.hippo.ehviewer.AppConfig
import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.GetText
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
@@ -55,13 +54,11 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.executeAsync
import org.json.JSONArray
import org.jsoup.Jsoup
import java.io.File
import kotlin.math.ceil
private val okHttpClient = EhApplication.okHttpClient
private const val TAG = "EhEngine"
private const val MAX_REQUEST_SIZE = 25
private const val SAD_PANDA_DISPOSITION = "inline; filename=\"sadpanda.jpg\""
@@ -120,12 +117,12 @@ private fun rethrowExactly(code: Int, headers: Headers, body: String, e: Throwab
private suspend inline fun <T> Request.executeAndParsingWith(block: String.() -> T): T {
Log.d(TAG, url.toString())
return okHttpClient.newCall(this).executeAsync().use { response ->
val body = response.body.string()
return execute {
val body = body.string()
try {
block(body)
} catch (e: Exception) {
rethrowExactly(response.code, response.headers, body, e)
rethrowExactly(code, headers, body, e)
}
}
}
@@ -15,14 +15,20 @@
*/
package com.hippo.ehviewer.client
import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
import com.hippo.ehviewer.EhApplication.Companion.okHttpClient
import okhttp3.FormBody
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.executeAsync
import org.json.JSONArray
import org.json.JSONObject
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
inline fun ehRequest(url: String, referer: String? = null, origin: String? = null, builder: Request.Builder.() -> Unit = {}) = Request.Builder().url(url).apply {
addHeader("User-Agent", CHROME_USER_AGENT)
@@ -44,6 +50,20 @@ fun jsonArrayOf(vararg element: Any?) = JSONArray().apply { element.forEach { pu
inline fun Request.Builder.jsonBody(builder: JSONObject.() -> Unit) = post(JSONObject().apply(builder).toString().toRequestBody(MEDIA_TYPE_JSON))
suspend inline fun <R> Request.execute(block: Response.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return okHttpClient.newCall(this).executeAsync().use(block)
}
suspend inline fun <R> Request.executeNonCache(block: Response.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return nonCacheOkHttpClient.newCall(this).executeAsync().use(block)
}
const val CHROME_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
const val CHROME_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
const val CHROME_ACCEPT_LANGUAGE = "en-US,en;q=0.5"
@@ -18,12 +18,12 @@ package com.hippo.ehviewer.spider
import android.graphics.ImageDecoder
import android.os.ParcelFileDescriptor
import android.os.ParcelFileDescriptor.MODE_READ_WRITE
import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhUtils.getSuitableTitle
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.ehRequest
import com.hippo.ehviewer.client.executeNonCache
import com.hippo.ehviewer.client.getImageKey
import com.hippo.ehviewer.coil.edit
import com.hippo.ehviewer.coil.read
@@ -38,7 +38,6 @@ import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.Response
import okhttp3.executeAsync
import okio.buffer
import okio.sink
import java.io.IOException
@@ -136,9 +135,9 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
): Boolean {
// TODO: Use HttpEngine[https://developer.android.com/reference/android/net/http/HttpEngine] directly here if available
// Since we don't want unnecessary copy between jvm heap & native heap
nonCacheOkHttpClient.newCall(ehRequest(url, referer)).executeAsync().use {
if (it.code >= 400) return false
return saveFromHttpResponse(index, it, notifyProgress)
ehRequest(url, referer).executeNonCache {
if (code >= 400) return false
return saveFromHttpResponse(index, this, notifyProgress)
}
}