From a635661236d3ee162e81ca6fb04c00a68389cd7d Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 1 Sep 2020 17:57:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2url=E6=94=AF=E6=8C=81put,get,?= =?UTF-8?q?js=E9=87=8C=E4=BD=BF=E7=94=A8java.put,java.get?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/Debug.kt | 11 +++++------ .../app/model/analyzeRule/AnalyzeUrl.kt | 11 ++++++++++- .../io/legado/app/model/webBook/BookList.kt | 4 ++-- .../app/model/webBook/SearchBookModel.kt | 12 ++++++++++++ .../io/legado/app/model/webBook/WebBook.kt | 16 +++++++++++----- .../io/legado/app/service/help/CheckSource.kt | 5 ++++- .../book/changecover/ChangeCoverViewModel.kt | 4 +++- .../changesource/ChangeSourceViewModel.kt | 4 +++- .../ui/book/explore/ExploreShowViewModel.kt | 3 ++- .../app/ui/book/read/ReadBookViewModel.kt | 19 +++++++++++-------- 10 files changed, 63 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index 795170985..77926b138 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -1,10 +1,7 @@ package io.legado.app.model import android.annotation.SuppressLint -import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookChapter -import io.legado.app.data.entities.RssArticle -import io.legado.app.data.entities.RssSource +import io.legado.app.data.entities.* import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.rss.Rss import io.legado.app.model.webBook.WebBook @@ -139,7 +136,8 @@ object Debug { private fun exploreDebug(webBook: WebBook, url: String) { log(debugSource, "︾开始解析发现页") - val explore = webBook.exploreBook(url, 1) + val variableBook = SearchBook() + val explore = webBook.exploreBook(url, 1, variableBook) .onSuccess { exploreBooks -> if (exploreBooks.isNotEmpty()) { log(debugSource, "︽发现页解析完成") @@ -161,7 +159,8 @@ object Debug { private fun searchDebug(webBook: WebBook, key: String) { log(debugSource, "︾开始解析搜索页") - val search = webBook.searchBook(key, 1) + val variableBook = SearchBook() + val search = webBook.searchBook(key, 1, variableBook) .onSuccess { searchBooks -> if (searchBooks.isNotEmpty()) { log(debugSource, "︽搜索页解析完成") 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 16cd607d3..653c3b0a1 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 @@ -39,7 +39,7 @@ class AnalyzeUrl( speakSpeed: Int? = null, headerMapF: Map? = null, baseUrl: String? = null, - book: BaseBook? = null, + val book: BaseBook? = null, var useWebView: Boolean = false, ) : JsExtensions { companion object { @@ -285,6 +285,15 @@ class AnalyzeUrl( return SCRIPT_ENGINE.eval(jsStr, bindings) } + fun put(key: String, value: String): String { + book?.putVariable(key, value) + return value + } + + fun get(key: String): String { + return book?.variableMap?.get(key) ?: "" + } + fun getResponse(tag: String): Call { val cookie = CookieStore.getCookie(tag) if (cookie.isNotEmpty()) { diff --git a/app/src/main/java/io/legado/app/model/webBook/BookList.kt b/app/src/main/java/io/legado/app/model/webBook/BookList.kt index 85e5e87c0..05570b6ff 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookList.kt @@ -25,7 +25,8 @@ object BookList { bookSource: BookSource, analyzeUrl: AnalyzeUrl, baseUrl: String, - isSearch: Boolean = true + variableBook: SearchBook, + isSearch: Boolean = true, ): ArrayList { val bookList = ArrayList() body ?: throw Exception( @@ -36,7 +37,6 @@ object BookList { ) Debug.log(bookSource.bookSourceUrl, "≡获取成功:${analyzeUrl.ruleUrl}") if (!scope.isActive) throw CancellationException() - val variableBook = SearchBook() val analyzeRule = AnalyzeRule(variableBook) analyzeRule.setContent(body, baseUrl) bookSource.bookUrlPattern?.let { diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt index 1224979af..9c257a7db 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt +++ b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt @@ -21,6 +21,7 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C private var searchKey: String = "" private var tasks = CompositeCoroutine() private var bookSourceList = arrayListOf() + private val variableBookMap = hashMapOf() @Volatile private var searchIndex = -1 @@ -60,6 +61,15 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C } } + private fun getVariableBook(sourceUrl: String): SearchBook { + var vBook = variableBookMap[sourceUrl] + if (vBook == null) { + vBook = SearchBook() + variableBookMap[sourceUrl] = vBook + } + return vBook + } + private fun search(searchId: Long) { synchronized(this) { if (searchIndex >= bookSourceList.lastIndex) { @@ -67,9 +77,11 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C } searchIndex++ val source = bookSourceList[searchIndex] + val variableBook = getVariableBook(source.bookSourceUrl) val task = WebBook(source).searchBook( searchKey, searchPage, + variableBook, scope = scope, context = searchPool!! ).timeout(30000L) 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 22b75ed84..bcc087839 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 @@ -22,18 +22,20 @@ class WebBook(val bookSource: BookSource) { fun searchBook( key: String, page: Int? = 1, + variableBook: SearchBook, scope: CoroutineScope = Coroutine.DEFAULT, - context: CoroutineContext = Dispatchers.IO + context: CoroutineContext = Dispatchers.IO, ): Coroutine> { return Coroutine.async(scope, context) { - searchBookSuspend(scope, key, page) + searchBookSuspend(scope, key, page, variableBook) } } suspend fun searchBookSuspend( scope: CoroutineScope, key: String, - page: Int? = 1 + page: Int? = 1, + variableBook: SearchBook, ): ArrayList { bookSource.searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl( @@ -41,7 +43,8 @@ class WebBook(val bookSource: BookSource) { key = key, page = page, baseUrl = sourceUrl, - headerMapF = bookSource.getHeaderMap() + headerMapF = bookSource.getHeaderMap(), + book = variableBook ) val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl) return BookList.analyzeBookList( @@ -50,6 +53,7 @@ class WebBook(val bookSource: BookSource) { bookSource, analyzeUrl, res.url, + variableBook, true ) } @@ -62,8 +66,9 @@ class WebBook(val bookSource: BookSource) { fun exploreBook( url: String, page: Int? = 1, + variableBook: SearchBook, scope: CoroutineScope = Coroutine.DEFAULT, - context: CoroutineContext = Dispatchers.IO + context: CoroutineContext = Dispatchers.IO, ): Coroutine> { return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl( @@ -79,6 +84,7 @@ class WebBook(val bookSource: BookSource) { bookSource, analyzeUrl, res.url, + variableBook, false ) } diff --git a/app/src/main/java/io/legado/app/service/help/CheckSource.kt b/app/src/main/java/io/legado/app/service/help/CheckSource.kt index 7ae99e667..352c4b799 100644 --- a/app/src/main/java/io/legado/app/service/help/CheckSource.kt +++ b/app/src/main/java/io/legado/app/service/help/CheckSource.kt @@ -6,6 +6,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.constant.IntentAction import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.webBook.WebBook import io.legado.app.service.CheckSourceService @@ -49,7 +50,9 @@ class CheckSource(val source: BookSource) { onNext: (sourceUrl: String) -> Unit ): Coroutine<*> { val webBook = WebBook(source) - return webBook.searchBook(keyword, scope = scope, context = context) + val variableBook = SearchBook(origin = source.bookSourceUrl) + return webBook + .searchBook(keyword, scope = scope, context = context, variableBook = variableBook) .timeout(60000L) .onError(Dispatchers.IO) { source.addGroup("失效") diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 8a9e7ebe0..fc8f6adf5 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -80,7 +80,9 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application } searchIndex++ val source = bookSourceList[searchIndex] - val task = WebBook(source).searchBook(name, scope = this, context = searchPool!!) + val variableBook = SearchBook(origin = source.bookSourceUrl) + val task = WebBook(source) + .searchBook(name, scope = this, context = searchPool!!, variableBook = variableBook) .timeout(60000L) .onSuccess(Dispatchers.IO) { if (it.isNotEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 6ce62b9f2..ec423c3dd 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -113,7 +113,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } searchIndex++ val source = bookSourceList[searchIndex] - val task = WebBook(source).searchBook(name, scope = this, context = searchPool!!) + val variableBook = SearchBook() + val task = WebBook(source) + .searchBook(name, variableBook = variableBook, scope = this, context = searchPool!!) .timeout(60000L) .onSuccess(IO) { it.forEach { searchBook -> diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index 9e7b8dd11..f2d3c2ef8 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt @@ -14,6 +14,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application val booksData = MutableLiveData>() private var bookSource: BookSource? = null + private val variableBook = SearchBook() private var exploreUrl: String? = null private var page = 1 @@ -32,7 +33,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application val source = bookSource val url = exploreUrl if (source != null && url != null) { - WebBook(source).exploreBook(url, page, this) + WebBook(source).exploreBook(url, page, variableBook, this) .timeout(30000L) .onSuccess(IO) { searchBooks -> booksData.postValue(searchBooks) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index a23b56bb1..908a968af 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -7,6 +7,7 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.SearchBook import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.IntentDataHelp @@ -98,7 +99,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { private fun loadBookInfo( book: Book, - changeDruChapterIndex: ((chapters: List) -> Unit)? = null + changeDruChapterIndex: ((chapters: List) -> Unit)? = null, ) { execute { if (book.isLocalBook()) { @@ -114,7 +115,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun loadChapterList( book: Book, - changeDruChapterIndex: ((chapters: List) -> Unit)? = null + changeDruChapterIndex: ((chapters: List) -> Unit)? = null, ) { execute { if (book.isLocalBook()) { @@ -182,13 +183,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { App.db.bookSourceDao().allTextEnabled.forEach { source -> try { - val searchBooks = WebBook(source).searchBookSuspend(this, name) - searchBooks.getOrNull(0)?.let { - if (it.name == name && (it.author == author || author == "")) { - changeTo(it.toBook()) - return@forEach + val variableBook = SearchBook() + WebBook(source) + .searchBookSuspend(this, name, variableBook = variableBook) + .getOrNull(0)?.let { + if (it.name == name && (it.author == author || author == "")) { + changeTo(it.toBook()) + return@forEach + } } - } } catch (e: Exception) { //nothing }