diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index d8fc7e4a3..30e77eab6 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -51,7 +51,6 @@ object Backup { writeListToJson(App.db.rssSourceDao().all, "rssSource.json", backupPath) writeListToJson(App.db.rssStarDao().all, "rssStar.json", backupPath) writeListToJson(App.db.replaceRuleDao().all, "replaceRule.json", backupPath) - GSON.toJson(ReadBookConfig.configList)?.let { FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.readConfigFileName) .writeText(it) diff --git a/app/src/main/java/io/legado/app/help/storage/OldBook.kt b/app/src/main/java/io/legado/app/help/storage/OldBook.kt new file mode 100644 index 000000000..f8321973c --- /dev/null +++ b/app/src/main/java/io/legado/app/help/storage/OldBook.kt @@ -0,0 +1,55 @@ +package io.legado.app.help.storage + +import android.util.Log +import io.legado.app.App +import io.legado.app.constant.AppConst +import io.legado.app.data.entities.Book +import io.legado.app.utils.readBool +import io.legado.app.utils.readInt +import io.legado.app.utils.readLong +import io.legado.app.utils.readString + +object OldBook { + + fun toNewBook(json: String): List { + val books = mutableListOf() + val items: List> = Restore.jsonPath.parse(json).read("$") + val existingBooks = App.db.bookDao().allBookUrls.toSet() + for (item in items) { + val jsonItem = Restore.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) { + Log.d(AppConst.APP_TAG, "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.group = jsonItem.readInt("$.group") ?: 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.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true + book.variable = jsonItem.readString("$.variable") + books.add(book) + } + return books + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 4385d6282..ee58fffbd 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -2,18 +2,19 @@ package io.legado.app.help.storage import android.content.Context import android.net.Uri -import android.util.Log import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import com.jayway.jsonpath.ParseContext import io.legado.app.App -import io.legado.app.constant.AppConst import io.legado.app.constant.PreferKey import io.legado.app.data.entities.* import io.legado.app.help.ReadBookConfig -import io.legado.app.utils.* +import io.legado.app.utils.DocumentUtils +import io.legado.app.utils.FileUtils +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonArray import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.GlobalScope @@ -50,51 +51,23 @@ object Restore { suspend fun restore(path: String) { withContext(IO) { - try { - val file = FileUtils.createFileIfNotExist(path + File.separator + "bookshelf.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.bookDao().insert(*it.toTypedArray()) - } - } catch (e: Exception) { - e.printStackTrace() + fileToListT(path, "bookshelf.json")?.let { + App.db.bookDao().insert(*it.toTypedArray()) } - try { - val file = FileUtils.createFileIfNotExist(path + File.separator + "bookGroup.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.bookGroupDao().insert(*it.toTypedArray()) - } - } catch (e: Exception) { - e.printStackTrace() + fileToListT(path, "bookGroup.json")?.let { + App.db.bookGroupDao().insert(*it.toTypedArray()) } - try { - val file = FileUtils.createFileIfNotExist(path + File.separator + "bookSource.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.bookSourceDao().insert(*it.toTypedArray()) - } - } catch (e: Exception) { - e.printStackTrace() + fileToListT(path, "bookSource.json")?.let { + App.db.bookSourceDao().insert(*it.toTypedArray()) } - try { - val file = FileUtils.createFileIfNotExist(path + File.separator + "rssSource.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.rssSourceDao().insert(*it.toTypedArray()) - } - } catch (e: Exception) { - e.printStackTrace() + fileToListT(path, "rssSource.json")?.let { + App.db.rssSourceDao().insert(*it.toTypedArray()) } - try { - val file = - FileUtils.createFileIfNotExist(path + File.separator + "replaceRule.json") - val json = file.readText() - GSON.fromJsonArray(json)?.let { - App.db.replaceRuleDao().insert(*it.toTypedArray()) - } - } catch (e: Exception) { - e.printStackTrace() + fileToListT(path, "rssStar.json")?.let { + App.db.rssStarDao().insert(*it.toTypedArray()) + } + fileToListT(path, "replaceRule.json")?.let { + App.db.replaceRuleDao().insert(*it.toTypedArray()) } try { val file = @@ -124,6 +97,17 @@ object Restore { } } + private inline fun fileToListT(path: String, fileName: String): List? { + try { + val file = FileUtils.createFileIfNotExist(path + File.separator + fileName) + val json = file.readText() + return GSON.fromJsonArray(json) + } catch (e: Exception) { + e.printStackTrace() + } + return null + } + fun importYueDuData(context: Context) { GlobalScope.launch(IO) { try {// 导入书架 @@ -171,43 +155,7 @@ object Restore { } fun importOldBookshelf(json: String): Int { - val books = mutableListOf() - val items: List> = jsonPath.parse(json).read("$") - val existingBooks = App.db.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) { - Log.d(AppConst.APP_TAG, "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.group = jsonItem.readInt("$.group") ?: 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.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true - book.variable = jsonItem.readString("$.variable") - books.add(book) - } + val books = OldBook.toNewBook(json) App.db.bookDao().insert(*books.toTypedArray()) return books.size }