pull/1679/head
kunfei 3 years ago
parent 5ca2f10fac
commit cce028c303
  1. 4
      app/src/main/java/io/legado/app/help/ReplaceAnalyzer.kt
  2. 46
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  3. 50
      app/src/main/java/io/legado/app/help/storage/OldBook.kt
  4. 6
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt

@ -1,9 +1,9 @@
package io.legado.app.help.storage
package io.legado.app.help
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.*
object OldReplace {
object ReplaceAnalyzer {
fun jsonToReplaceRules(json: String): List<ReplaceRule> {
val replaceRules = mutableListOf<ReplaceRule>()

@ -4,7 +4,9 @@ import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.ReplaceAnalyzer
import io.legado.app.utils.*
import java.io.File
@ -83,7 +85,7 @@ object ImportOldData {
}
private fun importOldBookshelf(json: String): Int {
val books = OldBook.toNewBook(json)
val books = fromOldBooks(json)
appDb.bookDao.insert(*books.toTypedArray())
return books.size
}
@ -95,8 +97,48 @@ object ImportOldData {
}
private fun importOldReplaceRule(json: String): Int {
val rules = OldReplace.jsonToReplaceRules(json)
val rules = ReplaceAnalyzer.jsonToReplaceRules(json)
appDb.replaceRuleDao.insert(*rules.toTypedArray())
return rules.size
}
private fun fromOldBooks(json: String): List<Book> {
val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val existingBooks = appDb.bookDao.allBookUrls.toSet()
for (item in items) {
val jsonItem = jsonPath.parse(item)
val book = Book()
book.bookUrl = jsonItem.readString("$.noteUrl") ?: ""
if (book.bookUrl.isBlank()) continue
book.name = jsonItem.readString("$.bookInfoBean.name") ?: ""
if (book.bookUrl in existingBooks) {
DebugLog.d(javaClass.name, "Found existing book: " + book.name)
continue
}
book.origin = jsonItem.readString("$.tag") ?: ""
book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: ""
book.author = jsonItem.readString("$.bookInfoBean.author") ?: ""
book.type =
if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl
book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl")
book.customCoverUrl = jsonItem.readString("$.customCoverPath")
book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0
book.canUpdate = jsonItem.readBool("$.allowUpdate") == true
book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0
book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0
book.durChapterTitle = jsonItem.readString("$.durChapterName")
book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0
book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0
book.intro = jsonItem.readString("$.bookInfoBean.introduce")
book.latestChapterTitle = jsonItem.readString("$.lastChapterName")
book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0
book.order = jsonItem.readInt("$.serialNumber") ?: 0
book.variable = jsonItem.readString("$.variable")
book.setUseReplaceRule(jsonItem.readBool("$.useReplaceRule") == true)
books.add(book)
}
return books
}
}

@ -1,50 +0,0 @@
package io.legado.app.help.storage
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.utils.*
object OldBook {
fun toNewBook(json: String): List<Book> {
val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val existingBooks = appDb.bookDao.allBookUrls.toSet()
for (item in items) {
val jsonItem = jsonPath.parse(item)
val book = Book()
book.bookUrl = jsonItem.readString("$.noteUrl") ?: ""
if (book.bookUrl.isBlank()) continue
book.name = jsonItem.readString("$.bookInfoBean.name") ?: ""
if (book.bookUrl in existingBooks) {
DebugLog.d(javaClass.name, "Found existing book: " + book.name)
continue
}
book.origin = jsonItem.readString("$.tag") ?: ""
book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: ""
book.author = jsonItem.readString("$.bookInfoBean.author") ?: ""
book.type =
if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl
book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl")
book.customCoverUrl = jsonItem.readString("$.customCoverPath")
book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0
book.canUpdate = jsonItem.readBool("$.allowUpdate") == true
book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0
book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0
book.durChapterTitle = jsonItem.readString("$.durChapterName")
book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0
book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0
book.intro = jsonItem.readString("$.bookInfoBean.introduce")
book.latestChapterTitle = jsonItem.readString("$.lastChapterName")
book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0
book.order = jsonItem.readInt("$.serialNumber") ?: 0
book.variable = jsonItem.readString("$.variable")
book.setUseReplaceRule(jsonItem.readBool("$.useReplaceRule") == true)
books.add(book)
}
return books
}
}

@ -7,10 +7,10 @@ import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.AppConfig
import io.legado.app.help.ReplaceAnalyzer
import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.storage.OldReplace
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.splitNotBlank
@ -87,11 +87,11 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
okHttpClient.newCallResponseBody {
url(text)
}.text("utf-8").let {
val rules = OldReplace.jsonToReplaceRules(it)
val rules = ReplaceAnalyzer.jsonToReplaceRules(it)
allRules.addAll(rules)
}
} else {
val rules = OldReplace.jsonToReplaceRules(text)
val rules = ReplaceAnalyzer.jsonToReplaceRules(text)
allRules.addAll(rules)
}
}.onError {

Loading…
Cancel
Save