Skip to content

Commit

Permalink
feat: redesign Get command
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarium150 committed Apr 11, 2022
1 parent 10e6d6c commit 97fe8c5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/MiraiConsoleLoafersCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 20 additions & 20 deletions src/main/kotlin/command/GetLoafersCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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()
}
}
}
23 changes: 18 additions & 5 deletions src/main/kotlin/util/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 97fe8c5

Please sign in to comment.