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

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

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

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

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

@ -27,7 +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.setContent(body, baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
infoRule.init?.let { infoRule.init?.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
Debug.log(bookSource.bookSourceUrl, "≡执行详情页初始化规则") Debug.log(bookSource.bookSourceUrl, "≡执行详情页初始化规则")

@ -38,7 +38,7 @@ 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 analyzeRule = AnalyzeRule(variableBook) val analyzeRule = AnalyzeRule(variableBook)
analyzeRule.setContent(body, baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
bookSource.bookUrlPattern?.let { bookSource.bookUrlPattern?.let {
if (baseUrl.matches(it.toRegex())) { if (baseUrl.matches(it.toRegex())) {
Debug.log(bookSource.bookSourceUrl, "≡链接为详情页") Debug.log(bookSource.bookSourceUrl, "≡链接为详情页")

@ -90,6 +90,22 @@ object NetworkUtils {
return relativeUrl 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? { fun getBaseUrl(url: String?): String? {
if (url == null || !url.startsWith("http")) return null if (url == null || !url.startsWith("http")) return null
val index = url.indexOf("/", 9) val index = url.indexOf("/", 9)
@ -98,12 +114,12 @@ object NetworkUtils {
} else url.substring(0, index) } else url.substring(0, index)
} }
fun getSubDomain(url: String?): String { fun getSubDomain(url: String?): String {
val baseUrl = getBaseUrl(url) ?: return "" val baseUrl = getBaseUrl(url) ?: return ""
return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) {
baseUrl.substring(baseUrl.lastIndexOf("/") + 1) baseUrl.substring(baseUrl.lastIndexOf("/") + 1)
} else baseUrl.substring(baseUrl.indexOf(".") + 1) } else baseUrl.substring(baseUrl.indexOf(".") + 1)
} }
/** /**
* Get local Ip address. * Get local Ip address.

Loading…
Cancel
Save