pull/462/head
gedoor 4 years ago
parent 9c72fdec08
commit b15fcd4b98
  1. 26
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  2. 6
      app/src/main/java/io/legado/app/model/rss/Rss.kt
  3. 2
      app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt
  4. 2
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  5. 4
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  6. 2
      app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
  7. 2
      app/src/main/java/io/legado/app/model/webBook/BookList.kt
  8. 16
      app/src/main/java/io/legado/app/utils/NetworkUtils.kt

@ -10,6 +10,7 @@ import io.legado.app.help.JsExtensions
import io.legado.app.utils.*
import org.jsoup.nodes.Entities
import org.mozilla.javascript.NativeObject
import java.net.URL
import java.util.*
import java.util.regex.Pattern
import javax.script.SimpleBindings
@ -25,6 +26,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
var chapter: BookChapter? = null
private var content: Any? = null
private var baseUrl: String? = null
private var baseURL: URL? = null
private var isJSON: Boolean = false
private var isRegex: Boolean = false
@ -37,18 +39,28 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
private var objectChangedJP = false
@Throws(Exception::class)
@JvmOverloads
fun setContent(content: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule {
fun setContent(content: Any?): AnalyzeRule {
if (content == null) throw AssertionError("Content cannot be null")
isJSON = content.toString().isJson()
this.content = content
this.baseUrl = baseUrl
objectChangedXP = true
objectChangedJS = true
objectChangedJP = true
return this
}
fun setBaseUrl(baseUrl: String?): AnalyzeRule {
this.baseUrl = baseUrl
baseUrl?.let {
try {
baseURL = URL(baseUrl.substringBefore(","))
} catch (e: Exception) {
e.printStackTrace()
}
}
return this
}
/**
* 获取XPath解析类
*/
@ -157,7 +169,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
val urlList = ArrayList<String>()
if (result is List<*>) {
for (url in result as List<*>) {
val absoluteURL = NetworkUtils.getAbsoluteURL(baseUrl, url.toString())
val absoluteURL = NetworkUtils.getAbsoluteURL(baseURL, url.toString())
if (!absoluteURL.isNullOrEmpty() && !urlList.contains(absoluteURL)) {
urlList.add(absoluteURL)
}
@ -220,7 +232,11 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
result.toString()
}
if (isUrl) {
return NetworkUtils.getAbsoluteURL(baseUrl, str) ?: ""
return if (str.isBlank()) {
baseUrl ?: ""
} else {
NetworkUtils.getAbsoluteURL(baseURL, str) ?: ""
}
}
return str
}

@ -45,10 +45,8 @@ object Rss {
).getResponseAwait(rssArticle.origin)
.body
val analyzeRule = AnalyzeRule()
analyzeRule.setContent(
body,
NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link)
)
analyzeRule.setContent(body)
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
analyzeRule.getString(ruleContent)
}
}

@ -38,7 +38,7 @@ object RssParserByRule {
} else {
val articleList = mutableListOf<RssArticle>()
val analyzeRule = AnalyzeRule()
analyzeRule.setContent(body, sortUrl)
analyzeRule.setContent(body).setBaseUrl(sortUrl)
var reverse = false
if (ruleArticles.startsWith("-")) {
reverse = true

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

@ -97,7 +97,7 @@ object BookContent {
val replaceRegex = bookSource.ruleContent?.replaceRegex
if (!replaceRegex.isNullOrEmpty()) {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(contentStr, baseUrl)
analyzeRule.setContent(contentStr).setBaseUrl(baseUrl)
analyzeRule.chapter = bookChapter
contentStr = analyzeRule.getString(replaceRegex)
}
@ -119,7 +119,7 @@ object BookContent {
printLog: Boolean = true
): ContentData<List<String>> {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body, baseUrl)
analyzeRule.setContent(body).setBaseUrl(baseUrl)
val nextUrlList = arrayListOf<String>()
analyzeRule.chapter = chapter
val nextUrlRule = contentRule.nextContentUrl

@ -27,7 +27,7 @@ object BookInfo {
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}")
val infoRule = bookSource.getBookInfoRule()
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body, baseUrl)
analyzeRule.setContent(body).setBaseUrl(baseUrl)
infoRule.init?.let {
if (it.isNotEmpty()) {
Debug.log(bookSource.bookSourceUrl, "≡执行详情页初始化规则")

@ -38,7 +38,7 @@ object BookList {
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${analyzeUrl.ruleUrl}")
if (!scope.isActive) throw CancellationException()
val analyzeRule = AnalyzeRule(variableBook)
analyzeRule.setContent(body, baseUrl)
analyzeRule.setContent(body).setBaseUrl(baseUrl)
bookSource.bookUrlPattern?.let {
if (baseUrl.matches(it.toRegex())) {
Debug.log(bookSource.bookSourceUrl, "≡链接为详情页")

@ -90,6 +90,22 @@ object NetworkUtils {
return relativeUrl
}
/**
* 获取绝对地址
*/
fun getAbsoluteURL(baseURL: URL?, relativePath: String): String? {
if (baseURL == null) return relativePath
var relativeUrl = relativePath
try {
val parseUrl = URL(baseURL, relativePath)
relativeUrl = parseUrl.toString()
return relativeUrl
} catch (e: Exception) {
e.printStackTrace()
}
return relativeUrl
}
fun getBaseUrl(url: String?): String? {
if (url == null || !url.startsWith("http")) return null
val index = url.indexOf("/", 9)

Loading…
Cancel
Save