From c874c6e816b7741a3ac12dcfe8e00b25ee873004 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 5 Sep 2019 14:01:46 +0800 Subject: [PATCH] up --- .../io/legado/app/help/storage/OldRule.kt | 84 ++++++++++++++++++- .../io/legado/app/help/storage/Restore.kt | 71 +--------------- .../app/ui/booksource/BookSourceActivity.kt | 2 + 3 files changed, 88 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt index 361fd1402..df2a389cc 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldRule.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -1,13 +1,92 @@ package io.legado.app.help.storage +import com.jayway.jsonpath.JsonPath +import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.rule.* import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.readInt +import io.legado.app.utils.readString import java.util.regex.Pattern object OldRule { private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE) - fun toNewUrl(oldUrl: String?): String? { + + fun jsonToBookSource(json: String): BookSource? { + var source: BookSource? = null + runCatching { + source = GSON.fromJsonObject(json) + } + runCatching { + source ?: let { + source = BookSource().apply { + val jsonItem = JsonPath.parse(json) + bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: "" + bookSourceName = jsonItem.readString("bookSourceName") ?: "" + bookSourceGroup = jsonItem.readString("bookSourceGroup") ?: "" + loginUrl = jsonItem.readString("loginUrl") + bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") + customOrder = jsonItem.readInt("serialNumber") ?: 0 + header = uaToHeader(jsonItem.readString("httpUserAgent")) + searchUrl = toNewUrl(jsonItem.readString("ruleSearchUrl")) + exploreUrl = toNewUrl(jsonItem.readString("ruleFindUrl")) + if (exploreUrl.isNullOrBlank()) { + enabledExplore = false + } + val searchRule = SearchRule( + bookList = jsonItem.readString("ruleSearchList"), + name = jsonItem.readString("ruleSearchName"), + author = jsonItem.readString("ruleSearchAuthor"), + intro = jsonItem.readString("ruleSearchIntroduce"), + kind = jsonItem.readString("ruleSearchKind"), + bookUrl = jsonItem.readString("ruleSearchNoteUrl"), + coverUrl = jsonItem.readString("ruleSearchCoverUrl"), + lastChapter = jsonItem.readString("ruleSearchLastChapter") + ) + ruleSearch = GSON.toJson(searchRule) + val exploreRule = ExploreRule( + bookList = jsonItem.readString("ruleFindList"), + name = jsonItem.readString("ruleFindName"), + author = jsonItem.readString("ruleFindAuthor"), + intro = jsonItem.readString("ruleFindIntroduce"), + kind = jsonItem.readString("ruleFindKind"), + bookUrl = jsonItem.readString("ruleFindNoteUrl"), + coverUrl = jsonItem.readString("ruleFindCoverUrl"), + lastChapter = jsonItem.readString("ruleFindLastChapter") + ) + ruleExplore = GSON.toJson(exploreRule) + val bookInfoRule = BookInfoRule( + init = jsonItem.readString("ruleBookInfoInit"), + name = jsonItem.readString("ruleBookName"), + author = jsonItem.readString("ruleBookAuthor"), + intro = jsonItem.readString("ruleIntroduce"), + kind = jsonItem.readString("ruleBookKind"), + coverUrl = jsonItem.readString("ruleCoverUrl"), + lastChapter = jsonItem.readString("ruleBookLastChapter"), + tocUrl = jsonItem.readString("ruleChapterUrl") + ) + ruleBookInfo = GSON.toJson(bookInfoRule) + val chapterRule = TocRule( + chapterList = jsonItem.readString("ruleChapterList"), + chapterName = jsonItem.readString("ruleChapterName"), + chapterUrl = jsonItem.readString("ruleContentUrl"), + nextTocUrl = jsonItem.readString("ruleChapterUrlNext") + ) + ruleToc = GSON.toJson(chapterRule) + val contentRule = ContentRule( + content = jsonItem.readString("ruleBookContent"), + nextContentUrl = jsonItem.readString("ruleContentUrlNext") + ) + ruleContent = GSON.toJson(contentRule) + } + } + } + return source + } + + private fun toNewUrl(oldUrl: String?): String? { if (oldUrl == null) return null val map = HashMap() var url: String = oldUrl @@ -46,9 +125,10 @@ object OldRule { return url } - fun uaToHeader(ua: String?): String? { + private fun uaToHeader(ua: String?): String? { if (ua.isNullOrEmpty()) return null val map = mapOf(Pair("user_agent", ua)) return GSON.toJson(map) } + } \ 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 0ae35a208..714b751cc 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 @@ -10,7 +10,6 @@ import io.legado.app.constant.AppConst import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.ReplaceRule -import io.legado.app.data.entities.rule.* import io.legado.app.utils.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast @@ -93,81 +92,19 @@ object Restore { // Book source val sourceFile = File(yuedu, "myBookSource.json") val bookSources = mutableListOf() - if (shelfFile.exists()) try { + if (sourceFile.exists()) try { doAsync { val items: List> = jsonPath.parse(sourceFile.readText()).read("$") - val existingSources = App.db.bookSourceDao().all.map { it.bookSourceUrl }.toSet() for (item in items) { val jsonItem = jsonPath.parse(item) - val source = BookSource() - source.bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: "" - if (source.bookSourceUrl.isBlank()) continue - if (source.bookSourceUrl in existingSources) continue - runCatching { - source.bookSourceName = jsonItem.readString("bookSourceName") ?: "" - source.bookSourceGroup = jsonItem.readString("bookSourceGroup") ?: "" - source.loginUrl = jsonItem.readString("loginUrl") - source.bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") - source.customOrder = jsonItem.readInt("serialNumber") ?: 0 - source.header = OldRule.uaToHeader(jsonItem.readString("httpUserAgent")) - source.searchUrl = OldRule.toNewUrl(jsonItem.readString("ruleSearchUrl")) - source.exploreUrl = OldRule.toNewUrl(jsonItem.readString("ruleFindUrl")) - if (source.exploreUrl.isNullOrBlank()) { - source.enabledExplore = false - } - val searchRule = SearchRule( - bookList = jsonItem.readString("ruleSearchList"), - name = jsonItem.readString("ruleSearchName"), - author = jsonItem.readString("ruleSearchAuthor"), - intro = jsonItem.readString("ruleSearchIntroduce"), - kind = jsonItem.readString("ruleSearchKind"), - bookUrl = jsonItem.readString("ruleSearchNoteUrl"), - coverUrl = jsonItem.readString("ruleSearchCoverUrl"), - lastChapter = jsonItem.readString("ruleSearchLastChapter") - ) - source.ruleSearch = GSON.toJson(searchRule) - val exploreRule = ExploreRule( - bookList = jsonItem.readString("ruleFindList"), - name = jsonItem.readString("ruleFindName"), - author = jsonItem.readString("ruleFindAuthor"), - intro = jsonItem.readString("ruleFindIntroduce"), - kind = jsonItem.readString("ruleFindKind"), - bookUrl = jsonItem.readString("ruleFindNoteUrl"), - coverUrl = jsonItem.readString("ruleFindCoverUrl"), - lastChapter = jsonItem.readString("ruleFindLastChapter") - ) - source.ruleExplore = GSON.toJson(exploreRule) - val bookInfoRule = BookInfoRule( - init = jsonItem.readString("ruleBookInfoInit"), - name = jsonItem.readString("ruleBookName"), - author = jsonItem.readString("ruleBookAuthor"), - intro = jsonItem.readString("ruleIntroduce"), - kind = jsonItem.readString("ruleBookKind"), - coverUrl = jsonItem.readString("ruleCoverUrl"), - lastChapter = jsonItem.readString("ruleBookLastChapter"), - tocUrl = jsonItem.readString("ruleChapterUrl") - ) - source.ruleBookInfo = GSON.toJson(bookInfoRule) - val chapterRule = TocRule( - chapterList = jsonItem.readString("ruleChapterList"), - chapterName = jsonItem.readString("ruleChapterName"), - chapterUrl = jsonItem.readString("ruleContentUrl"), - nextTocUrl = jsonItem.readString("ruleChapterUrlNext") - ) - source.ruleToc = GSON.toJson(chapterRule) - val contentRule = ContentRule( - content = jsonItem.readString("ruleBookContent"), - nextContentUrl = jsonItem.readString("ruleContentUrlNext") - ) - source.ruleContent = GSON.toJson(contentRule) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { + bookSources.add(it) } - - bookSources.add(source) } App.db.bookSourceDao().insert(*bookSources.toTypedArray()) } } catch (e: Exception) { - error(e.localizedMessage) + e.printStackTrace() } diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt index 0e815e15b..73646f3ff 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt @@ -18,6 +18,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.help.ItemTouchCallback +import io.legado.app.help.storage.Restore import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.qrcode.QrCodeActivity @@ -85,6 +86,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity supportFragmentManager, "groupManage" ) + R.id.menu_import_book_source_local -> Restore.importYueDuData(this) } if (item.groupId == R.id.source_group) { search_view.setQuery(item.title, true)