feat: add image bed api
This commit is contained in:
10 files changed
+91
-31
No files matched your search
@@ -41,6 +41,7 @@ val gson: Gson = GsonBuilder()
|
||||
|
||||
const val ROOT_PATH = "./data"
|
||||
const val ASSETS_BASE_URL = "https://static.rtast.cn"
|
||||
const val API_RTAST_URL = "https://api.rtast.cn"
|
||||
|
||||
val DEFAULT_CONFIG = Config(
|
||||
ncmAPI = "https://ncm.rtast.cn",
|
||||
@@ -67,7 +68,9 @@ val DEFAULT_CONFIG = Config(
|
||||
qqMusicApiUrl = "http://127.0.0.1:3200",
|
||||
startUpNoticeUser = 114514L,
|
||||
selfId = 114514L,
|
||||
tianXingApiKey = "1145141919810"
|
||||
tianXingApiKey = "1145141919810",
|
||||
githubUser = "RTAkland",
|
||||
githubImageRepo = "Static-Images"
|
||||
)
|
||||
|
||||
val configManager = ConfigManager()
|
||||
|
||||
@@ -7,10 +7,13 @@
|
||||
|
||||
package cn.rtast.fancybot.commands.misc
|
||||
|
||||
import cn.rtast.fancybot.API_RTAST_URL
|
||||
import cn.rtast.fancybot.annotations.CommandDescription
|
||||
import cn.rtast.fancybot.entity.ShortLink
|
||||
import cn.rtast.fancybot.entity.shortlink.ShortLinkPayload
|
||||
import cn.rtast.fancybot.entity.shortlink.ShortLinkResponse
|
||||
import cn.rtast.fancybot.util.Http
|
||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||
import cn.rtast.fancybot.util.str.toJson
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OneBotListener
|
||||
@@ -19,7 +22,16 @@ import cn.rtast.rob.util.ob.OneBotListener
|
||||
class ShortLinkCommand : BaseCommand() {
|
||||
override val commandNames = listOf("/s")
|
||||
|
||||
private val shortLinkApi = "https://api.rtast.cn"
|
||||
|
||||
companion object {
|
||||
fun String.makeShortLink(): String {
|
||||
val shortLink = Http.post<ShortLinkResponse>(
|
||||
"$API_RTAST_URL/short_link",
|
||||
ShortLinkPayload(this).toJson()
|
||||
)
|
||||
return "$API_RTAST_URL/s?rnd_id=${shortLink.id}"
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||
if (args.isEmpty()) {
|
||||
@@ -27,10 +39,7 @@ class ShortLinkCommand : BaseCommand() {
|
||||
return
|
||||
}
|
||||
val target = args.first().encodeToBase64()
|
||||
val shortLink = Http.post<ShortLink>(
|
||||
"$shortLinkApi/short_link",
|
||||
params = mapOf("target" to target)
|
||||
)
|
||||
message.reply("$shortLinkApi/s?rnd_id=${shortLink.id}")
|
||||
val shortLink = target.makeShortLink()
|
||||
message.reply(shortLink)
|
||||
}
|
||||
}
|
||||
@@ -113,15 +113,25 @@ class AsciiArtCommand : BaseCommand() {
|
||||
val asciiFrames = mutableListOf<BufferedImage>()
|
||||
val width = frames.first().width
|
||||
val height = frames.first().height
|
||||
frames.forEach { asciiFrames.add(it.convertToAscii().saveAsciiArtToImage(width, height)) }
|
||||
val gifBytes = decoder.makeGif(asciiFrames)
|
||||
val gifBase64 = gifBytes.encodeToBase64()
|
||||
logger.info("处理后的图片大小: ${gifBytes.size}字节, base64大小: ${gifBase64.length * 2}字节")
|
||||
message.reply(MessageChain.Builder().addImage(gifBase64, true).build())
|
||||
frames.forEachIndexed { index, item ->
|
||||
asciiFrames.add(item.convertToAscii().saveAsciiArtToImage(width, height))
|
||||
logger.info("帧:${index}处理完成")
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
message.reply("处理GIF失败")
|
||||
logger.info("gif处理失败")
|
||||
logger.info("合成GIF中...")
|
||||
val gifBytes = decoder.makeGif(asciiFrames)
|
||||
logger.info("合并完成")
|
||||
val gifBase64 = gifBytes.encodeToBase64()
|
||||
logger.info("处理后的图片大小: ${gifBytes.size}字节")
|
||||
val msg = MessageChain.Builder()
|
||||
.addImage(gifBase64, true)
|
||||
message.reply(msg.build())
|
||||
}
|
||||
} catch (_: OutOfMemoryError) {
|
||||
message.reply("GIF处理失败: 内存溢出")
|
||||
logger.info("GIF处理失败: 内存溢出")
|
||||
} catch (e: Exception) {
|
||||
message.reply("处理GIF失败: ${e.message}")
|
||||
logger.info("gif处理失败: ${e.message}")
|
||||
}
|
||||
} else {
|
||||
message.reply("回复错误已取消本次操作")
|
||||
|
||||
@@ -36,4 +36,6 @@ data class Config(
|
||||
val startUpNoticeUser: Long,
|
||||
val selfId: Long,
|
||||
val tianXingApiKey: String,
|
||||
val githubUser: String,
|
||||
val githubImageRepo: String,
|
||||
)
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/9/22
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
data class ShortLink(
|
||||
val id: String
|
||||
)
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package cn.rtast.fancybot.items
|
||||
|
||||
import cn.rtast.fancybot.API_RTAST_URL
|
||||
import cn.rtast.fancybot.entity.setu.Setu
|
||||
import cn.rtast.fancybot.niuziManager
|
||||
import cn.rtast.fancybot.util.Http
|
||||
@@ -27,7 +28,7 @@ class SetuItem : Item() {
|
||||
message: GroupMessage,
|
||||
after: Double
|
||||
): MessageChain.Builder {
|
||||
val response = Http.get("https://api.rtast.cn/api/setu")
|
||||
val response = Http.get("$API_RTAST_URL/api/setu")
|
||||
.fromArrayJson<List<Setu>>().first()
|
||||
if (response.r18) {
|
||||
niuziManager.updateLength(message.sender.userId, itemPrice)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package cn.rtast.fancybot.items
|
||||
|
||||
import cn.rtast.fancybot.API_RTAST_URL
|
||||
import cn.rtast.fancybot.ROOT_PATH
|
||||
import cn.rtast.fancybot.configManager
|
||||
import cn.rtast.fancybot.entity.setu.Setu
|
||||
@@ -48,7 +49,7 @@ private fun getImages(r18: Boolean): NodeMessageChain {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val response = Http.get("https://api.rtast.cn/api/setu?size=10").fromArrayJson<List<Setu>>()
|
||||
val response = Http.get("$API_RTAST_URL/api/setu?size=10").fromArrayJson<List<Setu>>()
|
||||
response.filter { !it.r18 }.map { it.urls.large }.forEach {
|
||||
try {
|
||||
val imageBase64 = URI(it).toURL().readBytes().encodeToBase64()
|
||||
|
||||
@@ -10,14 +10,15 @@ package cn.rtast.fancybot.util
|
||||
import cn.rtast.fancybot.util.str.fromJson
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
|
||||
object Http {
|
||||
|
||||
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||
private val jsonHeader = mapOf(
|
||||
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||
val jsonHeader = mapOf(
|
||||
"Content-Type" to "application/json; charset=utf-8",
|
||||
"Accept" to "application/json"
|
||||
)
|
||||
@@ -136,4 +137,45 @@ object Http {
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
inline fun <reified T> post(
|
||||
url: String,
|
||||
form: MultipartBody,
|
||||
headers: Map<String, String>? = null
|
||||
): T {
|
||||
val request = Request.Builder()
|
||||
.post(form)
|
||||
.url(buildParams(url, headers))
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||
}
|
||||
|
||||
inline fun <reified T> put(
|
||||
url: String,
|
||||
jsonBody: String,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null
|
||||
): T {
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.url(paramsUrl)
|
||||
.put(jsonBody.toRequestBody(jsonMediaType))
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||
}
|
||||
|
||||
fun put(
|
||||
url: String,
|
||||
jsonBody: String,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null
|
||||
): String {
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.url(paramsUrl)
|
||||
.put(jsonBody.toRequestBody(jsonMediaType))
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build())
|
||||
}
|
||||
}
|
||||
@@ -37,4 +37,6 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
|
||||
val startUpNoticeUser = config.startUpNoticeUser
|
||||
val selfId = config.selfId
|
||||
val tianXingApiKey = config.tianXingApiKey
|
||||
val githubUser = config.githubUser
|
||||
val githubImageRepo = config.githubImageRepo
|
||||
}
|
||||
@@ -28,5 +28,7 @@
|
||||
"qqMusicApiUrl": "http://127.0.0.1:3200",
|
||||
"startUpNoticeUser": 114514,
|
||||
"selfId": 114514,
|
||||
"tianXingApiKey": "1145141919810"
|
||||
"tianXingApiKey": "1145141919810",
|
||||
"githubUser": "RTAkland",
|
||||
"githubImageRepo": "Static-Images"
|
||||
}
|
||||
Reference in New Issue
Block a user