mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
f05b057182
commit
f902b80b7c
@ -479,6 +479,10 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
}
|
||||
}
|
||||
|
||||
fun exit() {
|
||||
callBack?.exit()
|
||||
}
|
||||
|
||||
interface CallBack {
|
||||
fun upMenuView()
|
||||
|
||||
@ -495,6 +499,8 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||
fun contentLoadFinish()
|
||||
|
||||
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
|
||||
*/
|
||||
@Suppress("UnnecessaryVariable")
|
||||
@Suppress("UnnecessaryVariable", "LiftReturnOrAssignment")
|
||||
suspend fun getByteArrayAwait(): ByteArray {
|
||||
val concurrentRecord = fetchStart()
|
||||
try {
|
||||
@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)
|
||||
getByteArrayIfDataUri()?.let {
|
||||
return it
|
||||
}
|
||||
return getResponseAwait().body!!.bytes()
|
||||
}
|
||||
|
||||
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 {
|
||||
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.RssSource
|
||||
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.AnalyzeUrl
|
||||
@ -42,8 +43,9 @@ object Rss {
|
||||
ruleData = ruleData,
|
||||
headerMapF = rssSource.getHeaderMap()
|
||||
)
|
||||
val body = analyzeUrl.getStrResponseAwait().body
|
||||
return RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData)
|
||||
val res = analyzeUrl.getStrResponseAwait()
|
||||
checkRedirect(rssSource, res)
|
||||
return RssParserByRule.parseXML(sortName, sortUrl, res.body, rssSource, ruleData)
|
||||
}
|
||||
|
||||
fun getContent(
|
||||
@ -70,12 +72,26 @@ object Rss {
|
||||
ruleData = rssArticle,
|
||||
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, body, state = 20)
|
||||
Debug.log(rssSource.sourceUrl, res.body, state = 20)
|
||||
val analyzeRule = AnalyzeRule(rssArticle, rssSource)
|
||||
analyzeRule.setContent(body)
|
||||
analyzeRule.setContent(res.body)
|
||||
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
|
||||
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
|
||||
}
|
||||
}
|
||||
checkRedirect(bookSource, res)
|
||||
return BookList.analyzeBookList(
|
||||
bookSource = bookSource,
|
||||
ruleData = ruleData,
|
||||
@ -107,6 +108,7 @@ object WebBook {
|
||||
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
|
||||
}
|
||||
}
|
||||
checkRedirect(bookSource, res)
|
||||
return BookList.analyzeBookList(
|
||||
bookSource = bookSource,
|
||||
ruleData = ruleData,
|
||||
@ -162,6 +164,7 @@ object WebBook {
|
||||
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
|
||||
}
|
||||
}
|
||||
checkRedirect(bookSource, res)
|
||||
BookInfo.analyzeBookInfo(
|
||||
bookSource = bookSource,
|
||||
book = book,
|
||||
@ -238,6 +241,7 @@ object WebBook {
|
||||
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
|
||||
}
|
||||
}
|
||||
checkRedirect(bookSource, res)
|
||||
BookChapterList.analyzeChapterList(
|
||||
bookSource = bookSource,
|
||||
book = book,
|
||||
@ -311,6 +315,7 @@ object WebBook {
|
||||
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
|
||||
}
|
||||
}
|
||||
checkRedirect(bookSource, res)
|
||||
BookContent.analyzeContent(
|
||||
bookSource = bookSource,
|
||||
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)
|
||||
window.setBackgroundDrawable(null)
|
||||
upScreenTimeOut()
|
||||
ReadBook.exit()
|
||||
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) {
|
||||
binding.refreshLayout.isRefreshing = false
|
||||
if (it) {
|
||||
loadMoreView.startLoad()
|
||||
loadMoreView.stopLoad()
|
||||
} else {
|
||||
loadMoreView.noMore()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user