|
|
|
@ -111,47 +111,12 @@ object Restore { |
|
|
|
|
fun importYueDuData(context: Context) { |
|
|
|
|
GlobalScope.launch(IO) { |
|
|
|
|
try {// 导入书架 |
|
|
|
|
val shelfFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") |
|
|
|
|
val books = mutableListOf<Book>() |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).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) |
|
|
|
|
} |
|
|
|
|
App.db.bookDao().insert(*books.toTypedArray()) |
|
|
|
|
val shelfFile = |
|
|
|
|
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") |
|
|
|
|
val json = shelfFile.readText() |
|
|
|
|
val importCount = importOldBookshelf(json) |
|
|
|
|
withContext(Main) { |
|
|
|
|
context.toast("成功导入书籍${books.size}") |
|
|
|
|
context.toast("成功导入书籍${importCount}") |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
withContext(Main) { |
|
|
|
@ -162,17 +127,10 @@ object Restore { |
|
|
|
|
try {// Book source |
|
|
|
|
val sourceFile = |
|
|
|
|
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json") |
|
|
|
|
val bookSources = mutableListOf<BookSource>() |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$") |
|
|
|
|
for (item in items) { |
|
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
|
OldRule.jsonToBookSource(jsonItem.jsonString())?.let { |
|
|
|
|
bookSources.add(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
|
val json = sourceFile.readText() |
|
|
|
|
val importCount = importOldSource(json) |
|
|
|
|
withContext(Main) { |
|
|
|
|
context.toast("成功导入书源${bookSources.size}") |
|
|
|
|
context.toast("成功导入书源${importCount}") |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
withContext(Main) { |
|
|
|
@ -183,26 +141,10 @@ object Restore { |
|
|
|
|
try {// Replace rules |
|
|
|
|
val ruleFile = |
|
|
|
|
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") |
|
|
|
|
val replaceRules = mutableListOf<ReplaceRule>() |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$") |
|
|
|
|
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() |
|
|
|
|
for ((index: Int, item: Map<String, Any>) in items.withIndex()) { |
|
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
|
val rule = ReplaceRule() |
|
|
|
|
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index) |
|
|
|
|
rule.pattern = jsonItem.readString("$.regex") ?: "" |
|
|
|
|
if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue |
|
|
|
|
rule.name = jsonItem.readString("$.replaceSummary") ?: "" |
|
|
|
|
rule.replacement = jsonItem.readString("$.replacement") ?: "" |
|
|
|
|
rule.isRegex = jsonItem.readBool("$.isRegex") == true |
|
|
|
|
rule.scope = jsonItem.readString("$.useTo") |
|
|
|
|
rule.isEnabled = jsonItem.readBool("$.enable") == true |
|
|
|
|
rule.order = jsonItem.readInt("$.serialNumber") ?: index |
|
|
|
|
replaceRules.add(rule) |
|
|
|
|
} |
|
|
|
|
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) |
|
|
|
|
val json = ruleFile.readText() |
|
|
|
|
val importCount = importOldReplaceRule(json) |
|
|
|
|
withContext(Main) { |
|
|
|
|
context.toast("成功导入替换规则${replaceRules.size}") |
|
|
|
|
context.toast("成功导入替换规则${importCount}") |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
withContext(Main) { |
|
|
|
@ -211,4 +153,81 @@ object Restore { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun importOldBookshelf(json: String): Int { |
|
|
|
|
val books = mutableListOf<Book>() |
|
|
|
|
val items: List<Map<String, Any>> = 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) |
|
|
|
|
} |
|
|
|
|
App.db.bookDao().insert(*books.toTypedArray()) |
|
|
|
|
return books.size |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun importOldSource(json: String): Int { |
|
|
|
|
val bookSources = mutableListOf<BookSource>() |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") |
|
|
|
|
for (item in items) { |
|
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
|
OldRule.jsonToBookSource(jsonItem.jsonString())?.let { |
|
|
|
|
bookSources.add(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
|
return bookSources.size |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun importOldReplaceRule(json: String): Int { |
|
|
|
|
val replaceRules = mutableListOf<ReplaceRule>() |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") |
|
|
|
|
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() |
|
|
|
|
for ((index: Int, item: Map<String, Any>) in items.withIndex()) { |
|
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
|
val rule = ReplaceRule() |
|
|
|
|
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index) |
|
|
|
|
rule.pattern = jsonItem.readString("$.regex") ?: "" |
|
|
|
|
if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue |
|
|
|
|
rule.name = jsonItem.readString("$.replaceSummary") ?: "" |
|
|
|
|
rule.replacement = jsonItem.readString("$.replacement") ?: "" |
|
|
|
|
rule.isRegex = jsonItem.readBool("$.isRegex") == true |
|
|
|
|
rule.scope = jsonItem.readString("$.useTo") |
|
|
|
|
rule.isEnabled = jsonItem.readBool("$.enable") == true |
|
|
|
|
rule.order = jsonItem.readInt("$.serialNumber") ?: index |
|
|
|
|
replaceRules.add(rule) |
|
|
|
|
} |
|
|
|
|
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) |
|
|
|
|
return replaceRules.size |
|
|
|
|
} |
|
|
|
|
} |