|
|
@ -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 |
|
|
|