pull/1751/head
kunfei 3 years ago
parent 518781985b
commit b477f2eecb
  1. 16
      app/src/main/java/io/legado/app/api/controller/BookSourceController.kt
  2. 4
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 36
      app/src/main/java/io/legado/app/help/SourceAnalyzer.kt
  4. 7
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  5. 2
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt
  6. 5
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  7. 8
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt

@ -7,7 +7,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.msg
object BookSourceController { object BookSourceController {
@ -23,8 +22,7 @@ object BookSourceController {
fun saveSource(postData: String?): ReturnData { fun saveSource(postData: String?): ReturnData {
val returnData = ReturnData() val returnData = ReturnData()
postData ?: return returnData.setErrorMsg("数据不能为空") postData ?: return returnData.setErrorMsg("数据不能为空")
kotlin.runCatching { val bookSource = BookSource.fromJson(postData).getOrNull()
val bookSource = BookSource.fromJson(postData)
if (bookSource != null) { if (bookSource != null) {
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
returnData.setErrorMsg("源名称和URL不能为空") returnData.setErrorMsg("源名称和URL不能为空")
@ -35,17 +33,16 @@ object BookSourceController {
} else { } else {
returnData.setErrorMsg("转换源失败") returnData.setErrorMsg("转换源失败")
} }
}.onFailure {
returnData.setErrorMsg(it.msg)
}
return returnData return returnData
} }
fun saveSources(postData: String?): ReturnData { fun saveSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("数据为空") postData ?: return ReturnData().setErrorMsg("数据为空")
val okSources = arrayListOf<BookSource>() val okSources = arrayListOf<BookSource>()
val bookSources = BookSource.fromJsonArray(postData) val bookSources = BookSource.fromJsonArray(postData).getOrNull()
if (bookSources.isNotEmpty()) { if (bookSources.isNullOrEmpty()) {
return ReturnData().setErrorMsg("转换源失败")
}
bookSources.forEach { bookSource -> bookSources.forEach { bookSource ->
if (bookSource.bookSourceName.isNotBlank() if (bookSource.bookSourceName.isNotBlank()
&& bookSource.bookSourceUrl.isNotBlank() && bookSource.bookSourceUrl.isNotBlank()
@ -54,9 +51,6 @@ object BookSourceController {
okSources.add(bookSource) okSources.add(bookSource)
} }
} }
} else {
return ReturnData().setErrorMsg("转换源失败")
}
return ReturnData().setData(okSources) return ReturnData().setData(okSources)
} }

@ -204,11 +204,11 @@ data class BookSource(
companion object { companion object {
fun fromJson(json: String): BookSource? { fun fromJson(json: String): Result<BookSource> {
return SourceAnalyzer.jsonToBookSource(json) return SourceAnalyzer.jsonToBookSource(json)
} }
fun fromJsonArray(json: String): List<BookSource> { fun fromJsonArray(json: String): Result<MutableList<BookSource>> {
return SourceAnalyzer.jsonToBookSources(json) return SourceAnalyzer.jsonToBookSources(json)
} }

@ -7,6 +7,7 @@ import io.legado.app.constant.AppLog
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.exception.NoStackTraceException
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.InputStream import java.io.InputStream
@ -17,16 +18,30 @@ object SourceAnalyzer {
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 jsonToBookSources(json: String): List<BookSource> { fun jsonToBookSources(json: String): Result<MutableList<BookSource>> {
return kotlin.runCatching {
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
when {
json.isJsonArray() -> {
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
jsonToBookSource(jsonItem.jsonString())?.let { jsonToBookSource(jsonItem.jsonString()).getOrThrow().let {
bookSources.add(it)
}
}
}
json.isJsonObject() -> {
jsonToBookSource(json).getOrThrow().let {
bookSources.add(it) bookSources.add(it)
} }
} }
return bookSources else -> {
throw NoStackTraceException("格式不对")
}
}
bookSources
}
} }
fun jsonToBookSources(inputStream: InputStream): Result<MutableList<BookSource>> { fun jsonToBookSources(inputStream: InputStream): Result<MutableList<BookSource>> {
@ -36,14 +51,14 @@ object SourceAnalyzer {
val items: List<Map<String, Any>> = jsonPath.parse(inputStream).read("$") val items: List<Map<String, Any>> = jsonPath.parse(inputStream).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
jsonToBookSource(jsonItem.jsonString())?.let { jsonToBookSource(jsonItem.jsonString()).getOrThrow().let {
bookSources.add(it) bookSources.add(it)
} }
} }
}.onFailure { }.onFailure {
val item: Map<String, Any> = jsonPath.parse(inputStream).read("$") val item: Map<String, Any> = jsonPath.parse(inputStream).read("$")
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
jsonToBookSource(jsonItem.jsonString())?.let { jsonToBookSource(jsonItem.jsonString()).getOrThrow().let {
bookSources.add(it) bookSources.add(it)
} }
} }
@ -51,17 +66,18 @@ object SourceAnalyzer {
} }
} }
fun jsonToBookSource(json: String): BookSource? { fun jsonToBookSource(json: String): Result<BookSource> {
val source = BookSource() val source = BookSource()
val sourceAny = GSON.fromJsonObject<BookSourceAny>(json.trim()) val sourceAny = GSON.fromJsonObject<BookSourceAny>(json.trim())
.onFailure { .onFailure {
AppLog.put("转化书源出错", it) AppLog.put("转化书源出错", it)
}.getOrNull() }.getOrNull()
try { return kotlin.runCatching {
if (sourceAny?.ruleToc == null) { if (sourceAny?.ruleToc == null) {
source.apply { source.apply {
val jsonItem = jsonPath.parse(json.trim()) val jsonItem = jsonPath.parse(json.trim())
bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: return null bookSourceUrl = jsonItem.readString("bookSourceUrl")
?: throw NoStackTraceException("格式不对")
bookSourceName = jsonItem.readString("bookSourceName") ?: "" bookSourceName = jsonItem.readString("bookSourceName") ?: ""
bookSourceGroup = jsonItem.readString("bookSourceGroup") bookSourceGroup = jsonItem.readString("bookSourceGroup")
loginUrl = jsonItem.readString("loginUrl") loginUrl = jsonItem.readString("loginUrl")
@ -189,10 +205,8 @@ object SourceAnalyzer {
.getOrNull() .getOrNull()
} }
} }
} catch (e: Exception) { source
e.printOnDebug()
} }
return source
} }
@Keep @Keep

@ -91,9 +91,10 @@ object ImportOldData {
} }
fun importOldSource(json: String): Int { fun importOldSource(json: String): Int {
val bookSources = BookSource.fromJsonArray(json) val count = BookSource.fromJsonArray(json).onSuccess {
appDb.bookSourceDao.insert(*bookSources.toTypedArray()) appDb.bookSourceDao.insert(*it.toTypedArray())
return bookSources.size }.getOrNull()?.size
return count ?: 0
} }
private fun importOldReplaceRule(json: String): Int { private fun importOldReplaceRule(json: String): Int {

@ -189,7 +189,7 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie
override fun onCodeSave(code: String, requestId: String?) { override fun onCodeSave(code: String, requestId: String?) {
requestId?.toInt()?.let { requestId?.toInt()?.let {
BookSource.fromJson(code)?.let { source -> BookSource.fromJson(code).getOrNull()?.let { source ->
viewModel.allSources[it] = source viewModel.allSources[it] = source
adapter.setItem(it, source) adapter.setItem(it, source)
} }

@ -97,13 +97,12 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
importSourceUrl(it) importSourceUrl(it)
} }
} else { } else {
BookSource.fromJson(mText)?.let { BookSource.fromJson(mText).getOrThrow().let {
allSources.add(it) allSources.add(it)
} }
} }
} }
mText.isJsonArray() -> { mText.isJsonArray() -> BookSource.fromJsonArray(mText).getOrThrow().let { items ->
val items = BookSource.fromJsonArray(mText)
allSources.addAll(items) allSources.addAll(items)
} }
mText.isAbsUrl() -> { mText.isAbsUrl() -> {

@ -86,14 +86,12 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
text.isJsonArray() -> { text.isJsonArray() -> {
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$") val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
val jsonItem = jsonPath.parse(items[0]) val jsonItem = jsonPath.parse(items[0])
BookSource.fromJson(jsonItem.jsonString()) BookSource.fromJson(jsonItem.jsonString()).getOrThrow()
} }
text.isJsonObject() -> { text.isJsonObject() -> {
BookSource.fromJson(text) BookSource.fromJson(text).getOrThrow()
}
else -> {
null
} }
else -> throw NoStackTraceException("格式不对")
} }
} }

Loading…
Cancel
Save