搜索url支持put,get,js里使用java.put,java.get

pull/354/head
gedoor 4 years ago
parent 82f8c15f09
commit a635661236
  1. 11
      app/src/main/java/io/legado/app/model/Debug.kt
  2. 11
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 4
      app/src/main/java/io/legado/app/model/webBook/BookList.kt
  4. 12
      app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt
  5. 16
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  6. 5
      app/src/main/java/io/legado/app/service/help/CheckSource.kt
  7. 4
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  8. 4
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  9. 3
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt
  10. 11
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.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, "︽搜索页解析完成")

@ -39,7 +39,7 @@ class AnalyzeUrl(
speakSpeed: Int? = null,
headerMapF: Map<String, String>? = 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<String> {
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {

@ -25,7 +25,8 @@ object BookList {
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
baseUrl: String,
isSearch: Boolean = true
variableBook: SearchBook,
isSearch: Boolean = true,
): ArrayList<SearchBook> {
val bookList = ArrayList<SearchBook>()
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 {

@ -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<BookSource>()
private val variableBookMap = hashMapOf<String, SearchBook>()
@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)

@ -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<ArrayList<SearchBook>> {
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<SearchBook> {
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<List<SearchBook>> {
return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl(
@ -79,6 +84,7 @@ class WebBook(val bookSource: BookSource) {
bookSource,
analyzeUrl,
res.url,
variableBook,
false
)
}

@ -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("失效")

@ -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()) {

@ -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 ->

@ -14,6 +14,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
val booksData = MutableLiveData<List<SearchBook>>()
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)

@ -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<BookChapter>) -> Unit)? = null
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null,
) {
execute {
if (book.isLocalBook()) {
@ -114,7 +115,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun loadChapterList(
book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null,
) {
execute {
if (book.isLocalBook()) {
@ -182,8 +183,10 @@ 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 {
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

Loading…
Cancel
Save