订阅更书籍一样可以put,get

pull/754/head
gedoor 4 years ago
parent 660ff0132d
commit 1a02f43343
  1. 4
      app/src/main/java/io/legado/app/data/entities/BaseBook.kt
  2. 4
      app/src/main/java/io/legado/app/data/entities/RssArticle.kt
  3. 4
      app/src/main/java/io/legado/app/data/entities/RssStar.kt
  4. 5
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  5. 18
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  6. 10
      app/src/main/java/io/legado/app/model/analyzeRule/RuleData.kt
  7. 9
      app/src/main/java/io/legado/app/model/analyzeRule/RuleDataInterface.kt
  8. 17
      app/src/main/java/io/legado/app/model/rss/Rss.kt
  9. 13
      app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt
  10. 1
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  11. 2
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  12. 1
      app/src/main/java/io/legado/app/model/webBook/BookInfo.kt

@ -1,9 +1,9 @@
package io.legado.app.data.entities package io.legado.app.data.entities
import io.legado.app.model.analyzeRule.RuleData import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
interface BaseBook : RuleData { interface BaseBook : RuleDataInterface {
var name: String var name: String
var author: String var author: String
var bookUrl: String var bookUrl: String

@ -2,7 +2,7 @@ package io.legado.app.data.entities
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Ignore import androidx.room.Ignore
import io.legado.app.model.analyzeRule.RuleData import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
@ -24,7 +24,7 @@ data class RssArticle(
var image: String? = null, var image: String? = null,
var read: Boolean = false, var read: Boolean = false,
var variable: String? = null var variable: String? = null
) : RuleData { ) : RuleDataInterface {
override fun hashCode() = link.hashCode() override fun hashCode() = link.hashCode()

@ -2,7 +2,7 @@ package io.legado.app.data.entities
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Ignore import androidx.room.Ignore
import io.legado.app.model.analyzeRule.RuleData import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
@ -23,7 +23,7 @@ data class RssStar(
var content: String? = null, var content: String? = null,
var image: String? = null, var image: String? = null,
var variable: String? = null var variable: String? = null
) : RuleData { ) : RuleDataInterface {
@delegate:Transient @delegate:Transient
@delegate:Ignore @delegate:Ignore

@ -24,7 +24,8 @@ import kotlin.collections.HashMap
*/ */
@Keep @Keep
@Suppress("unused", "RegExpRedundantEscape") @Suppress("unused", "RegExpRedundantEscape")
class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
var book: BaseBook? = null
var chapter: BookChapter? = null var chapter: BookChapter? = null
private var content: Any? = null private var content: Any? = null
private var baseUrl: String? = null private var baseUrl: String? = null
@ -606,6 +607,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
fun put(key: String, value: String): String { fun put(key: String, value: String): String {
chapter?.putVariable(key, value) chapter?.putVariable(key, value)
?: book?.putVariable(key, value) ?: book?.putVariable(key, value)
?: ruleData.putVariable(key, value)
return value return value
} }
@ -620,6 +622,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
} }
return chapter?.variableMap?.get(key) return chapter?.variableMap?.get(key)
?: book?.variableMap?.get(key) ?: book?.variableMap?.get(key)
?: ruleData.variableMap[key]
?: "" ?: ""
} }

@ -40,6 +40,7 @@ class AnalyzeUrl(
var useWebView: Boolean = false, var useWebView: Boolean = false,
val book: BaseBook? = null, val book: BaseBook? = null,
val chapter: BookChapter? = null, val chapter: BookChapter? = null,
private val ruleData: RuleDataInterface? = null,
headerMapF: Map<String, String>? = null headerMapF: Map<String, String>? = null
) : JsExtensions { ) : JsExtensions {
companion object { companion object {
@ -266,12 +267,25 @@ class AnalyzeUrl(
} }
fun put(key: String, value: String): String { fun put(key: String, value: String): String {
book?.putVariable(key, value) chapter?.putVariable(key, value)
?: book?.putVariable(key, value)
?: ruleData?.putVariable(key, value)
return value return value
} }
fun get(key: String): String { fun get(key: String): String {
return book?.variableMap?.get(key) ?: "" when (key) {
"bookName" -> book?.let {
return it.name
}
"title" -> chapter?.let {
return it.title
}
}
return chapter?.variableMap?.get(key)
?: book?.variableMap?.get(key)
?: ruleData?.variableMap?.get(key)
?: ""
} }
suspend fun getStrResponse( suspend fun getStrResponse(

@ -1,9 +1,13 @@
package io.legado.app.model.analyzeRule package io.legado.app.model.analyzeRule
interface RuleData { class RuleData : RuleDataInterface {
val variableMap: HashMap<String, String> override val variableMap by lazy {
hashMapOf<String, String>()
}
fun putVariable(key: String, value: String) override fun putVariable(key: String, value: String) {
variableMap[key] = value
}
} }

@ -0,0 +1,9 @@
package io.legado.app.model.analyzeRule
interface RuleDataInterface {
val variableMap: HashMap<String, String>
fun putVariable(key: String, value: String)
}

@ -5,6 +5,7 @@ import io.legado.app.data.entities.RssSource
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleData
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -21,13 +22,15 @@ object Rss {
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO
): Coroutine<RssResult> { ): Coroutine<RssResult> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
val ruleData = RuleData()
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
sortUrl, sortUrl,
page = page, page = page,
ruleData = ruleData,
headerMapF = rssSource.getHeaderMap() headerMapF = rssSource.getHeaderMap()
) )
val body = analyzeUrl.getStrResponse(rssSource.sourceUrl).body val body = analyzeUrl.getStrResponse(rssSource.sourceUrl).body
RssParserByRule.parseXML(sortName, sortUrl, body, rssSource) RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData)
} }
} }
@ -39,12 +42,14 @@ object Rss {
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO
): Coroutine<String> { ): Coroutine<String> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
val body = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
rssArticle.link, baseUrl = rssArticle.origin, rssArticle.link,
baseUrl = rssArticle.origin,
ruleData = rssArticle,
headerMapF = rssSource?.getHeaderMap() headerMapF = rssSource?.getHeaderMap()
).getStrResponse(rssArticle.origin) )
.body val body = analyzeUrl.getStrResponse(rssArticle.origin).body
val analyzeRule = AnalyzeRule() val analyzeRule = AnalyzeRule(rssArticle)
analyzeRule.setContent(body) analyzeRule.setContent(body)
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link)) .setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
analyzeRule.getString(ruleContent) analyzeRule.getString(ruleContent)

