搜索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. 19
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -1,10 +1,7 @@
package io.legado.app.model package io.legado.app.model
import android.annotation.SuppressLint import android.annotation.SuppressLint
import io.legado.app.data.entities.Book import io.legado.app.data.entities.*
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.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
@ -139,7 +136,8 @@ object Debug {
private fun exploreDebug(webBook: WebBook, url: String) { private fun exploreDebug(webBook: WebBook, url: String) {
log(debugSource, "︾开始解析发现页") log(debugSource, "︾开始解析发现页")
val explore = webBook.exploreBook(url, 1) val variableBook = SearchBook()
val explore = webBook.exploreBook(url, 1, variableBook)
.onSuccess { exploreBooks -> .onSuccess { exploreBooks ->
if (exploreBooks.isNotEmpty()) { if (exploreBooks.isNotEmpty()) {
log(debugSource, "︽发现页解析完成") log(debugSource, "︽发现页解析完成")
@ -161,7 +159,8 @@ object Debug {
private fun searchDebug(webBook: WebBook, key: String) { private fun searchDebug(webBook: WebBook, key: String) {
log(debugSource, "︾开始解析搜索页") log(debugSource, "︾开始解析搜索页")
val search = webBook.searchBook(key, 1) val variableBook = SearchBook()
val search = webBook.searchBook(key, 1, variableBook)
.onSuccess { searchBooks -> .onSuccess { searchBooks ->
if (searchBooks.isNotEmpty()) { if (searchBooks.isNotEmpty()) {
log(debugSource, "︽搜索页解析完成") log(debugSource, "︽搜索页解析完成")

@ -39,7 +39,7 @@ class AnalyzeUrl(
speakSpeed: Int? = null, speakSpeed: Int? = null,
headerMapF: Map<String, String>? = null, headerMapF: Map<String, String>? = null,
baseUrl: String? = null, baseUrl: String? = null,
book: BaseBook? = null, val book: BaseBook? = null,
var useWebView: Boolean = false, var useWebView: Boolean = false,
) : JsExtensions { ) : JsExtensions {
companion object { companion object {
@ -285,6 +285,15 @@ class AnalyzeUrl(
return SCRIPT_ENGINE.eval(jsStr, bindings) 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> { fun getResponse(tag: String): Call<String> {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {

@ -25,7 +25,8 @@ object BookList {
bookSource: BookSource, bookSource: BookSource,
analyzeUrl: AnalyzeUrl, analyzeUrl: AnalyzeUrl,
baseUrl: String, baseUrl: String,
isSearch: Boolean = true variableBook: SearchBook,
isSearch: Boolean = true,
): ArrayList<SearchBook> { ): ArrayList<SearchBook> {
val bookList = ArrayList<SearchBook>() val bookList = ArrayList<SearchBook>()
body ?: throw Exception( body ?: throw Exception(
@ -36,7 +37,6 @@ object BookList {
) )
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${analyzeUrl.ruleUrl}") Debug.log(bookSource.bookSourceUrl, "≡获取成功:${analyzeUrl.ruleUrl}")
if (!scope.isActive) throw CancellationException() if (!scope.isActive) throw CancellationException()
val variableBook = SearchBook()
val analyzeRule = AnalyzeRule(variableBook) val analyzeRule = AnalyzeRule(variableBook)
analyzeRule.setContent(body, baseUrl) analyzeRule.setContent(body, baseUrl)
bookSource.bookUrlPattern?.let { bookSource.bookUrlPattern?.let {

@ -21,6 +21,7 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
private var searchKey: String = "" private var searchKey: String = ""
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private var bookSourceList = arrayListOf<BookSource>() private var bookSourceList = arrayListOf<BookSource>()
private val variableBookMap = hashMapOf<String, SearchBook>()
@Volatile @Volatile
private var searchIndex = -1 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) { private fun search(searchId: Long) {
synchronized(this) { synchronized(this) {
if (searchIndex >= bookSourceList.lastIndex) { if (searchIndex >= bookSourceList.lastIndex) {
@ -67,9 +77,11 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
} }
searchIndex++ searchIndex++
val source = bookSourceList[searchIndex] val source = bookSourceList[searchIndex]
val variableBook = getVariableBook(source.bookSourceUrl)
val task = WebBook(source).searchBook( val task = WebBook(source).searchBook(
searchKey, searchKey,
searchPage, searchPage,
variableBook,
scope = scope, scope = scope,
context = searchPool!! context = searchPool!!
).timeout(30000L) ).timeout(30000L)

@ -22,18 +22,20 @@ class WebBook(val bookSource: BookSource) {
fun searchBook( fun searchBook(
key: String, key: String,
page: Int? = 1, page: Int? = 1,
variableBook: SearchBook,
scope: CoroutineScope = Coroutine.DEFAULT, scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO,
): Coroutine<ArrayList<SearchBook>> { ): Coroutine<ArrayList<SearchBook>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
searchBookSuspend(scope, key, page) searchBookSuspend(scope, key, page, variableBook)
} }
} }
suspend fun searchBookSuspend( suspend fun searchBookSuspend(
scope: CoroutineScope, scope: CoroutineScope,
key: String, key: String,
page: Int? = 1 page: Int? = 1,
variableBook: SearchBook,
): ArrayList<SearchBook> { ): ArrayList<SearchBook> {
bookSource.searchUrl?.let { searchUrl -> bookSource.searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
@ -41,7 +43,8 @@ class WebBook(val bookSource: BookSource) {
key = key, key = key,
page = page, page = page,
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap(),
book = variableBook
) )
val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl) val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl)
return BookList.analyzeBookList( return BookList.analyzeBookList(
@ -50,6 +53,7 @@ class WebBook(val bookSource: BookSource) {
bookSource, bookSource,
analyzeUrl, analyzeUrl,
res.url, res.url,
variableBook,
true true
) )
} }
@ -62,8 +66,9 @@ class WebBook(val bookSource: BookSource) {
fun exploreBook( fun exploreBook(
url: String, url: String,
page: Int? = 1, page: Int? = 1,
variableBook: SearchBook,
scope: CoroutineScope = Coroutine.DEFAULT, scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO,
): Coroutine<List<SearchBook>> { ): Coroutine<List<SearchBook>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
@ -79,6 +84,7 @@ class WebBook(val bookSource: BookSource) {
bookSource, bookSource,
analyzeUrl, analyzeUrl,
res.url, res.url,
variableBook,
false false
) )
} }

@ -6,6 +6,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource 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.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CheckSourceService import io.legado.app.service.CheckSourceService
@ -49,7 +50,9 @@ class CheckSource(val source: BookSource) {
onNext: (sourceUrl: String) -> Unit onNext: (sourceUrl: String) -> Unit
): Coroutine<*> { ): Coroutine<*> {
val webBook = WebBook(source) 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) .timeout(60000L)
.onError(Dispatchers.IO) { .onError(Dispatchers.IO) {
source.addGroup("失效") source.addGroup("失效")

@ -80,7 +80,9 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
} }
searchIndex++ searchIndex++
val source = bookSourceList[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) .timeout(60000L)
.onSuccess(Dispatchers.IO) { .onSuccess(Dispatchers.IO) {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {

@ -113,7 +113,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
searchIndex++ searchIndex++
val source = bookSourceList[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) .timeout(60000L)
.onSuccess(IO) { .onSuccess(IO) {
it.forEach { searchBook -> it.forEach { searchBook ->

@ -14,6 +14,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
val booksData = MutableLiveData<List<SearchBook>>() val booksData = MutableLiveData<List<SearchBook>>()
private var bookSource: BookSource? = null private var bookSource: BookSource? = null
private val variableBook = SearchBook()
private var exploreUrl: String? = null private var exploreUrl: String? = null
private var page = 1 private var page = 1
@ -32,7 +33,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
val source = bookSource val source = bookSource
val url = exploreUrl val url = exploreUrl
if (source != null && url != null) { if (source != null && url != null) {
WebBook(source).exploreBook(url, page, this) WebBook(source).exploreBook(url, page, variableBook, this)
.timeout(30000L) .timeout(30000L)
.onSuccess(IO) { searchBooks -> .onSuccess(IO) { searchBooks ->
booksData.postValue(searchBooks) booksData.postValue(searchBooks)

@ -7,6 +7,7 @@ import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter 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.AppConfig
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
@ -98,7 +99,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun loadBookInfo( private fun loadBookInfo(
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null,
) { ) {
execute { execute {
if (book.isLocalBook()) { if (book.isLocalBook()) {
@ -114,7 +115,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun loadChapterList( fun loadChapterList(
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null,
) { ) {
execute { execute {
if (book.isLocalBook()) { if (book.isLocalBook()) {
@ -182,13 +183,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
App.db.bookSourceDao().allTextEnabled.forEach { source -> App.db.bookSourceDao().allTextEnabled.forEach { source ->
try { try {
val searchBooks = WebBook(source).searchBookSuspend(this, name) val variableBook = SearchBook()
searchBooks.getOrNull(0)?.let { WebBook(source)
if (it.name == name && (it.author == author || author == "")) { .searchBookSuspend(this, name, variableBook = variableBook)
changeTo(it.toBook()) .getOrNull(0)?.let {
return@forEach if (it.name == name && (it.author == author || author == "")) {
changeTo(it.toBook())
return@forEach
}
} }
}
} catch (e: Exception) { } catch (e: Exception) {
//nothing //nothing
} }

Loading…
Cancel
Save