Skip to content

Commit

Permalink
fix: try backup API before throwing
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
Samarium150 committed Mar 14, 2024
1 parent f2574ec commit 8e84823
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion 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.8.3"
version = "1.8.4"

repositories {
mavenCentral()
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/MiraiConsoleLoafersCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object MiraiConsoleLoafersCalendar : KotlinPlugin(
JvmPluginDescription(
id = "io.github.samarium150.mirai.plugin.mirai-console-loafers-calendar",
name = "Loafers' Calender",
version = "1.8.2",
version = "1.8.4",
) {
author("Samarium")
}
Expand All @@ -52,9 +52,9 @@ object MiraiConsoleLoafersCalendar : KotlinPlugin(
private val registry by lazy { IIORegistry.getDefaultInstance() }

private fun setupScheduler(cronExpression: String, timezone: String) {
CronUtil.schedule(cronExpression, Notification)
CronUtil.setMatchSecond(true)
CronUtil.getScheduler().timeZone = TimeZone.getTimeZone(timezone)
CronUtil.schedule(cronExpression, Notification)
}

override fun onEnable() {
Expand Down Expand Up @@ -94,6 +94,7 @@ object MiraiConsoleLoafersCalendar : KotlinPlugin(
GetLoafersCalendar.unregister()
Subscribe.unregister()
Unsubscribe.unregister()
Clean.unregister()

client.close()

Expand Down
60 changes: 47 additions & 13 deletions src/main/kotlin/util/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runInterruptible
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Contact.Companion.sendImage
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.text.ParseException
import java.text.SimpleDateFormat
Expand All @@ -51,36 +53,67 @@ internal val httpClient by lazy {
MiraiConsoleLoafersCalendar.client
}

internal fun getUTC8Date(): Date {
return Calendar.getInstance(TimeZone.getTimeZone("UTC+8")).time
internal val CTT = TimeZone.getTimeZone("CTT")

internal val calendar get() = Calendar.getInstance(CTT)

internal val SDF = SimpleDateFormat("yyyyMMdd").apply { timeZone = CTT }

internal fun getUTC8Date(): String {
return SDF.format(calendar.time)
}

internal fun isSunday(date: Date): Boolean {
return calendar.apply { time = date }.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
}

internal fun Date.isSameDay(other: Date): Boolean {
return calendar.apply {
time = this@isSameDay
}.get(Calendar.DAY_OF_YEAR) == calendar.apply {
time = other
}.get(Calendar.DAY_OF_YEAR)
}

@Throws(ParseException::class)
internal fun sanitizeDate(date: String?): String {
if (date == null) return SimpleDateFormat("yyyyMMdd").format(getUTC8Date())
SimpleDateFormat("yyyyMMdd").parse(date)
if (date == null) return getUTC8Date()
SDF.parse(date)
return date
}

internal suspend fun convertWebPToPNG(webpData: ByteArray): ByteArray {
return runInterruptible {
@Throws(CancellationException::class, IOException::class)
internal suspend fun convertToPNG(bytes: ByteArray): ByteArray {
return runInterruptible(Dispatchers.IO) {
val output = ByteArrayOutputStream()
ImageIO.write(ImageIO.read(webpData.inputStream()), "png", output)
ImageIO.write(ImageIO.read(bytes.inputStream()), "png", output)
return@runInterruptible output.toByteArray()
}
}

@Throws(ParseException::class, ServerResponseException::class, NotUpdatedYetException::class)
@Throws(
CancellationException::class,
IOException::class,
NotUpdatedYetException::class,
ParseException::class,
ServerResponseException::class
)
internal suspend fun downloadLoafersCalender(date: String? = null): InputStream {
val target = sanitizeDate(date)
val targetDate = SDF.parse(target)
val file = cacheFolder.resolve("$target.png")
if (file.exists()) return file.inputStream()
val response: HttpResponse = httpClient.get("https://api.j4u.ink/proxy/redirect/moyu/calendar/$target.png")
var response: HttpResponse = httpClient.get("https://api.j4u.ink/proxy/redirect/moyu/calendar/$target.png")
var body: ByteArray = response.body()
if (response.etag() == "\"6251bbbb-d2781\"")
throw NotUpdatedYetException("API is not updated yet")
if (response.headers["Content-Type"] == "image/webp")
body = convertWebPToPNG(body)
if (response.etag() == "dcfa2a3538f911d47550b49cbfbfb23f" && !isSunday(targetDate)) {
if (!targetDate.isSameDay(calendar.time))
throw NotUpdatedYetException("API is not updated yet")
response = httpClient.get("https://api.vvhan.com/api/moyu")
if (response.lastModified()?.isSameDay(targetDate) == false)
throw NotUpdatedYetException("API is not updated yet")
body = response.body()
}
body = convertToPNG(body)
if (PluginConfig.save)
file.writeBytes(body)
return body.inputStream()
Expand All @@ -103,6 +136,7 @@ internal fun Bot.sendUpdate() = MiraiConsoleLoafersCalendar.launch {
}
}

@Throws(ParseException::class)
internal fun cleanCalendarCache(date: String?) = runCatching {
if (date == null) {
cacheFolder.listFiles()?.forEach {
Expand Down

0 comments on commit 8e84823

Please sign in to comment.