BooruGrabberTelegramBot/src/main/kotlin/ChatSettings.kt

66 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-09-07 09:01:03 +00:00
import dev.inmo.krontab.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.kodehawa.lib.imageboards.DefaultImageBoards
import net.kodehawa.lib.imageboards.ImageBoard
import net.kodehawa.lib.imageboards.boards.DefaultBoards
import net.kodehawa.lib.imageboards.entities.BoardImage
2022-09-09 10:42:59 +00:00
import net.kodehawa.lib.imageboards.entities.Rating
2022-09-07 09:01:03 +00:00
@Serializable
data class ChatSettings(
val query: String,
2022-10-07 16:15:06 +00:00
val krontabTemplate: KrontabTemplate? = null,
2022-09-07 09:01:03 +00:00
@Serializable(BoardSerializer::class)
2022-10-07 16:15:06 +00:00
private val boardBase: DefaultBoards = DefaultBoards.SAFEBOORU,
2022-09-07 09:01:03 +00:00
val count: Int = 1,
2022-09-09 10:42:59 +00:00
val gallery: Boolean = false,
val rating: Rating? = null,
val attachUrls: Boolean = false
2022-09-07 09:01:03 +00:00
) {
val scheduler by lazy {
2022-09-09 10:28:14 +00:00
krontabTemplate ?.toSchedule()
2022-09-07 09:01:03 +00:00
}
val board: ImageBoard<*>
get() = when (boardBase) {
DefaultBoards.R34 -> DefaultImageBoards.RULE34
DefaultBoards.E621 -> DefaultImageBoards.E621
DefaultBoards.KONACHAN -> DefaultImageBoards.KONACHAN
DefaultBoards.YANDERE -> DefaultImageBoards.YANDERE
DefaultBoards.DANBOORU -> DefaultImageBoards.DANBOORU
DefaultBoards.SAFEBOORU -> DefaultImageBoards.SAFEBOORU
DefaultBoards.GELBOORU -> DefaultImageBoards.GELBOORU
DefaultBoards.E926 -> DefaultImageBoards.E926
}
2022-09-07 11:34:07 +00:00
suspend fun makeRequest(page: Int): List<BoardImage> {
2022-09-07 09:01:03 +00:00
return withContext(Dispatchers.IO) {
2023-04-16 07:28:40 +00:00
board.search(page, count, query, rating).blocking() ?: emptyList()
2022-09-07 09:01:03 +00:00
}
}
@Serializer(DefaultBoards::class)
object BoardSerializer : KSerializer<DefaultBoards> {
override val descriptor: SerialDescriptor = String.serializer().descriptor
val types = DefaultBoards.values().associateBy { it.name.lowercase() }
override fun deserialize(decoder: Decoder): DefaultBoards {
val type = decoder.decodeString()
return types.getValue(type)
}
override fun serialize(encoder: Encoder, value: DefaultBoards) {
encoder.encodeString(value.name.lowercase())
}
}
2022-10-07 16:15:06 +00:00
companion object {
val DEFAULT = ChatSettings("", null, DefaultBoards.SAFEBOORU)
}
2022-09-07 09:01:03 +00:00
}