pull/35/head
kunfei 5 years ago
parent 6b4c9c16a8
commit c0ae964d5c
  1. 9
      app/src/main/java/io/legado/app/help/storage/OldRule.kt
  2. 13
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  4. 53
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  5. 16
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -1,10 +1,8 @@
package io.legado.app.help.storage 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.BookSource
import io.legado.app.data.entities.rule.* 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.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.readInt import io.legado.app.utils.readInt
@ -14,11 +12,6 @@ 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)
private val jsonPath = JsonPath.using(
Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build()
)
fun jsonToBookSource(json: String): BookSource? { fun jsonToBookSource(json: String): BookSource? {
var source: BookSource? = null var source: BookSource? = null

@ -5,6 +5,7 @@ import android.util.Log
import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option import com.jayway.jsonpath.Option
import com.jayway.jsonpath.ParseContext
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -21,11 +22,13 @@ import org.jetbrains.anko.uiThread
import java.io.File import java.io.File
object Restore { object Restore {
private val jsonPath = JsonPath.using( val jsonPath: ParseContext by lazy {
Configuration.builder() JsonPath.using(
.options(Option.SUPPRESS_EXCEPTIONS) Configuration.builder()
.build() .options(Option.SUPPRESS_EXCEPTIONS)
) .build()
)
}
fun restore(path: String = defaultPath) { fun restore(path: String = defaultPath) {
doAsync { doAsync {

@ -188,6 +188,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
cacheUrls.add(0, it) cacheUrls.add(0, it)
aCache.put("sourceUrl", cacheUrls.joinToString(",")) aCache.put("sourceUrl", cacheUrls.joinToString(","))
} }
viewModel.importSource(it)
} }
} }
cancelButton() cancelButton()

@ -2,10 +2,15 @@ package io.legado.app.ui.book.source.manage
import android.app.Application import android.app.Application
import android.text.TextUtils import android.text.TextUtils
import com.jayway.jsonpath.JsonPath
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.IHttpGetApi
import io.legado.app.data.entities.BookSource 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) { 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<List<String>>("$.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<BookSource>()
val items: List<Map<String, Any>> = 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<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
importSource(body)
}
}
}.onError {
toast(it.localizedMessage)
}
}
} }

@ -18,6 +18,22 @@ fun String?.isJson(): Boolean = this?.run {
} }
} ?: false } ?: 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 fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else
this.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n")// 替换特定标签为换行符 this.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n")// 替换特定标签为换行符
.replace("<[script>]*.*?>|&nbsp;".toRegex(), "")// 删除script标签对和空格转义符 .replace("<[script>]*.*?>|&nbsp;".toRegex(), "")// 删除script标签对和空格转义符

Loading…
Cancel
Save