feat: add minecraft wiki content query
This commit is contained in:
6 files changed
+112
-25
No files matched your search
@@ -15,6 +15,7 @@ version = fancyBotVersion
|
|||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
maven("https://oss.sonatype.org/content/repositories/snapshots")
|
||||||
maven("https://repo.rtast.cn/api/v4/projects/33/packages/maven")
|
maven("https://repo.rtast.cn/api/v4/projects/33/packages/maven")
|
||||||
maven("https://repo.rtast.cn/api/v4/projects/19/packages/maven")
|
maven("https://repo.rtast.cn/api/v4/projects/19/packages/maven")
|
||||||
}
|
}
|
||||||
@@ -45,6 +46,7 @@ dependencies {
|
|||||||
implementation(libs.logback.classic)
|
implementation(libs.logback.classic)
|
||||||
implementation(libs.slf4j.api)
|
implementation(libs.slf4j.api)
|
||||||
implementation(libs.aws.s3.sdk)
|
implementation(libs.aws.s3.sdk)
|
||||||
|
implementation(libs.sweble.wikitext)
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.build {
|
tasks.build {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ zxingVersion = "3.5.3"
|
|||||||
logbackClassicVersion = "1.5.8"
|
logbackClassicVersion = "1.5.8"
|
||||||
sl4jVersion = "2.0.16"
|
sl4jVersion = "2.0.16"
|
||||||
awsS3SDKVersion = "2.28.18"
|
awsS3SDKVersion = "2.28.18"
|
||||||
|
blikiWikiVersion = "3.1.1-SNAPSHOT"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
||||||
@@ -31,6 +32,7 @@ zxing-javase = { group = "com.google.zxing", name = "javase", version.ref = "zxi
|
|||||||
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logbackClassicVersion" }
|
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logbackClassicVersion" }
|
||||||
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "sl4jVersion" }
|
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "sl4jVersion" }
|
||||||
aws-s3-sdk = { group = "software.amazon.awssdk", name = "s3", version.ref = "awsS3SDKVersion" }
|
aws-s3-sdk = { group = "software.amazon.awssdk", name = "s3", version.ref = "awsS3SDKVersion" }
|
||||||
|
sweble-wikitext = { group = "info.bliki.wiki", name = "bliki-core", version.ref = "blikiWikiVersion" }
|
||||||
|
|
||||||
[bundles]
|
[bundles]
|
||||||
|
|
||||||
|
|||||||
@@ -9,18 +9,22 @@
|
|||||||
package cn.rtast.fancybot.commands.lookup
|
package cn.rtast.fancybot.commands.lookup
|
||||||
|
|
||||||
import cn.rtast.fancybot.annotations.CommandDescription
|
import cn.rtast.fancybot.annotations.CommandDescription
|
||||||
|
import cn.rtast.fancybot.commands.lookup.WikipediaCommand.Companion.extractPlainTextFromHtml
|
||||||
import cn.rtast.fancybot.configManager
|
import cn.rtast.fancybot.configManager
|
||||||
import cn.rtast.fancybot.entity.mc.DecodedSkin
|
import cn.rtast.fancybot.entity.mc.DecodedSkin
|
||||||
import cn.rtast.fancybot.entity.mc.MCVersion
|
import cn.rtast.fancybot.entity.mc.MCVersion
|
||||||
|
import cn.rtast.fancybot.entity.mc.MCWikiPageResponse
|
||||||
import cn.rtast.fancybot.entity.mc.MCWikiSearchResponse
|
import cn.rtast.fancybot.entity.mc.MCWikiSearchResponse
|
||||||
import cn.rtast.fancybot.entity.mc.Skin
|
import cn.rtast.fancybot.entity.mc.Skin
|
||||||
import cn.rtast.fancybot.entity.mc.Username
|
import cn.rtast.fancybot.entity.mc.Username
|
||||||
import cn.rtast.fancybot.enums.mc.MCVersionType
|
import cn.rtast.fancybot.enums.mc.MCVersionType
|
||||||
import cn.rtast.fancybot.util.Http
|
import cn.rtast.fancybot.util.Http
|
||||||
|
import cn.rtast.fancybot.util.Logger
|
||||||
import cn.rtast.fancybot.util.misc.isFullyTransparent
|
import cn.rtast.fancybot.util.misc.isFullyTransparent
|
||||||
import cn.rtast.fancybot.util.misc.scaleImage
|
import cn.rtast.fancybot.util.misc.scaleImage
|
||||||
import cn.rtast.fancybot.util.misc.toByteArray
|
import cn.rtast.fancybot.util.misc.toByteArray
|
||||||
import cn.rtast.fancybot.util.misc.toURL
|
import cn.rtast.fancybot.util.misc.toURL
|
||||||
|
import cn.rtast.fancybot.util.str.convertToHTML
|
||||||
import cn.rtast.fancybot.util.str.decodeToString
|
import cn.rtast.fancybot.util.str.decodeToString
|
||||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||||
import cn.rtast.fancybot.util.str.fromJson
|
import cn.rtast.fancybot.util.str.fromJson
|
||||||
@@ -67,32 +71,64 @@ class MCVersionCommand : BaseCommand() {
|
|||||||
class MinecraftWikiCommand : BaseCommand() {
|
class MinecraftWikiCommand : BaseCommand() {
|
||||||
override val commandNames = listOf("mcwiki")
|
override val commandNames = listOf("mcwiki")
|
||||||
|
|
||||||
private val searchApiUrl = "https://zh.minecraft.wiki"
|
private val baseApiUrl = "https://zh.minecraft.wiki/rest.php/v1"
|
||||||
|
private val logger = Logger.getLogger<MinecraftWikiCommand>()
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
val searchKeyword = args.joinToString(" ").trim()
|
val action = args.first()
|
||||||
val messages = mutableListOf<MessageChain>()
|
when (action) {
|
||||||
val headerMessage = listOf("结果如下:").asMessageChain()
|
"s", "搜索" -> {
|
||||||
messages.add(headerMessage)
|
val searchKeyword = args.drop(1).joinToString(" ").trim()
|
||||||
Http.get<MCWikiSearchResponse>(
|
val messages = mutableListOf<MessageChain>()
|
||||||
"$searchApiUrl/rest.php/v1/search/title",
|
val headerMessage = listOf("结果如下:").asMessageChain()
|
||||||
mapOf("q" to searchKeyword, "limit" to 10)
|
messages.add(headerMessage)
|
||||||
).pages.forEach {
|
Http.get<MCWikiSearchResponse>(
|
||||||
val msg = MessageChain.Builder()
|
"$baseApiUrl/search/title",
|
||||||
val imageUrl = it.thumbnail
|
mapOf("q" to searchKeyword, "limit" to 10)
|
||||||
if (imageUrl != null) {
|
).pages.forEach {
|
||||||
val imageBase64 = imageUrl.url.toURL().readBytes().encodeToBase64()
|
val msg = MessageChain.Builder()
|
||||||
msg.addImage(imageBase64, true)
|
val imageUrl = it.thumbnail
|
||||||
|
if (imageUrl != null) {
|
||||||
|
val imageBase64 = imageUrl.url.toURL().readBytes().encodeToBase64()
|
||||||
|
msg.addImage(imageBase64, true)
|
||||||
|
}
|
||||||
|
msg.addText("标题: ${it.title}")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("ID: ${it.id}")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("https://zh.minecraft.wiki/w/${it.title.uriEncode}")
|
||||||
|
messages.add(msg.build())
|
||||||
|
}
|
||||||
|
message.reply(messages.asNode(configManager.selfId))
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
val title = args.joinToString(" ").trim()
|
||||||
|
var response = Http.get<MCWikiPageResponse>("$baseApiUrl/page/$title")
|
||||||
|
if (response.httpCode != null) {
|
||||||
|
logger.info("页面($title)不存在")
|
||||||
|
message.reply("页面($title)不存在")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (response.redirectTarget != null) {
|
||||||
|
logger.info("重定向Wiki页面到: ${response.redirectTarget}")
|
||||||
|
response = Http.get<MCWikiPageResponse>(response.redirectTarget)
|
||||||
|
}
|
||||||
|
val messages = mutableListOf<MessageChain>()
|
||||||
|
val headerMsg = listOf("标题${response.title}的内容如下").asMessageChain()
|
||||||
|
val footerMsg = listOf(
|
||||||
|
"数据来源: https://zh.minecraft.wiki",
|
||||||
|
"协议: ${response.license.title}(${response.license.url})"
|
||||||
|
).asMessageChain(true)
|
||||||
|
val bodyMsg = MessageChain.Builder()
|
||||||
|
.addText(response.source.convertToHTML().extractPlainTextFromHtml())
|
||||||
|
.build()
|
||||||
|
messages.add(headerMsg)
|
||||||
|
messages.add(bodyMsg)
|
||||||
|
messages.add(footerMsg)
|
||||||
|
message.reply(messages.asNode(configManager.selfId))
|
||||||
}
|
}
|
||||||
msg.addText("标题: ${it.title}")
|
|
||||||
.addNewLine()
|
|
||||||
.addText("ID: ${it.id}")
|
|
||||||
.addNewLine()
|
|
||||||
.addText("https://zh.minecraft.wiki/w/${it.title.uriEncode}")
|
|
||||||
messages.add(msg.build())
|
|
||||||
}
|
}
|
||||||
message.reply(messages.asNode(configManager.selfId))
|
|
||||||
repeat(3) { message.sender.poke() }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,9 +25,11 @@ class WikipediaCommand : BaseCommand() {
|
|||||||
|
|
||||||
private val wikipediaAPI = "https://proxy.rtast.cn/https/zh.wikipedia.org/w/api.php"
|
private val wikipediaAPI = "https://proxy.rtast.cn/https/zh.wikipedia.org/w/api.php"
|
||||||
|
|
||||||
private fun String.extractPlainTextFromHtml(): String {
|
companion object {
|
||||||
val doc = Jsoup.parse(this)
|
fun String.extractPlainTextFromHtml(): String {
|
||||||
return doc.text()
|
val doc = Jsoup.parse(this)
|
||||||
|
return doc.text()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/10/10
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.entity.mc
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class MCWikiPageResponse(
|
||||||
|
@SerializedName("redirect_target")
|
||||||
|
val redirectTarget: String?,
|
||||||
|
val httpCode: Int?,
|
||||||
|
val id: Int,
|
||||||
|
val title: String,
|
||||||
|
val latest: Latest,
|
||||||
|
val source: String,
|
||||||
|
val license: License
|
||||||
|
) {
|
||||||
|
data class Latest(
|
||||||
|
val id: Int,
|
||||||
|
val timestamp: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class License(
|
||||||
|
val url: String,
|
||||||
|
val title: String,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/10/10
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.util.str
|
||||||
|
|
||||||
|
import info.bliki.wiki.model.WikiModel
|
||||||
|
|
||||||
|
fun String.convertToHTML(): String {
|
||||||
|
return WikiModel.toHtml(this)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user