pull/2473/head
Horis 2 years ago
parent f05b057182
commit f902b80b7c
  1. 6
      app/src/main/java/io/legado/app/model/ReadBook.kt
  2. 65
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 26
      app/src/main/java/io/legado/app/model/rss/Rss.kt
  4. 18
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  5. 11
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  6. 2
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt

@ -479,6 +479,10 @@ object ReadBook : CoroutineScope by MainScope() {
} }
} }
fun exit() {
callBack?.exit()
}
interface CallBack { interface CallBack {
fun upMenuView() fun upMenuView()
@ -495,6 +499,8 @@ object ReadBook : CoroutineScope by MainScope() {
fun contentLoadFinish() fun contentLoadFinish()
fun upPageAnim() fun upPageAnim()
fun exit()
} }
} }

@ -463,46 +463,28 @@ class AnalyzeUrl(
} }
} }
@Suppress("UnnecessaryVariable")
private fun getByteArrayIfDataUri(): ByteArray? {
@Suppress("RegExpRedundantEscape")
val dataUriFindResult = dataUriRegex.find(urlNoQuery)
@Suppress("BlockingMethodInNonBlockingContext")
if (dataUriFindResult != null) {
val dataUriBase64 = dataUriFindResult.groupValues[1]
val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT)
return byteArray
}
return null
}
/** /**
* 访问网站,返回ByteArray * 访问网站,返回ByteArray
*/ */
@Suppress("UnnecessaryVariable") @Suppress("UnnecessaryVariable", "LiftReturnOrAssignment")
suspend fun getByteArrayAwait(): ByteArray { suspend fun getByteArrayAwait(): ByteArray {
val concurrentRecord = fetchStart() getByteArrayIfDataUri()?.let {
try { return it
@Suppress("RegExpRedundantEscape")
val dataUriFindResult = dataUriRegex.find(urlNoQuery)
@Suppress("BlockingMethodInNonBlockingContext")
if (dataUriFindResult != null) {
val dataUriBase64 = dataUriFindResult.groupValues[1]
val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT)
return byteArray
} else {
setCookie(source?.getKey())
val byteArray = getProxyClient(proxy).newCallResponseBody(retry) {
addHeaders(headerMap)
when (method) {
RequestMethod.POST -> {
url(urlNoQuery)
val contentType = headerMap["Content-Type"]
val body = body
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
postForm(fieldMap, true)
} else if (!contentType.isNullOrBlank()) {
val requestBody = body.toRequestBody(contentType.toMediaType())
post(requestBody)
} else {
postJson(body)
}
}
else -> get(urlNoQuery, fieldMap, true)
}
}.bytes()
return byteArray
}
} finally {
fetchEnd(concurrentRecord)
} }
return getResponseAwait().body!!.bytes()
} }
fun getByteArray(): ByteArray { fun getByteArray(): ByteArray {
@ -511,9 +493,20 @@ class AnalyzeUrl(
} }
} }
/**
* 访问网站,返回InputStream
*/
@Suppress("LiftReturnOrAssignment")
suspend fun getInputStreamAwait(): InputStream {
getByteArrayIfDataUri()?.let {
return ByteArrayInputStream(it)
}
return getResponseAwait().body!!.byteStream()
}
fun getInputStream(): InputStream { fun getInputStream(): InputStream {
return runBlocking { return runBlocking {
getResponseAwait().body!!.byteStream() getInputStreamAwait()
} }
} }

