pull/32/head
kunfei 5 years ago
parent 3a2adca253
commit c874c6e816
  1. 84
      app/src/main/java/io/legado/app/help/storage/OldRule.kt
  2. 71
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 2
      app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt

@ -1,13 +1,92 @@
package io.legado.app.help.storage 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.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.readInt
import io.legado.app.utils.readString
import java.util.regex.Pattern import java.util.regex.Pattern
object OldRule { object OldRule {
private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", 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<BookSource>(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 if (oldUrl == null) return null
val map = HashMap<String, String>() val map = HashMap<String, String>()
var url: String = oldUrl var url: String = oldUrl
@ -46,9 +125,10 @@ object OldRule {
return url return url
} }
fun uaToHeader(ua: String?): String? { private fun uaToHeader(ua: String?): String? {
if (ua.isNullOrEmpty()) return null if (ua.isNullOrEmpty()) return null
val map = mapOf(Pair("user_agent", ua)) val map = mapOf(Pair("user_agent", ua))
return GSON.toJson(map) return GSON.toJson(map)
} }
} }

@ -10,7 +10,6 @@ import io.legado.app.constant.AppConst
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.data.entities.rule.*
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -93,81 +92,19 @@ object Restore {
// Book source // Book source
val sourceFile = File(yuedu, "myBookSource.json") val sourceFile = File(yuedu, "myBookSource.json")
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
if (shelfFile.exists()) try { if (sourceFile.exists()) try {
doAsync { doAsync {
val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$")
val existingSources = App.db.bookSourceDao().all.map { it.bookSourceUrl }.toSet()
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
val source = BookSource() OldRule.jsonToBookSource(jsonItem.jsonString())?.let {
source.bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: "" bookSources.add(it)
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)
} }
bookSources.add(source)
} }
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) App.db.bookSourceDao().insert(*bookSources.toTypedArray())
} }
} catch (e: Exception) { } catch (e: Exception) {
error(e.localizedMessage) e.printStackTrace()
} }

@ -18,6 +18,7 @@ import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.ItemTouchCallback 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.ATH
import io.legado.app.lib.theme.primaryTextColor import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.qrcode.QrCodeActivity
@ -85,6 +86,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
supportFragmentManager, supportFragmentManager,
"groupManage" "groupManage"
) )
R.id.menu_import_book_source_local -> Restore.importYueDuData(this)
} }
if (item.groupId == R.id.source_group) { if (item.groupId == R.id.source_group) {
search_view.setQuery(item.title, true) search_view.setQuery(item.title, true)

Loading…
Cancel
Save