From c0ae964d5c3f54e49dd4d4b517da69470b7b4115 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 26 Sep 2019 11:41:29 +0800 Subject: [PATCH] up --- .../io/legado/app/help/storage/OldRule.kt | 9 +--- .../io/legado/app/help/storage/Restore.kt | 13 +++-- .../book/source/manage/BookSourceActivity.kt | 1 + .../book/source/manage/BookSourceViewModel.kt | 53 ++++++++++++++++++- .../io/legado/app/utils/StringExtensions.kt | 16 ++++++ 5 files changed, 78 insertions(+), 14 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 3c49817a8..9295aaa0d 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,10 +1,8 @@ package io.legado.app.help.storage -import com.jayway.jsonpath.Configuration -import com.jayway.jsonpath.JsonPath -import com.jayway.jsonpath.Option import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* +import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import io.legado.app.utils.readInt @@ -14,11 +12,6 @@ import java.util.regex.Pattern object OldRule { private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE) - private val jsonPath = JsonPath.using( - Configuration.builder() - .options(Option.SUPPRESS_EXCEPTIONS) - .build() - ) fun jsonToBookSource(json: String): BookSource? { var source: BookSource? = null 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 ad0db5710..593bbec5e 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 @@ -5,6 +5,7 @@ import android.util.Log 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.data.entities.Book @@ -21,11 +22,13 @@ import org.jetbrains.anko.uiThread import java.io.File object Restore { - private val jsonPath = JsonPath.using( - Configuration.builder() - .options(Option.SUPPRESS_EXCEPTIONS) - .build() - ) + val jsonPath: ParseContext by lazy { + JsonPath.using( + Configuration.builder() + .options(Option.SUPPRESS_EXCEPTIONS) + .build() + ) + } fun restore(path: String = defaultPath) { doAsync { diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 8c1b5b954..6dfc8f3a2 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -188,6 +188,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity cacheUrls.add(0, it) aCache.put("sourceUrl", cacheUrls.joinToString(",")) } + viewModel.importSource(it) } } cancelButton() diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 289b3a84a..3ae268728 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -2,10 +2,15 @@ package io.legado.app.ui.book.source.manage import android.app.Application import android.text.TextUtils +import com.jayway.jsonpath.JsonPath import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.api.IHttpGetApi import io.legado.app.data.entities.BookSource -import io.legado.app.utils.splitNotBlank +import io.legado.app.help.http.HttpHelper +import io.legado.app.help.storage.OldRule +import io.legado.app.help.storage.Restore.jsonPath +import io.legado.app.utils.* class BookSourceViewModel(application: Application) : BaseViewModel(application) { @@ -92,4 +97,50 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } } + + fun importSource(text: String) { + execute { + val text1 = text.trim() + if (text1.isJsonObject()) { + val json = JsonPath.parse(text1) + val urls = json.read>("$.sourceUrls") + if (!urls.isNullOrEmpty()) { + urls.forEach { importSourceUrl(it) } + } else { + OldRule.jsonToBookSource(text1)?.let { + App.db.bookSourceDao().insert(it) + } + } + } else if (text1.isJsonArray()) { + val bookSources = mutableListOf() + val items: List> = jsonPath.parse(text1).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { + bookSources.add(it) + } + } + App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + } else if (text1.isAbsUrl()) { + importSourceUrl(text1) + } else { + toast("格式不对") + } + }.onError { + toast(it.localizedMessage) + } + } + + private fun importSourceUrl(url: String) { + execute { + NetworkUtils.getBaseUrl(url)?.let { + val response = HttpHelper.getApiService(it).get(url, mapOf()).execute() + response.body()?.let { body -> + importSource(body) + } + } + }.onError { + toast(it.localizedMessage) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index a31dff411..a58e17c56 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -18,6 +18,22 @@ fun String?.isJson(): Boolean = this?.run { } } ?: false +fun String?.isJsonObject(): Boolean = this?.run { + val str = this.trim() + when { + str.startsWith("{") && str.endsWith("}") -> true + else -> false + } +} ?: false + +fun String?.isJsonArray(): Boolean = this?.run { + val str = this.trim() + when { + str.startsWith("[") && str.endsWith("]") -> true + else -> false + } +} ?: false + fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else this.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n")// 替换特定标签为换行符 .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符