@ -3,6 +3,7 @@ package io.legado.app.model.rss
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.coroutine.Coroutine 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.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
@ -42,8 +43,9 @@ object Rss {
ruleData = ruleData, ruleData = ruleData,
headerMapF = rssSource.getHeaderMap() headerMapF = rssSource.getHeaderMap()
) )
val body = analyzeUrl.getStrResponseAwait().body val res = analyzeUrl.getStrResponseAwait()
return RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData) checkRedirect(rssSource, res)
return RssParserByRule.parseXML(sortName, sortUrl, res.body, rssSource, ruleData)
} }
fun getContent( fun getContent(
@ -70,12 +72,26 @@ object Rss {
ruleData = rssArticle, ruleData = rssArticle,
headerMapF = rssSource.getHeaderMap() headerMapF = rssSource.getHeaderMap()
) )
val body = analyzeUrl.getStrResponseAwait().body val res = analyzeUrl.getStrResponseAwait()
checkRedirect(rssSource, res)
Debug.log(rssSource.sourceUrl, "≡获取成功:${rssSource.sourceUrl}") Debug.log(rssSource.sourceUrl, "≡获取成功:${rssSource.sourceUrl}")
Debug.log(rssSource.sourceUrl, body, state = 20) Debug.log(rssSource.sourceUrl, res.body, state = 20)
val analyzeRule = AnalyzeRule(rssArticle, rssSource) val analyzeRule = AnalyzeRule(rssArticle, rssSource)
analyzeRule.setContent(body) analyzeRule.setContent(res.body)
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link)) .setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
return analyzeRule.getString(ruleContent) return analyzeRule.getString(ruleContent)
} }
/**
* 检测重定向
*/
private fun checkRedirect(rssSource: RssSource, response: StrResponse) {
response.raw.priorResponse?.let {
if (it.isRedirect) {
Debug.log(rssSource.sourceUrl, "≡检测到重定向(${it.code})")
Debug.log(rssSource.sourceUrl, "┌重定向后地址")
Debug.log(rssSource.sourceUrl, "${response.url}")
}
}
}
} }

@ -59,6 +59,7 @@ object WebBook {
res = analyzeUrl.evalJS(checkJs, res) as StrResponse res = analyzeUrl.evalJS(checkJs, res) as StrResponse
} }
} }
checkRedirect(bookSource, res)
return BookList.analyzeBookList( return BookList.analyzeBookList(
bookSource = bookSource, bookSource = bookSource,
ruleData = ruleData, ruleData = ruleData,
@ -107,6 +108,7 @@ object WebBook {
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
} }
} }
checkRedirect(bookSource, res)
return BookList.analyzeBookList( return BookList.analyzeBookList(
bookSource = bookSource, bookSource = bookSource,
ruleData = ruleData, ruleData = ruleData,
@ -162,6 +164,7 @@ object WebBook {
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
} }
} }
checkRedirect(bookSource, res)
BookInfo.analyzeBookInfo( BookInfo.analyzeBookInfo(
bookSource = bookSource, bookSource = bookSource,
book = book, book = book,
@ -238,6 +241,7 @@ object WebBook {
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
} }
} }
checkRedirect(bookSource, res)
BookChapterList.analyzeChapterList( BookChapterList.analyzeChapterList(
bookSource = bookSource, bookSource = bookSource,
book = book, book = book,
@ -311,6 +315,7 @@ object WebBook {
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
} }
} }
checkRedirect(bookSource, res)
BookContent.analyzeContent( BookContent.analyzeContent(
bookSource = bookSource, bookSource = bookSource,
book = book, book = book,
@ -374,4 +379,17 @@ object WebBook {
} }
} }
/**
* 检测重定向
*/
private fun checkRedirect(bookSource: BookSource, response: StrResponse) {
response.raw.priorResponse?.let {
if (it.isRedirect) {
Debug.log(bookSource.bookSourceUrl, "≡检测到重定向(${it.code})")
Debug.log(bookSource.bookSourceUrl, "┌重定向后地址")
Debug.log(bookSource.bookSourceUrl, "${response.url}")
}
}
}
} }

@ -180,6 +180,7 @@ class ReadBookActivity : BaseReadBookActivity(),
binding.cursorRight.setOnTouchListener(this) binding.cursorRight.setOnTouchListener(this)
window.setBackgroundDrawable(null) window.setBackgroundDrawable(null)
upScreenTimeOut() upScreenTimeOut()
ReadBook.exit()
ReadBook.callBack = this ReadBook.callBack = this
} }
@ -785,6 +786,16 @@ class ReadBookActivity : BaseReadBookActivity(),
} }
} }
override fun exit() {
ReadBook.book?.let {
if (!ReadBook.inBookshelf) {
viewModel.removeFromBookshelf { super.finish() }
} else {
super.finish()
}
} ?: super.finish()
}
/** /**
* 页面改变 * 页面改变
*/ */

@ -128,7 +128,7 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
viewModel.loadFinallyLiveData.observe(viewLifecycleOwner) { viewModel.loadFinallyLiveData.observe(viewLifecycleOwner) {
binding.refreshLayout.isRefreshing = false binding.refreshLayout.isRefreshing = false
if (it) { if (it) {
loadMoreView.startLoad() loadMoreView.stopLoad()
} else { } else {
loadMoreView.noMore() loadMoreView.noMore()
} }

Loading…
Cancel
Save