From f902b80b7cb51dc4c69664662eb866c50997079f Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sun, 23 Oct 2022 21:32:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/ReadBook.kt | 6 ++ .../app/model/analyzeRule/AnalyzeUrl.kt | 65 +++++++++---------- .../main/java/io/legado/app/model/rss/Rss.kt | 26 ++++++-- .../io/legado/app/model/webBook/WebBook.kt | 18 +++++ .../app/ui/book/read/ReadBookActivity.kt | 11 ++++ .../app/ui/rss/article/RssArticlesFragment.kt | 2 +- 6 files changed, 86 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index 6060a6af6..ed749c688 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -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() } } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index ff2ebb698..0651d22e3 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -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() } } diff --git a/app/src/main/java/io/legado/app/model/rss/Rss.kt b/app/src/main/java/io/legado/app/model/rss/Rss.kt index 25a989cd6..7863ba088 100644 --- a/app/src/main/java/io/legado/app/model/rss/Rss.kt +++ b/app/src/main/java/io/legado/app/model/rss/Rss.kt @@ -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}") + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 39ab71c99..7e4dbfbd0 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -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}") + } + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 2fba05c45..92c6f3422 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -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() + } + /** * 页面改变 */ diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt index b232f147b..1e2e09e8e 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt @@ -128,7 +128,7 @@ class RssArticlesFragment() : VMBaseFragment(R.layout.frag viewModel.loadFinallyLiveData.observe(viewLifecycleOwner) { binding.refreshLayout.isRefreshing = false if (it) { - loadMoreView.startLoad() + loadMoreView.stopLoad() } else { loadMoreView.noMore() }