@ -7,6 +7,8 @@ import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.GSON
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import java.util.* import java.util.*
@ -18,16 +20,14 @@ object RssParserByRule {
sortName: String, sortName: String,
sortUrl: String, sortUrl: String,
body: String?, body: String?,
rssSource: RssSource rssSource: RssSource,
ruleData: RuleDataInterface
): RssResult { ): RssResult {
val sourceUrl = rssSource.sourceUrl val sourceUrl = rssSource.sourceUrl
var nextUrl: String? = null var nextUrl: String? = null
if (body.isNullOrBlank()) { if (body.isNullOrBlank()) {
throw Exception( throw Exception(
App.INSTANCE.getString( App.INSTANCE.getString(R.string.error_get_web_content, rssSource.sourceUrl)
R.string.error_get_web_content,
rssSource.sourceUrl
)
) )
} }
Debug.log(sourceUrl, "≡获取成功:$sourceUrl") Debug.log(sourceUrl, "≡获取成功:$sourceUrl")
@ -37,7 +37,7 @@ object RssParserByRule {
return RssParserDefault.parseXML(sortName, body, sourceUrl) return RssParserDefault.parseXML(sortName, body, sourceUrl)
} else { } else {
val articleList = mutableListOf<RssArticle>() val articleList = mutableListOf<RssArticle>()
val analyzeRule = AnalyzeRule() val analyzeRule = AnalyzeRule(ruleData)
analyzeRule.setContent(body).setBaseUrl(sortUrl) analyzeRule.setContent(body).setBaseUrl(sortUrl)
var reverse = false var reverse = false
if (ruleArticles.startsWith("-")) { if (ruleArticles.startsWith("-")) {
@ -114,6 +114,7 @@ object RssParserByRule {
Debug.log(sourceUrl, "┌获取文章链接", log) Debug.log(sourceUrl, "┌获取文章链接", log)
rssArticle.link = NetworkUtils.getAbsoluteURL(sourceUrl, analyzeRule.getString(ruleLink)) rssArticle.link = NetworkUtils.getAbsoluteURL(sourceUrl, analyzeRule.getString(ruleLink))
Debug.log(sourceUrl, "${rssArticle.link}", log) Debug.log(sourceUrl, "${rssArticle.link}", log)
rssArticle.variable = GSON.toJson(analyzeRule.ruleData.variableMap)
if (rssArticle.title.isBlank()) { if (rssArticle.title.isBlank()) {
return null return null
} }

@ -211,6 +211,7 @@ object BookChapterList {
): ChapterData<List<String>> { ): ChapterData<List<String>> {
val analyzeRule = AnalyzeRule(book) val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
analyzeRule.book = book
val chapterList = arrayListOf<BookChapter>() val chapterList = arrayListOf<BookChapter>()
val nextUrlList = arrayListOf<String>() val nextUrlList = arrayListOf<String>()
val nextTocRule = tocRule.nextTocUrl val nextTocRule = tocRule.nextTocUrl

@ -35,6 +35,7 @@ object BookContent {
val nextUrlList = arrayListOf(baseUrl) val nextUrlList = arrayListOf(baseUrl)
val contentRule = bookSource.getContentRule() val contentRule = bookSource.getContentRule()
val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl) val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl)
analyzeRule.book = book
var contentData = analyzeContent( var contentData = analyzeContent(
book, baseUrl, body, contentRule, bookChapter, bookSource book, baseUrl, body, contentRule, bookChapter, bookSource
) )
@ -118,6 +119,7 @@ object BookContent {
): ContentData<List<String>> { ): ContentData<List<String>> {
val analyzeRule = AnalyzeRule(book) val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
analyzeRule.book = book
val nextUrlList = arrayListOf<String>() val nextUrlList = arrayListOf<String>()
analyzeRule.chapter = chapter analyzeRule.chapter = chapter
val nextUrlRule = contentRule.nextContentUrl val nextUrlRule = contentRule.nextContentUrl

@ -27,6 +27,7 @@ object BookInfo {
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}") Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}")
val infoRule = bookSource.getBookInfoRule() val infoRule = bookSource.getBookInfoRule()
val analyzeRule = AnalyzeRule(book) val analyzeRule = AnalyzeRule(book)
analyzeRule.book = book
analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
infoRule.init?.let { infoRule.init?.let {
if (it.isNotBlank()) { if (it.isNotBlank()) {

Loading…
Cancel
Save