diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 27aac8fc6..6e5c64d2f 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -3,6 +3,7 @@ package io.legado.app.help.storage import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile +import com.jayway.jsonpath.DocumentContext import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.constant.BookSourceType @@ -20,7 +21,9 @@ import java.util.regex.Pattern object ImportOldData { + @Suppress("RegExpRedundantEscape") private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) + @Suppress("RegExpRedundantEscape") private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE) fun importUri(context: Context, uri: Uri) { @@ -162,79 +165,83 @@ object ImportOldData { val items: List> = jsonPath.parse(json).read("$") for (item in items) { val jsonItem = jsonPath.parse(item) - val source = BookSource() - source.apply { - bookSourceUrl = jsonItem.readString("bookSourceUrl") - ?: throw NoStackTraceException(appCtx.getString(R.string.wrong_format)) - bookSourceName = jsonItem.readString("bookSourceName") ?: "" - bookSourceGroup = jsonItem.readString("bookSourceGroup") - loginUrl = jsonItem.readString("loginUrl") - loginUi = jsonItem.readString("loginUi") - loginCheckJs = jsonItem.readString("loginCheckJs") - coverDecodeJs = jsonItem.readString("coverDecodeJs") - bookSourceComment = jsonItem.readString("bookSourceComment") ?: "" - bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") - customOrder = jsonItem.readInt("serialNumber") ?: 0 - header = uaToHeader(jsonItem.readString("httpUserAgent")) - searchUrl = toNewUrl(jsonItem.readString("ruleSearchUrl")) - exploreUrl = toNewUrls(jsonItem.readString("ruleFindUrl")) - bookSourceType = - if (jsonItem.readString("bookSourceType") == "AUDIO") BookSourceType.audio else BookSourceType.default - enabled = jsonItem.readBool("enable") ?: true - if (exploreUrl.isNullOrBlank()) { - enabledExplore = false - } - ruleSearch = SearchRule( - bookList = toNewRule(jsonItem.readString("ruleSearchList")), - name = toNewRule(jsonItem.readString("ruleSearchName")), - author = toNewRule(jsonItem.readString("ruleSearchAuthor")), - intro = toNewRule(jsonItem.readString("ruleSearchIntroduce")), - kind = toNewRule(jsonItem.readString("ruleSearchKind")), - bookUrl = toNewRule(jsonItem.readString("ruleSearchNoteUrl")), - coverUrl = toNewRule(jsonItem.readString("ruleSearchCoverUrl")), - lastChapter = toNewRule(jsonItem.readString("ruleSearchLastChapter")) - ) - ruleExplore = ExploreRule( - bookList = toNewRule(jsonItem.readString("ruleFindList")), - name = toNewRule(jsonItem.readString("ruleFindName")), - author = toNewRule(jsonItem.readString("ruleFindAuthor")), - intro = toNewRule(jsonItem.readString("ruleFindIntroduce")), - kind = toNewRule(jsonItem.readString("ruleFindKind")), - bookUrl = toNewRule(jsonItem.readString("ruleFindNoteUrl")), - coverUrl = toNewRule(jsonItem.readString("ruleFindCoverUrl")), - lastChapter = toNewRule(jsonItem.readString("ruleFindLastChapter")) - ) - ruleBookInfo = BookInfoRule( - init = toNewRule(jsonItem.readString("ruleBookInfoInit")), - name = toNewRule(jsonItem.readString("ruleBookName")), - author = toNewRule(jsonItem.readString("ruleBookAuthor")), - intro = toNewRule(jsonItem.readString("ruleIntroduce")), - kind = toNewRule(jsonItem.readString("ruleBookKind")), - coverUrl = toNewRule(jsonItem.readString("ruleCoverUrl")), - lastChapter = toNewRule(jsonItem.readString("ruleBookLastChapter")), - tocUrl = toNewRule(jsonItem.readString("ruleChapterUrl")) - ) - ruleToc = TocRule( - chapterList = toNewRule(jsonItem.readString("ruleChapterList")), - chapterName = toNewRule(jsonItem.readString("ruleChapterName")), - chapterUrl = toNewRule(jsonItem.readString("ruleContentUrl")), - nextTocUrl = toNewRule(jsonItem.readString("ruleChapterUrlNext")) - ) - var content = toNewRule(jsonItem.readString("ruleBookContent")) ?: "" - if (content.startsWith("$") && !content.startsWith("$.")) { - content = content.substring(1) - } - ruleContent = ContentRule( - content = content, - replaceRegex = toNewRule(jsonItem.readString("ruleBookContentReplace")), - nextContentUrl = toNewRule(jsonItem.readString("ruleContentUrlNext")) - ) - } + val source = fromOldBookSource(jsonItem) sources.add(source) } return sources } + fun fromOldBookSource(jsonItem: DocumentContext): BookSource { + val source = BookSource() + return source.apply { + bookSourceUrl = jsonItem.readString("bookSourceUrl") + ?: throw NoStackTraceException(appCtx.getString(R.string.wrong_format)) + bookSourceName = jsonItem.readString("bookSourceName") ?: "" + bookSourceGroup = jsonItem.readString("bookSourceGroup") + loginUrl = jsonItem.readString("loginUrl") + loginUi = jsonItem.readString("loginUi") + loginCheckJs = jsonItem.readString("loginCheckJs") + coverDecodeJs = jsonItem.readString("coverDecodeJs") + bookSourceComment = jsonItem.readString("bookSourceComment") ?: "" + bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") + customOrder = jsonItem.readInt("serialNumber") ?: 0 + header = uaToHeader(jsonItem.readString("httpUserAgent")) + searchUrl = toNewUrl(jsonItem.readString("ruleSearchUrl")) + exploreUrl = toNewUrls(jsonItem.readString("ruleFindUrl")) + bookSourceType = + if (jsonItem.readString("bookSourceType") == "AUDIO") BookSourceType.audio else BookSourceType.default + enabled = jsonItem.readBool("enable") ?: true + if (exploreUrl.isNullOrBlank()) { + enabledExplore = false + } + ruleSearch = SearchRule( + bookList = toNewRule(jsonItem.readString("ruleSearchList")), + name = toNewRule(jsonItem.readString("ruleSearchName")), + author = toNewRule(jsonItem.readString("ruleSearchAuthor")), + intro = toNewRule(jsonItem.readString("ruleSearchIntroduce")), + kind = toNewRule(jsonItem.readString("ruleSearchKind")), + bookUrl = toNewRule(jsonItem.readString("ruleSearchNoteUrl")), + coverUrl = toNewRule(jsonItem.readString("ruleSearchCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleSearchLastChapter")) + ) + ruleExplore = ExploreRule( + bookList = toNewRule(jsonItem.readString("ruleFindList")), + name = toNewRule(jsonItem.readString("ruleFindName")), + author = toNewRule(jsonItem.readString("ruleFindAuthor")), + intro = toNewRule(jsonItem.readString("ruleFindIntroduce")), + kind = toNewRule(jsonItem.readString("ruleFindKind")), + bookUrl = toNewRule(jsonItem.readString("ruleFindNoteUrl")), + coverUrl = toNewRule(jsonItem.readString("ruleFindCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleFindLastChapter")) + ) + ruleBookInfo = BookInfoRule( + init = toNewRule(jsonItem.readString("ruleBookInfoInit")), + name = toNewRule(jsonItem.readString("ruleBookName")), + author = toNewRule(jsonItem.readString("ruleBookAuthor")), + intro = toNewRule(jsonItem.readString("ruleIntroduce")), + kind = toNewRule(jsonItem.readString("ruleBookKind")), + coverUrl = toNewRule(jsonItem.readString("ruleCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleBookLastChapter")), + tocUrl = toNewRule(jsonItem.readString("ruleChapterUrl")) + ) + ruleToc = TocRule( + chapterList = toNewRule(jsonItem.readString("ruleChapterList")), + chapterName = toNewRule(jsonItem.readString("ruleChapterName")), + chapterUrl = toNewRule(jsonItem.readString("ruleContentUrl")), + nextTocUrl = toNewRule(jsonItem.readString("ruleChapterUrlNext")) + ) + var content = toNewRule(jsonItem.readString("ruleBookContent")) ?: "" + if (content.startsWith("$") && !content.startsWith("$.")) { + content = content.substring(1) + } + ruleContent = ContentRule( + content = content, + replaceRegex = toNewRule(jsonItem.readString("ruleBookContentReplace")), + nextContentUrl = toNewRule(jsonItem.readString("ruleContentUrlNext")) + ) + } + } + // default规则适配 // #正则#替换内容 替换成 ##正则##替换内容 diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index 9c355cd65..d0b268e75 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -12,6 +12,7 @@ import io.legado.app.help.config.SourceConfig import io.legado.app.help.http.CookieStore import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.okHttpClient +import io.legado.app.help.storage.ImportOldData import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers @@ -92,10 +93,15 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat text.isJsonArray() -> { val items: List> = jsonPath.parse(text).read("$") val jsonItem = jsonPath.parse(items[0]) - BookSource.fromJson(jsonItem.jsonString()).getOrThrow() + BookSource.fromJson(jsonItem.jsonString()).getOrElse { + ImportOldData.fromOldBookSource(jsonItem) + } } text.isJsonObject() -> { - BookSource.fromJson(text).getOrThrow() + BookSource.fromJson(text).getOrElse { + val jsonItem = jsonPath.parse(text) + ImportOldData.fromOldBookSource(jsonItem) + } } else -> throw NoStackTraceException("格式不对") }