优化书源转换

pull/49/head
Celeter 5 years ago
parent f5062e8c79
commit 2af5ce705c
  1. 120
      app/src/main/java/io/legado/app/help/storage/OldRule.kt

@ -9,6 +9,7 @@ import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.readInt import io.legado.app.utils.readInt
import io.legado.app.utils.readString import io.legado.app.utils.readString
import io.legado.app.utils.readBool
import java.util.regex.Pattern import java.util.regex.Pattern
object OldRule { object OldRule {
@ -26,7 +27,7 @@ object OldRule {
val jsonItem = jsonPath.parse(json.trim()) val jsonItem = jsonPath.parse(json.trim())
bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: "" bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: ""
bookSourceName = jsonItem.readString("bookSourceName") ?: "" bookSourceName = jsonItem.readString("bookSourceName") ?: ""
bookSourceGroup = jsonItem.readString("bookSourceGroup") ?: "" bookSourceGroup = jsonItem.readString("bookSourceGroup")
loginUrl = jsonItem.readString("loginUrl") loginUrl = jsonItem.readString("loginUrl")
bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") bookUrlPattern = jsonItem.readString("ruleBookUrlPattern")
customOrder = jsonItem.readInt("serialNumber") ?: 0 customOrder = jsonItem.readInt("serialNumber") ?: 0
@ -35,56 +36,57 @@ object OldRule {
exploreUrl = toNewUrls(jsonItem.readString("ruleFindUrl")) exploreUrl = toNewUrls(jsonItem.readString("ruleFindUrl"))
bookSourceType = bookSourceType =
if (jsonItem.readString("bookSourceType") == "AUDIO") BookType.audio else BookType.default if (jsonItem.readString("bookSourceType") == "AUDIO") BookType.audio else BookType.default
enabled = jsonItem.readBool("enable") ?: true
if (exploreUrl.isNullOrBlank()) { if (exploreUrl.isNullOrBlank()) {
enabledExplore = false enabledExplore = false
} }
val searchRule = SearchRule( val searchRule = SearchRule(
bookList = jsonItem.readString("ruleSearchList"), bookList = toNewRule(jsonItem.readString("ruleSearchList")),
name = jsonItem.readString("ruleSearchName"), name = toNewRule(jsonItem.readString("ruleSearchName")),
author = jsonItem.readString("ruleSearchAuthor"), author = toNewRule(jsonItem.readString("ruleSearchAuthor")),
intro = jsonItem.readString("ruleSearchIntroduce"), intro = toNewRule(jsonItem.readString("ruleSearchIntroduce")),
kind = jsonItem.readString("ruleSearchKind"), kind = toNewRule(jsonItem.readString("ruleSearchKind")),
bookUrl = jsonItem.readString("ruleSearchNoteUrl"), bookUrl = toNewRule(jsonItem.readString("ruleSearchNoteUrl")),
coverUrl = jsonItem.readString("ruleSearchCoverUrl"), coverUrl = toNewRule(jsonItem.readString("ruleSearchCoverUrl")),
lastChapter = jsonItem.readString("ruleSearchLastChapter") lastChapter = toNewRule(jsonItem.readString("ruleSearchLastChapter"))
) )
ruleSearch = GSON.toJson(searchRule) ruleSearch = GSON.toJson(searchRule)
val exploreRule = ExploreRule( val exploreRule = ExploreRule(
bookList = jsonItem.readString("ruleFindList"), bookList = toNewRule(jsonItem.readString("ruleFindList")),
name = jsonItem.readString("ruleFindName"), name = toNewRule(jsonItem.readString("ruleFindName")),
author = jsonItem.readString("ruleFindAuthor"), author = toNewRule(jsonItem.readString("ruleFindAuthor")),
intro = jsonItem.readString("ruleFindIntroduce"), intro = toNewRule(jsonItem.readString("ruleFindIntroduce")),
kind = jsonItem.readString("ruleFindKind"), kind = toNewRule(jsonItem.readString("ruleFindKind")),
bookUrl = jsonItem.readString("ruleFindNoteUrl"), bookUrl = toNewRule(jsonItem.readString("ruleFindNoteUrl")),
coverUrl = jsonItem.readString("ruleFindCoverUrl"), coverUrl = toNewRule(jsonItem.readString("ruleFindCoverUrl")),
lastChapter = jsonItem.readString("ruleFindLastChapter") lastChapter = toNewRule(jsonItem.readString("ruleFindLastChapter"))
) )
ruleExplore = GSON.toJson(exploreRule) ruleExplore = GSON.toJson(exploreRule)
val bookInfoRule = BookInfoRule( val bookInfoRule = BookInfoRule(
init = jsonItem.readString("ruleBookInfoInit"), init = toNewRule(jsonItem.readString("ruleBookInfoInit")),
name = jsonItem.readString("ruleBookName"), name = toNewRule(jsonItem.readString("ruleBookName")),
author = jsonItem.readString("ruleBookAuthor"), author = toNewRule(jsonItem.readString("ruleBookAuthor")),
intro = jsonItem.readString("ruleIntroduce"), intro = toNewRule(jsonItem.readString("ruleIntroduce")),
kind = jsonItem.readString("ruleBookKind"), kind = toNewRule(jsonItem.readString("ruleBookKind")),
coverUrl = jsonItem.readString("ruleCoverUrl"), coverUrl = toNewRule(jsonItem.readString("ruleCoverUrl")),
lastChapter = jsonItem.readString("ruleBookLastChapter"), lastChapter = toNewRule(jsonItem.readString("ruleBookLastChapter")),
tocUrl = jsonItem.readString("ruleChapterUrl") tocUrl = toNewRule(jsonItem.readString("ruleChapterUrl"))
) )
ruleBookInfo = GSON.toJson(bookInfoRule) ruleBookInfo = GSON.toJson(bookInfoRule)
val chapterRule = TocRule( val chapterRule = TocRule(
chapterList = jsonItem.readString("ruleChapterList"), chapterList = toNewRule(jsonItem.readString("ruleChapterList")),
chapterName = jsonItem.readString("ruleChapterName"), chapterName = toNewRule(jsonItem.readString("ruleChapterName")),
chapterUrl = jsonItem.readString("ruleContentUrl"), chapterUrl = toNewRule(jsonItem.readString("ruleContentUrl")),
nextTocUrl = jsonItem.readString("ruleChapterUrlNext") nextTocUrl = toNewRule(jsonItem.readString("ruleChapterUrlNext"))
) )
ruleToc = GSON.toJson(chapterRule) ruleToc = GSON.toJson(chapterRule)
var content = jsonItem.readString("ruleBookContent") ?: "" var content = toNewRule(jsonItem.readString("ruleBookContent")) ?: ""
if (content.startsWith("$") && !content.startsWith("$.")) { if (content.startsWith("$") && !content.startsWith("$.")) {
content = content.substring(1) content = content.substring(1)
} }
val contentRule = ContentRule( val contentRule = ContentRule(
content = content, content = content,
nextContentUrl = jsonItem.readString("ruleContentUrlNext") nextContentUrl = toNewRule(jsonItem.readString("ruleContentUrlNext"))
) )
ruleContent = GSON.toJson(contentRule) ruleContent = GSON.toJson(contentRule)
} }
@ -93,21 +95,55 @@ object OldRule {
return source return source
} }
private fun toNewUrls(oldUrl: String?): String? { // default规则适配
if (oldUrl == null) return null // #正则#替换内容 替换成 ##正则##替换内容
if (!oldUrl.contains("\n") && !oldUrl.contains("&&")) // | 替换成 ||
return toNewUrl(oldUrl) // & 替换成 &&
private fun toNewRule(oldRule: String?): String? {
val urls = oldUrl.split("(&&|\n)+".toRegex()) if (oldRule.isNullOrBlank()) return null
var newUrl = "" var newRule = oldRule
for (url in urls) { if (oldRule.startsWith("-")) {
newUrl += toNewUrl(url)?.replace("\\n\\s*".toRegex(), "") + "\n" newRule = oldRule.substring(1)
}
if (!newRule.startsWith("@CSS:", true) &&
!newRule.startsWith("@XPath:", true) &&
!newRule.startsWith("//") &&
!newRule.startsWith("##") &&
!newRule.startsWith(":")
) {
if (newRule.contains("#") && !newRule.contains("##")) {
newRule = oldRule.replace("#", "##")
}
if (newRule.contains("|") && !newRule.contains("||")) {
if (newRule.contains("##")) {
if (newRule.split("##")[0].contains("|")) {
newRule = newRule.replace("|", "||")
}
} else {
newRule = newRule.replace("|", "||")
}
}
if (newRule.contains("&") && !newRule.contains("&&")) {
newRule = newRule.replace("&", "&&")
}
} }
return newUrl return newRule
}
private fun toNewUrls(oldUrls: String?): String? {
if (oldUrls.isNullOrBlank()) return null
if (!oldUrls.contains("\n") && !oldUrls.contains("&&"))
return toNewUrl(oldUrls)
val urls = oldUrls.split("(&&|\n)+".toRegex())
val newUrls = urls.map {
toNewUrl(it)?.replace("\n\\s*".toRegex(),"")
}.joinToString("\n")
return newUrls
} }
private fun toNewUrl(oldUrl: String?): String? { private fun toNewUrl(oldUrl: String?): String? {
if (oldUrl == null) return null if (oldUrl.isNullOrBlank()) return null
var url: String = oldUrl var url: String = oldUrl
if (oldUrl.startsWith("<js>", true)) { if (oldUrl.startsWith("<js>", true)) {
url = url.replace("=searchKey", "={{key}}") url = url.replace("=searchKey", "={{key}}")

Loading…
Cancel
Save