diff --git a/build.gradle.kts b/build.gradle.kts index 3047bd6..56f39da 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { } group = "io.github.samarium150" -version = "1.0.1" +version = "1.1.0" repositories { maven("https://maven.aliyun.com/repository/public") @@ -31,7 +31,6 @@ dependencies { implementation("it.justwrote:kjob-inmem:0.2.0") { exclude(group = "org.jetbrains.kotlin") exclude(group = "org.jetbrains.kotlinx") - exclude(group = "org.slf4j") } } diff --git a/src/main/kotlin/MiraiConsoleLoafersCalendar.kt b/src/main/kotlin/MiraiConsoleLoafersCalendar.kt index 5ad6595..4addd0c 100644 --- a/src/main/kotlin/MiraiConsoleLoafersCalendar.kt +++ b/src/main/kotlin/MiraiConsoleLoafersCalendar.kt @@ -39,7 +39,7 @@ object MiraiConsoleLoafersCalendar : KotlinPlugin( JvmPluginDescription( id = "io.github.samarium150.mirai.plugin.mirai-console-loafers-calender", name = "Loafers' Calender", - version = "1.0.1", + version = "1.1.0", ) { author("Samarium") } diff --git a/src/main/kotlin/command/GetLoafersCalendar.kt b/src/main/kotlin/command/GetLoafersCalendar.kt index 5255df3..f1c15c7 100644 --- a/src/main/kotlin/command/GetLoafersCalendar.kt +++ b/src/main/kotlin/command/GetLoafersCalendar.kt @@ -19,8 +19,9 @@ package io.github.samarium150.mirai.plugin.loafers_calendar.command import io.github.samarium150.mirai.plugin.loafers_calendar.MiraiConsoleLoafersCalendar import io.github.samarium150.mirai.plugin.loafers_calendar.config.CommandConfig import io.github.samarium150.mirai.plugin.loafers_calendar.config.PluginConfig -import io.github.samarium150.mirai.plugin.loafers_calendar.util.cacheFolder import io.github.samarium150.mirai.plugin.loafers_calendar.util.downloadLoafersCalender +import io.github.samarium150.mirai.plugin.loafers_calendar.util.logger +import io.ktor.client.features.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import net.mamoe.mirai.console.command.CommandSender @@ -29,8 +30,8 @@ import net.mamoe.mirai.console.command.SimpleCommand import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.contact.Contact.Companion.sendImage +import java.text.ParseException -@Suppress("unused") object GetLoafersCalendar : SimpleCommand( MiraiConsoleLoafersCalendar, primaryName = "loafers-calendar", @@ -42,26 +43,25 @@ object GetLoafersCalendar : SimpleCommand( @ConsoleExperimentalApi override val prefixOptional = true + @Suppress("unused") @Handler - suspend fun CommandSender.handle() { - val inputStream = downloadLoafersCalender() - if (this is CommandSenderOnMessage<*>) { - fromEvent.subject.sendImage(inputStream) - withContext(Dispatchers.IO) { - inputStream.close() + suspend fun CommandSender.handle(date: String? = null) { + val inputStream = runCatching { + downloadLoafersCalender(date) + }.getOrElse { + when (it) { + is ParseException -> sendMessage("日期格式错误,请使用 yyyyMMdd 格式") + is ServerResponseException -> sendMessage("获取日历图片失败") + else -> logger.error(it) } - } else if (PluginConfig.save) + return@handle + } + if (this is CommandSenderOnMessage<*>) + fromEvent.subject.sendImage(inputStream) + else if (PluginConfig.save) sendMessage("图片已下载") - } - - @Handler - suspend fun CommandSenderOnMessage<*>.handle(date: String) { - if (PluginConfig.save) { - if (date.matches(Regex("^\\d{4}-\\d{2}-\\d{2}$"))) { - val file = cacheFolder.resolve("${date}.png") - if (file.exists()) fromEvent.subject.sendImage(file) - else sendMessage("没有找${date}的日历图片") - } else sendMessage("日期格式错误,请使用 yyyy-MM-dd 格式") - } else sendMessage("仅支持获取保存过的日历图片,请先设置PluginConfig.save为true") + withContext(Dispatchers.IO) { + inputStream.close() + } } } diff --git a/src/main/kotlin/util/General.kt b/src/main/kotlin/util/General.kt index 4337acb..7485073 100644 --- a/src/main/kotlin/util/General.kt +++ b/src/main/kotlin/util/General.kt @@ -20,15 +20,21 @@ import io.github.samarium150.mirai.plugin.loafers_calendar.MiraiConsoleLoafersCa import io.github.samarium150.mirai.plugin.loafers_calendar.config.PluginConfig import io.github.samarium150.mirai.plugin.loafers_calendar.data.PluginData import io.ktor.client.call.* +import io.ktor.client.features.* import io.ktor.client.request.* import io.ktor.client.statement.* import it.justwrote.kjob.KronJob import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.Contact.Companion.sendImage import java.io.InputStream +import java.text.ParseException import java.text.SimpleDateFormat import java.util.* +internal val logger by lazy { + MiraiConsoleLoafersCalendar.logger +} + internal val cacheFolder by lazy { val folder = MiraiConsoleLoafersCalendar.dataFolder.resolve("cache") if (!folder.exists()) folder.mkdirs() @@ -43,12 +49,19 @@ internal fun getUTC8Date(): Date { return Calendar.getInstance(TimeZone.getTimeZone("UTC+8")).time } -internal suspend fun downloadLoafersCalender(): InputStream { - val file = cacheFolder.resolve( - "${SimpleDateFormat("yyyy-MM-dd").format(getUTC8Date())}.png" - ) +@Throws(ParseException::class) +internal fun sanitizeDate(date: String?): String { + if (date == null) return SimpleDateFormat("yyyyMMdd").format(getUTC8Date()) + SimpleDateFormat("yyyyMMdd").parse(date) + return date +} + +@Throws(ParseException::class, ServerResponseException::class) +internal suspend fun downloadLoafersCalender(date: String? = null): InputStream { + val target = sanitizeDate(date) + val file = cacheFolder.resolve("${target}.png") if (file.exists()) return file.inputStream() - val response: HttpResponse = httpClient.get("https://api.vvhan.com/api/moyu") + val response: HttpResponse = httpClient.get("https://api.j4u.ink/proxy/redirect/moyu/calendar/${target}.png") val body: ByteArray = response.receive() if (PluginConfig.save) file.writeBytes(body)