Use kotlinx.serialization, remove jsoup dependency

This commit is contained in:
Him188 2020-05-22 19:56:02 +08:00
parent ef7ea024a0
commit f82d01e606
4 changed files with 53 additions and 85 deletions

View File

@ -34,8 +34,6 @@ dependencies {
api("net.mamoe.yamlkt:yamlkt:0.3.1") api("net.mamoe.yamlkt:yamlkt:0.3.1")
api("org.jsoup:jsoup:1.12.1")
api("org.jetbrains:annotations:19.0.0") api("org.jetbrains:annotations:19.0.0")
testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}") testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}")

View File

@ -1,89 +1,73 @@
@file:OptIn(MiraiExperimentalAPI::class)
package net.mamoe.mirai.console.center package net.mamoe.mirai.console.center
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import io.ktor.client.request.get import io.ktor.client.request.get
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
import kotlinx.serialization.json.* import kotlinx.serialization.Serializable
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import net.mamoe.mirai.console.utils.retryCatching import net.mamoe.mirai.console.utils.retryCatching
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import java.io.File import java.io.File
@OptIn(UnstableDefault::class)
internal val json = Json(JsonConfiguration(isLenient = true, ignoreUnknownKeys = true))
@OptIn(KtorExperimentalAPI::class)
internal val Http = HttpClient(CIO)
internal object CuiPluginCenter : PluginCenter { internal object CuiPluginCenter : PluginCenter {
var plugins: JsonArray? = null var plugins: List<PluginCenter.PluginInsight>? = null
/** /**
* 一页10个吧,pageMinNum=1 * 一页 10 pageMinNum=1
*/ */
override suspend fun fetchPlugin(page: Int): Map<String, PluginCenter.PluginInsight> { override suspend fun fetchPlugin(page: Int): Map<String, PluginCenter.PluginInsight> {
check(page > 0) check(page > 0)
val startIndex = (page - 1) * 10 val startIndex = (page - 1) * 10
val endIndex = startIndex + 9 val endIndex = startIndex + 9
val map = mutableMapOf<String, PluginCenter.PluginInsight>() val map = mutableMapOf<String, PluginCenter.PluginInsight>()
(startIndex until endIndex).forEach { (startIndex until endIndex).forEach { index ->
if (plugins == null) { val plugins = plugins ?: kotlin.run {
refresh() refresh()
} plugins
if (it >= plugins!!.size) { } ?: return mapOf()
if (index >= plugins.size) {
return@forEach return@forEach
} }
val info = plugins!![it]
with(info.jsonObject) { map[name] = plugins[index]
map[this["name"]!!.toString()] = PluginCenter.PluginInsight(
this["name"]?.primitive?.content ?: "",
this["version"]?.primitive?.content ?: "",
this["core"]?.primitive?.content ?: "",
this["console"]?.primitive?.content ?: "",
this["author"]?.primitive?.content ?: "",
this["description"]?.primitive?.content ?: "",
this["tags"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(),
this["commands"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf()
)
}
} }
return map return map
} }
@OptIn(KtorExperimentalAPI::class)
private val Http = HttpClient(CIO)
override suspend fun findPlugin(name: String): PluginCenter.PluginInfo? { override suspend fun findPlugin(name: String): PluginCenter.PluginInfo? {
val result = retryCatching(3) { val result = retryCatching(3) {
Http.get<String>("https://miraiapi.jasonczc.cn/getPluginDetailedInfo?name=$name") Http.get<String>("https://miraiapi.jasonczc.cn/getPluginDetailedInfo?name=$name")
}.recover { }.getOrElse { return null }
return null if (result == "err:not found") return null
}.getOrNull() ?: return null
if (result == "err:not found") {
return null
}
return result.asJson().run {
PluginCenter.PluginInfo(
this["name"]?.primitive?.content ?: "",
this["version"]?.primitive?.content ?: "",
this["core"]?.primitive?.content ?: "",
this["console"]?.primitive?.content ?: "",
this["tags"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(),
this["author"]?.primitive?.content ?: "",
this["contact"]?.primitive?.content ?: "",
this["description"]?.primitive?.content ?: "",
this["usage"]?.primitive?.content ?: "",
this["vsc"]?.primitive?.content ?: "",
this["commands"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(),
this["changeLog"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf()
)
}
return json.parse(PluginCenter.PluginInfo.serializer(), result)
} }
override suspend fun refresh() { override suspend fun refresh() {
val results = Http.get<String>("https://miraiapi.jasonczc.cn/getPluginList").asJson()
if (!(results.containsKey("success") && results["success"]?.boolean == true)) { @Serializable
error("Failed to fetch plugin list from Cui Cloud") data class Result(
} val success: Boolean,
plugins = results["result"]?.jsonArray//先不解析 val result: List<PluginCenter.PluginInsight>
)
val result = json.parse(Result.serializer(), Http.get("https://miraiapi.jasonczc.cn/getPluginList"))
check(result.success) { "Failed to fetch plugin list from Cui Cloud" }
plugins = result.result
} }
override suspend fun <T : Any> T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit): File { override suspend fun <T : Any> T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit): File {
@ -112,15 +96,6 @@ internal object CuiPluginCenter : PluginCenter {
*/ */
} }
override val name: String override val name: String get() = "崔云"
get() = "崔云"
private val json = Json(JsonConfiguration.Stable)
private fun String.asJson(): JsonObject {
return json.parseJson(this).jsonObject
}
} }

View File

@ -1,13 +1,20 @@
package net.mamoe.mirai.console.center package net.mamoe.mirai.console.center
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import java.io.File import java.io.File
@MiraiExperimentalAPI
interface PluginCenter { interface PluginCenter {
@Serializable
data class PluginInsight( data class PluginInsight(
val name: String, val name: String,
val version: String, val version: String,
@SerialName("core")
val coreVersion: String, val coreVersion: String,
@SerialName("console")
val consoleVersion: String, val consoleVersion: String,
val author: String, val author: String,
val description: String, val description: String,
@ -15,10 +22,13 @@ interface PluginCenter {
val commands: List<String> val commands: List<String>
) )
@Serializable
data class PluginInfo( data class PluginInfo(
val name: String, val name: String,
val version: String, val version: String,
@SerialName("core")
val coreVersion: String, val coreVersion: String,
@SerialName("console")
val consoleVersion: String, val consoleVersion: String,
val tags: List<String>, val tags: List<String>,
val author: String, val author: String,
@ -32,7 +42,7 @@ interface PluginCenter {
/** /**
* 获取一些中心的插件基本信息, * 获取一些中心的插件基本信息,
* 能获取到多少由实际的PluginCenter决定 * 能获取到多少由实际的 [PluginCenter] 决定
* 返回 插件名->Insight * 返回 插件名->Insight
*/ */
suspend fun fetchPlugin(page: Int): Map<String, PluginInsight> suspend fun fetchPlugin(page: Int): Map<String, PluginInsight>
@ -41,17 +51,18 @@ interface PluginCenter {
* 尝试获取到某个插件 by 全名, case sensitive * 尝试获取到某个插件 by 全名, case sensitive
* null 则没有 * null 则没有
*/ */
suspend fun findPlugin(name:String):PluginInfo? suspend fun findPlugin(name: String): PluginInfo?
suspend fun <T:Any> T.downloadPlugin(name:String, progressListener:T.(Float) -> Unit): File suspend fun <T : Any> T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit): File
suspend fun downloadPlugin(name:String, progressListener:PluginCenter.(Float) -> Unit): File = downloadPlugin<PluginCenter>(name,progressListener) suspend fun downloadPlugin(name: String, progressListener: PluginCenter.(Float) -> Unit): File =
downloadPlugin<PluginCenter>(name, progressListener)
/** /**
* 刷新 * 刷新
*/ */
suspend fun refresh() suspend fun refresh()
val name:String val name: String
} }

View File

@ -1,16 +0,0 @@
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.setting
object ValueSerializerMark
/*
* More generic ones
*/