优化
This commit is contained in:
@@ -4,6 +4,7 @@ import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.legado.app.help
|
||||
import androidx.annotation.Keep
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.ACache
|
||||
import io.legado.app.utils.FileUtils
|
||||
|
||||
@@ -4,8 +4,6 @@ import com.bumptech.glide.load.Option
|
||||
import com.bumptech.glide.load.Options
|
||||
import com.bumptech.glide.load.model.GlideUrl
|
||||
import com.bumptech.glide.load.model.ModelLoader
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.isAbsUrl
|
||||
import java.io.InputStream
|
||||
|
||||
object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> {
|
||||
@@ -20,15 +18,7 @@ object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> {
|
||||
height: Int,
|
||||
options: Options,
|
||||
): ModelLoader.LoadData<InputStream?> {
|
||||
val cacheKey = model.toString()
|
||||
var modelWithHeader = model
|
||||
if (cacheKey.isAbsUrl()) {
|
||||
modelWithHeader = AnalyzeUrl(cacheKey).getGlideUrl()
|
||||
}
|
||||
return ModelLoader.LoadData(
|
||||
modelWithHeader,
|
||||
OkHttpStreamFetcher(model,modelWithHeader, options)
|
||||
)
|
||||
return ModelLoader.LoadData(model, OkHttpStreamFetcher(model, options))
|
||||
}
|
||||
|
||||
override fun handles(model: GlideUrl): Boolean {
|
||||
|
||||
@@ -10,12 +10,12 @@ import com.bumptech.glide.util.ContentLengthInputStream
|
||||
import com.script.rhino.runScriptWithContext
|
||||
import io.legado.app.data.entities.BaseSource
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.http.CookieManager.cookieJarHeader
|
||||
import io.legado.app.help.http.addHeaders
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.help.http.okHttpClientManga
|
||||
import io.legado.app.help.source.SourceHelp
|
||||
import io.legado.app.model.ReadManga
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.ImageUtils
|
||||
import io.legado.app.utils.isWifiConnect
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -30,7 +30,6 @@ import java.io.InputStream
|
||||
|
||||
|
||||
class OkHttpStreamFetcher(
|
||||
private val oldUrl: GlideUrl,
|
||||
private val url: GlideUrl,
|
||||
private val options: Options,
|
||||
) :
|
||||
@@ -41,6 +40,7 @@ class OkHttpStreamFetcher(
|
||||
private var source: BaseSource? = null
|
||||
private val manga = options.get(OkHttpModelLoader.mangaOption) == true
|
||||
private val coroutineContext = Job()
|
||||
private lateinit var analyzedUrl: GlideUrl
|
||||
|
||||
@Volatile
|
||||
private var call: Call? = null
|
||||
@@ -59,21 +59,19 @@ class OkHttpStreamFetcher(
|
||||
callback.onLoadFailed(NoStackTraceException("只在wifi加载图片"))
|
||||
return
|
||||
}
|
||||
val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl())
|
||||
val headerMap = HashMap<String, String>()
|
||||
|
||||
options.get(OkHttpModelLoader.sourceOriginOption)?.let { sourceUrl ->
|
||||
source = SourceHelp.getSource(sourceUrl)
|
||||
runScriptWithContext(coroutineContext) {
|
||||
source?.getHeaderMap(true)?.let {
|
||||
headerMap.putAll(it)
|
||||
}
|
||||
}
|
||||
if (source?.enabledCookieJar == true) {
|
||||
headerMap[cookieJarHeader] = "1"
|
||||
}
|
||||
}
|
||||
headerMap.putAll(url.headers)
|
||||
requestBuilder.addHeaders(headerMap)
|
||||
|
||||
analyzedUrl = AnalyzeUrl(
|
||||
url.toString(),
|
||||
source = source,
|
||||
coroutineContext = coroutineContext
|
||||
).getGlideUrl()
|
||||
|
||||
val requestBuilder = Request.Builder().url(analyzedUrl.toStringUrl())
|
||||
requestBuilder.addHeaders(analyzedUrl.headers)
|
||||
val request: Request = requestBuilder.build()
|
||||
this.callback = callback
|
||||
call = if (manga) {
|
||||
@@ -118,7 +116,7 @@ class OkHttpStreamFetcher(
|
||||
responseBody!!.byteStream()
|
||||
} else if (manga) {
|
||||
ImageUtils.decode(
|
||||
oldUrl.toString(),
|
||||
url.toString(),
|
||||
responseBody!!.bytes(),
|
||||
isCover = false,
|
||||
source,
|
||||
@@ -126,7 +124,7 @@ class OkHttpStreamFetcher(
|
||||
)?.inputStream()
|
||||
} else {
|
||||
ImageUtils.decode(
|
||||
url.toStringUrl(), responseBody!!.byteStream(),
|
||||
analyzedUrl.toStringUrl(), responseBody!!.byteStream(),
|
||||
isCover = true, source
|
||||
)
|
||||
}
|
||||
@@ -142,7 +140,7 @@ class OkHttpStreamFetcher(
|
||||
}
|
||||
} else {
|
||||
if (!manga) {
|
||||
failUrl.add(url.toStringUrl())
|
||||
failUrl.add(analyzedUrl.toStringUrl())
|
||||
}
|
||||
callback?.onLoadFailed(HttpException(response.message, response.code))
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import io.legado.app.help.glide.BlurTransformation
|
||||
import io.legado.app.help.glide.ImageLoader
|
||||
import io.legado.app.help.glide.OkHttpModelLoader
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.BitmapUtils
|
||||
import io.legado.app.utils.GSON
|
||||
|
||||
@@ -88,11 +88,6 @@ class AnalyzeRule(
|
||||
return this
|
||||
}
|
||||
|
||||
fun setCoroutineContext(context: CoroutineContext): AnalyzeRule {
|
||||
coroutineContext = context.minusKey(ContinuationInterceptor)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setBaseUrl(baseUrl: String?): AnalyzeRule {
|
||||
baseUrl?.let {
|
||||
this.baseUrl = baseUrl
|
||||
@@ -861,6 +856,12 @@ class AnalyzeRule(
|
||||
private val evalPattern =
|
||||
Pattern.compile("@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}", Pattern.CASE_INSENSITIVE)
|
||||
private val regexPattern = Pattern.compile("\\$\\d{1,2}")
|
||||
|
||||
fun AnalyzeRule.setCoroutineContext(context: CoroutineContext): AnalyzeRule {
|
||||
coroutineContext = context.minusKey(ContinuationInterceptor)
|
||||
return this
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class AnalyzeUrl(
|
||||
val page: Int? = null,
|
||||
val speakText: String? = null,
|
||||
val speakSpeed: Int? = null,
|
||||
var baseUrl: String = "",
|
||||
private var baseUrl: String = "",
|
||||
private val source: BaseSource? = null,
|
||||
private val ruleData: RuleDataInterface? = null,
|
||||
private val chapter: BookChapter? = null,
|
||||
@@ -88,12 +88,6 @@ class AnalyzeUrl(
|
||||
headerMapF: Map<String, String>? = null,
|
||||
hasLoginHeader: Boolean = true
|
||||
) : JsExtensions {
|
||||
companion object {
|
||||
val paramPattern: Pattern = Pattern.compile("\\s*,\\s*(?=\\{)")
|
||||
private val pagePattern = Pattern.compile("<(.*?)>")
|
||||
private val queryEncoder =
|
||||
RFC3986.UNRESERVED.orNew(PercentCodec.of("!$%&()*+,/:;=?@[\\]^`{|}"))
|
||||
}
|
||||
|
||||
var ruleUrl = ""
|
||||
private set
|
||||
@@ -644,11 +638,6 @@ class AnalyzeUrl(
|
||||
return GlideUrl(url, GlideHeaders(headerMap))
|
||||
}
|
||||
|
||||
fun getMediaItem(): MediaItem {
|
||||
setCookie()
|
||||
return ExoPlayerHelper.createMediaItem(url, headerMap)
|
||||
}
|
||||
|
||||
fun getUserAgent(): String {
|
||||
return headerMap.get(UA_NAME, true) ?: AppConfig.userAgent
|
||||
}
|
||||
@@ -661,6 +650,19 @@ class AnalyzeUrl(
|
||||
return source
|
||||
}
|
||||
|
||||
companion object {
|
||||
val paramPattern: Pattern = Pattern.compile("\\s*,\\s*(?=\\{)")
|
||||
private val pagePattern = Pattern.compile("<(.*?)>")
|
||||
private val queryEncoder =
|
||||
RFC3986.UNRESERVED.orNew(PercentCodec.of("!$%&()*+,/:;=?@[\\]^`{|}"))
|
||||
|
||||
fun AnalyzeUrl.getMediaItem(): MediaItem {
|
||||
setCookie()
|
||||
return ExoPlayerHelper.createMediaItem(url, headerMap)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Keep
|
||||
data class UrlOption(
|
||||
private var method: String? = null,
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.legado.app.help.coroutine.Coroutine
|
||||
import io.legado.app.help.http.StrResponse
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.analyzeRule.RuleData
|
||||
import io.legado.app.utils.NetworkUtils
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.RuleData
|
||||
import io.legado.app.utils.NetworkUtils
|
||||
import splitties.init.appCtx
|
||||
|
||||
@@ -16,6 +16,7 @@ import io.legado.app.help.book.simulatedTotalChapterNum
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.isTrue
|
||||
import io.legado.app.utils.mapAsync
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.legado.app.help.book.BookHelp
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.HtmlFormatter
|
||||
import io.legado.app.utils.NetworkUtils
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.legado.app.help.book.BookHelp
|
||||
import io.legado.app.help.book.isWebFile
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.utils.DebugLog
|
||||
import io.legado.app.utils.HtmlFormatter
|
||||
import io.legado.app.utils.NetworkUtils
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.legado.app.help.book.BookHelp
|
||||
import io.legado.app.help.source.getBookType
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.analyzeRule.RuleData
|
||||
import io.legado.app.utils.HtmlFormatter
|
||||
|
||||
@@ -14,6 +14,7 @@ import io.legado.app.help.http.StrResponse
|
||||
import io.legado.app.help.source.getBookType
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.analyzeRule.RuleData
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
@@ -36,6 +36,7 @@ import io.legado.app.help.exoplayer.ExoPlayerHelper
|
||||
import io.legado.app.help.glide.ImageLoader
|
||||
import io.legado.app.model.AudioPlay
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl.Companion.getMediaItem
|
||||
import io.legado.app.receiver.MediaButtonReceiver
|
||||
import io.legado.app.ui.book.audio.AudioPlayActivity
|
||||
import io.legado.app.utils.activityPendingIntent
|
||||
|
||||
@@ -61,6 +61,7 @@ import io.legado.app.lib.theme.accentColor
|
||||
import io.legado.app.model.ReadAloud
|
||||
import io.legado.app.model.ReadBook
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule
|
||||
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
|
||||
import io.legado.app.model.localBook.EpubFile
|
||||
import io.legado.app.model.localBook.MobiFile
|
||||
import io.legado.app.receiver.NetworkChangedListener
|
||||
|
||||
Reference in New Issue
Block a user