pull/1296/head
gedoor 3 years ago
parent db068853f9
commit b9ec61989d
  1. 8
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 36
      app/src/main/java/io/legado/app/model/ReadBook.kt
  3. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  4. 19
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  5. 6
      app/src/main/java/io/legado/app/model/rss/Rss.kt
  6. 4
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  7. 4
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  8. 9
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt

@ -40,7 +40,7 @@ interface JsExtensions {
return runBlocking { return runBlocking {
kotlin.runCatching { kotlin.runCatching {
val analyzeUrl = AnalyzeUrl(urlStr) val analyzeUrl = AnalyzeUrl(urlStr)
analyzeUrl.getStrResponse(urlStr).body analyzeUrl.getStrResponse().body
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
}.getOrElse { }.getOrElse {
@ -58,7 +58,7 @@ interface JsExtensions {
async(IO) { async(IO) {
val url = urlList[it] val url = urlList[it]
val analyzeUrl = AnalyzeUrl(url) val analyzeUrl = AnalyzeUrl(url)
analyzeUrl.getStrResponse(url) analyzeUrl.getStrResponse()
} }
} }
val resArray = Array<StrResponse?>(urlList.size) { val resArray = Array<StrResponse?>(urlList.size) {
@ -75,7 +75,7 @@ interface JsExtensions {
return runBlocking { return runBlocking {
val analyzeUrl = AnalyzeUrl(urlStr) val analyzeUrl = AnalyzeUrl(urlStr)
kotlin.runCatching { kotlin.runCatching {
analyzeUrl.getStrResponse(urlStr) analyzeUrl.getStrResponse()
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
}.getOrElse { }.getOrElse {
@ -89,7 +89,7 @@ interface JsExtensions {
val headerMap = GSON.fromJsonObject<Map<String, String>>(header) val headerMap = GSON.fromJsonObject<Map<String, String>>(header)
val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap) val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap)
kotlin.runCatching { kotlin.runCatching {
analyzeUrl.getStrResponse(urlStr) analyzeUrl.getStrResponse()
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
}.getOrElse { }.getOrElse {

@ -136,14 +136,6 @@ object ReadBook : CoroutineScope by MainScope() {
saveRead() saveRead()
callBack?.upView() callBack?.upView()
curPageChanged() curPageChanged()
Coroutine.async {
//预下载
val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum
for (i in durChapterIndex.plus(2)..maxChapterIndex) {
delay(1000)
download(i)
}
}
return true return true
} else { } else {
return false return false
@ -169,14 +161,6 @@ object ReadBook : CoroutineScope by MainScope() {
saveRead() saveRead()
callBack?.upView() callBack?.upView()
curPageChanged() curPageChanged()
Coroutine.async {
//预下载
val minChapterIndex = durChapterIndex - 5
for (i in durChapterIndex.minus(2) downTo minChapterIndex) {
delay(1000)
download(i)
}
}
return true return true
} else { } else {
return false return false
@ -207,6 +191,7 @@ object ReadBook : CoroutineScope by MainScope() {
readAloud(!BaseReadAloudService.pause) readAloud(!BaseReadAloudService.pause)
} }
upReadStartTime() upReadStartTime()
preDownload()
} }
/** /**
@ -436,6 +421,25 @@ object ReadBook : CoroutineScope by MainScope() {
} }
} }
/**
* 预下载
*/
private fun preDownload() {
Coroutine.async {
//预下载
val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum
for (i in durChapterIndex.plus(2)..maxChapterIndex) {
delay(1000)
download(i)
}
val minChapterIndex = durChapterIndex - 5
for (i in durChapterIndex.minus(2) downTo minChapterIndex) {
delay(1000)
download(i)
}
}
}
interface CallBack { interface CallBack {
fun loadChapterList(book: Book) fun loadChapterList(book: Book)

@ -659,7 +659,7 @@ class AnalyzeRule(
return runBlocking { return runBlocking {
kotlin.runCatching { kotlin.runCatching {
val analyzeUrl = AnalyzeUrl(urlStr, book = book, source = source) val analyzeUrl = AnalyzeUrl(urlStr, book = book, source = source)
analyzeUrl.getStrResponse(urlStr).body analyzeUrl.getStrResponse().body
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
}.getOrElse { }.getOrElse {

@ -68,13 +68,17 @@ class AnalyzeUrl(
headerMap.remove("proxy") headerMap.remove("proxy")
} }
} }
//替换参数 //执行@js,<js></js>
analyzeJs() analyzeJs()
//替换参数
replaceKeyPageJs() replaceKeyPageJs()
//处理URL //处理URL
initUrl() initUrl()
} }
/**
* 执行@js,<js></js>
*/
private fun analyzeJs() { private fun analyzeJs() {
var start = 0 var start = 0
var tmp: String var tmp: String
@ -104,9 +108,7 @@ class AnalyzeUrl(
private fun replaceKeyPageJs() { //先替换内嵌规则再替换页数规则,避免内嵌规则中存在大于小于号时,规则被切错 private fun replaceKeyPageJs() { //先替换内嵌规则再替换页数规则,避免内嵌规则中存在大于小于号时,规则被切错
//js //js
if (ruleUrl.contains("{{") && ruleUrl.contains("}}")) { if (ruleUrl.contains("{{") && ruleUrl.contains("}}")) {
val analyze = RuleAnalyzer(ruleUrl) //创建解析 val analyze = RuleAnalyzer(ruleUrl) //创建解析
//替换所有内嵌{{js}} //替换所有内嵌{{js}}
val url = analyze.innerRule("{{", "}}") { val url = analyze.innerRule("{{", "}}") {
val jsEval = evalJS(it) ?: "" val jsEval = evalJS(it) ?: ""
@ -262,14 +264,13 @@ class AnalyzeUrl(
} }
suspend fun getStrResponse( suspend fun getStrResponse(
tag: String,
jsStr: String? = null, jsStr: String? = null,
sourceRegex: String? = null, sourceRegex: String? = null,
): StrResponse { ): StrResponse {
if (type != null) { if (type != null) {
return StrResponse(url, StringUtils.byteToHexString(getByteArray(tag))) return StrResponse(url, StringUtils.byteToHexString(getByteArray()))
} }
setCookie(tag) setCookie(source?.getStoreUrl())
if (useWebView) { if (useWebView) {
val params = AjaxWebView.AjaxParams(url) val params = AjaxWebView.AjaxParams(url)
params.headerMap = headerMap params.headerMap = headerMap
@ -277,7 +278,7 @@ class AnalyzeUrl(
params.javaScript = jsStr params.javaScript = jsStr
params.sourceRegex = sourceRegex params.sourceRegex = sourceRegex
params.postData = body?.toByteArray() params.postData = body?.toByteArray()
params.tag = tag params.tag = source?.getStoreUrl()
return getWebViewSrc(params) return getWebViewSrc(params)
} }
return getProxyClient(proxy).newCallStrResponse(retry) { return getProxyClient(proxy).newCallStrResponse(retry) {
@ -296,8 +297,8 @@ class AnalyzeUrl(
} }
} }
suspend fun getByteArray(tag: String? = null): ByteArray { suspend fun getByteArray(): ByteArray {
setCookie(tag) setCookie(source?.getStoreUrl())
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
return getProxyClient(proxy).newCall(retry) { return getProxyClient(proxy).newCall(retry) {
addHeaders(headerMap) addHeaders(headerMap)

@ -39,9 +39,10 @@ object Rss {
sortUrl, sortUrl,
page = page, page = page,
ruleData = ruleData, ruleData = ruleData,
source = rssSource,
headerMapF = rssSource.getHeaderMap() headerMapF = rssSource.getHeaderMap()
) )
val body = analyzeUrl.getStrResponse(rssSource.sourceUrl).body val body = analyzeUrl.getStrResponse().body
return RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData) return RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData)
} }
@ -66,9 +67,10 @@ object Rss {
rssArticle.link, rssArticle.link,
baseUrl = rssArticle.origin, baseUrl = rssArticle.origin,
ruleData = rssArticle, ruleData = rssArticle,
source = rssSource,
headerMapF = rssSource.getHeaderMap() headerMapF = rssSource.getHeaderMap()
) )
val body = analyzeUrl.getStrResponse(rssArticle.origin).body val body = analyzeUrl.getStrResponse().body
Debug.log(rssSource.sourceUrl, "≡获取成功:${rssSource.sourceUrl}") Debug.log(rssSource.sourceUrl, "≡获取成功:${rssSource.sourceUrl}")
Debug.log(rssSource.sourceUrl, body, state = 20) Debug.log(rssSource.sourceUrl, body, state = 20)
val analyzeRule = AnalyzeRule(rssArticle, rssSource) val analyzeRule = AnalyzeRule(rssArticle, rssSource)

@ -61,7 +61,7 @@ object BookChapterList {
book = book, book = book,
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
).getStrResponse(bookSource.bookSourceUrl).body?.let { nextBody -> ).getStrResponse().body?.let { nextBody ->
chapterData = analyzeChapterList( chapterData = analyzeChapterList(
scope, book, nextUrl, nextUrl, scope, book, nextUrl, nextUrl,
nextBody, tocRule, listRule, bookSource nextBody, tocRule, listRule, bookSource
@ -84,7 +84,7 @@ object BookChapterList {
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) val res = analyzeUrl.getStrResponse()
analyzeChapterList( analyzeChapterList(
this, book, urlStr, res.url, this, book, urlStr, res.url,
res.body!!, tocRule, listRule, bookSource, false res.body!!, tocRule, listRule, bookSource, false

@ -68,7 +68,7 @@ object BookContent {
book = book, book = book,
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
).getStrResponse(bookSource.bookSourceUrl) ).getStrResponse()
res.body?.let { nextBody -> res.body?.let { nextBody ->
contentData = analyzeContent( contentData = analyzeContent(
book, nextUrl, res.url, nextBody, contentRule, book, nextUrl, res.url, nextBody, contentRule,
@ -92,7 +92,7 @@ object BookContent {
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) val res = analyzeUrl.getStrResponse()
analyzeContent( analyzeContent(
book, urlStr, res.url, res.body!!, contentRule, book, urlStr, res.url, res.body!!, contentRule,
bookChapter, bookSource, mNextChapterUrl, false bookChapter, bookSource, mNextChapterUrl, false

@ -74,7 +74,7 @@ object WebBook {
book = variableBook, book = variableBook,
source = bookSource source = bookSource
) )
var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) var res = analyzeUrl.getStrResponse()
//检测书源是否已登录 //检测书源是否已登录
bookSource.loginCheckJs?.let { checkJs -> bookSource.loginCheckJs?.let { checkJs ->
if (checkJs.isNotBlank()) { if (checkJs.isNotBlank()) {
@ -124,7 +124,7 @@ object WebBook {
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap(true) headerMapF = bookSource.getHeaderMap(true)
) )
var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) var res = analyzeUrl.getStrResponse()
//检测书源是否已登录 //检测书源是否已登录
bookSource.loginCheckJs?.let { checkJs -> bookSource.loginCheckJs?.let { checkJs ->
if (checkJs.isNotBlank()) { if (checkJs.isNotBlank()) {
@ -182,7 +182,7 @@ object WebBook {
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap(true) headerMapF = bookSource.getHeaderMap(true)
) )
var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) var res = analyzeUrl.getStrResponse()
//检测书源是否已登录 //检测书源是否已登录
bookSource.loginCheckJs?.let { checkJs -> bookSource.loginCheckJs?.let { checkJs ->
if (checkJs.isNotBlank()) { if (checkJs.isNotBlank()) {
@ -239,7 +239,7 @@ object WebBook {
source = bookSource, source = bookSource,
headerMapF = bookSource.getHeaderMap(true) headerMapF = bookSource.getHeaderMap(true)
) )
var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) var res = analyzeUrl.getStrResponse()
//检测书源是否已登录 //检测书源是否已登录
bookSource.loginCheckJs?.let { checkJs -> bookSource.loginCheckJs?.let { checkJs ->
if (checkJs.isNotBlank()) { if (checkJs.isNotBlank()) {
@ -305,7 +305,6 @@ object WebBook {
headerMapF = bookSource.getHeaderMap(true) headerMapF = bookSource.getHeaderMap(true)
) )
var res = analyzeUrl.getStrResponse( var res = analyzeUrl.getStrResponse(
bookSource.bookSourceUrl,
jsStr = bookSource.getContentRule().webJs, jsStr = bookSource.getContentRule().webJs,
sourceRegex = bookSource.getContentRule().sourceRegex sourceRegex = bookSource.getContentRule().sourceRegex
) )

Loading…
Cancel
Save