kunfei 3 rokov pred
rodič
commit
5416f9160d

+ 75 - 68
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.content.Context
 import android.net.Uri
 import android.net.Uri
 import androidx.documentfile.provider.DocumentFile
 import androidx.documentfile.provider.DocumentFile
+import com.jayway.jsonpath.DocumentContext
 import io.legado.app.R
 import io.legado.app.R
 import io.legado.app.constant.AppConst
 import io.legado.app.constant.AppConst
 import io.legado.app.constant.BookSourceType
 import io.legado.app.constant.BookSourceType
@@ -20,7 +21,9 @@ import java.util.regex.Pattern
 
 
 object ImportOldData {
 object ImportOldData {
 
 
+    @Suppress("RegExpRedundantEscape")
     private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
     private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
+    @Suppress("RegExpRedundantEscape")
     private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE)
     private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE)
 
 
     fun importUri(context: Context, uri: Uri) {
     fun importUri(context: Context, uri: Uri) {
@@ -162,79 +165,83 @@ object ImportOldData {
         val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
         val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
         for (item in items) {
         for (item in items) {
             val jsonItem = jsonPath.parse(item)
             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)
             sources.add(source)
         }
         }
         return sources
         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规则适配
     // default规则适配
     // #正则#替换内容 替换成 ##正则##替换内容
     // #正则#替换内容 替换成 ##正则##替换内容

+ 8 - 2
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.CookieStore
 import io.legado.app.help.http.newCallStrResponse
 import io.legado.app.help.http.newCallStrResponse
 import io.legado.app.help.http.okHttpClient
 import io.legado.app.help.http.okHttpClient
+import io.legado.app.help.storage.ImportOldData
 import io.legado.app.utils.*
 import io.legado.app.utils.*
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers
 
 
@@ -92,10 +93,15 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
             text.isJsonArray() -> {
             text.isJsonArray() -> {
                 val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
                 val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
                 val jsonItem = jsonPath.parse(items[0])
                 val jsonItem = jsonPath.parse(items[0])
-                BookSource.fromJson(jsonItem.jsonString()).getOrThrow()
+                BookSource.fromJson(jsonItem.jsonString()).getOrElse {
+                    ImportOldData.fromOldBookSource(jsonItem)
+                }
             }
             }
             text.isJsonObject() -> {
             text.isJsonObject() -> {
-                BookSource.fromJson(text).getOrThrow()
+                BookSource.fromJson(text).getOrElse {
+                    val jsonItem = jsonPath.parse(text)
+                    ImportOldData.fromOldBookSource(jsonItem)
+                }
             }
             }
             else -> throw NoStackTraceException("格式不对")
             else -> throw NoStackTraceException("格式不对")
         }
         }