diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 11dcbab2c..0d13ee94d 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.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() 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 } diff --git a/app/src/main/java/io/legado/app/model/rss/Rss.kt b/app/src/main/java/io/legado/app/model/rss/Rss.kt index 830f72a80..6dee8a9d0 100644 --- a/app/src/main/java/io/legado/app/model/rss/Rss.kt +++ b/app/src/main/java/io/legado/app/model/rss/Rss.kt @@ -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) } } diff --git a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt index 82ab063ae..955df439b 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt @@ -38,7 +38,7 @@ object RssParserByRule { } else { val articleList = mutableListOf() val analyzeRule = AnalyzeRule() - analyzeRule.setContent(body, sortUrl) + analyzeRule.setContent(body).setBaseUrl(sortUrl) var reverse = false if (ruleArticles.startsWith("-")) { reverse = true diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index 78a139345..16ffa803a 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -210,7 +210,7 @@ object BookChapterList { log: Boolean = false ): ChapterData> { val analyzeRule = AnalyzeRule(book) - analyzeRule.setContent(body, baseUrl) + analyzeRule.setContent(body).setBaseUrl(baseUrl) val chapterList = arrayListOf() val nextUrlList = arrayListOf() val nextTocRule = tocRule.nextTocUrl diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index c2ae45071..50b895ec7 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -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> { val analyzeRule = AnalyzeRule(book) - analyzeRule.setContent(body, baseUrl) + analyzeRule.setContent(body).setBaseUrl(baseUrl) val nextUrlList = arrayListOf() analyzeRule.chapter = chapter val nextUrlRule = contentRule.nextContentUrl diff --git a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt index 7d6c3a94c..24dd869e6 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt @@ -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, "≡执行详情页初始化规则") diff --git a/app/src/main/java/io/legado/app/model/webBook/BookList.kt b/app/src/main/java/io/legado/app/model/webBook/BookList.kt index 05570b6ff..fd2697587 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookList.kt @@ -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, "≡链接为详情页") diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index 8379c2dd3..3b9ae2ed8 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -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) @@ -98,13 +114,13 @@ object NetworkUtils { } else url.substring(0, index) } - fun getSubDomain(url: String?): String { - val baseUrl = getBaseUrl(url) ?: return "" - return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { - baseUrl.substring(baseUrl.lastIndexOf("/") + 1) - } else baseUrl.substring(baseUrl.indexOf(".") + 1) - } - + fun getSubDomain(url: String?): String { + val baseUrl = getBaseUrl(url) ?: return "" + return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { + baseUrl.substring(baseUrl.lastIndexOf("/") + 1) + } else baseUrl.substring(baseUrl.indexOf(".") + 1) + } + /** * Get local Ip address. */