mirror of
https://github.com/InsanusMokrassar/BooruGrabberTelegramBot.git
synced 2024-11-21 15:43:46 +00:00
first version of bot
This commit is contained in:
parent
4c4d4f6946
commit
cec7719ad7
@ -18,6 +18,7 @@ plugins {
|
|||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
maven { url "https://jitpack.io" }
|
maven { url "https://jitpack.io" }
|
||||||
|
mavenLocal()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -35,7 +36,7 @@ dependencies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
mainClassName = 'telegram_bot.AppKt'
|
mainClassName = 'AppKt'
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
kotlin = "1.7.10"
|
kotlin = "1.7.10"
|
||||||
tgbotapi = "3.2.0"
|
tgbotapi = "3.2.0"
|
||||||
microutils = "0.12.7"
|
microutils = "0.12.9"
|
||||||
imageboard = "2.5.2"
|
imageboard = "2.5.2"
|
||||||
krontab = "0.8.0"
|
krontab = "0.8.0"
|
||||||
kslog = "0.5.1"
|
kslog = "0.5.1"
|
||||||
|
@ -3,6 +3,7 @@ import dev.inmo.micro_utils.coroutines.*
|
|||||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||||
import dev.inmo.micro_utils.repos.add
|
import dev.inmo.micro_utils.repos.add
|
||||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import dev.inmo.micro_utils.repos.cache.cached
|
import dev.inmo.micro_utils.repos.cache.cached
|
||||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
|
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
|
||||||
@ -69,7 +70,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
{ this },
|
{ this },
|
||||||
{ ChatId(this) },
|
{ ChatId(this) },
|
||||||
{ this },
|
{ this },
|
||||||
).cached(FullKVCache(), scope = scope)
|
).cached(KVCache(128), scope = scope)
|
||||||
|
|
||||||
val chatsChangingMutex = Mutex()
|
val chatsChangingMutex = Mutex()
|
||||||
val chatsSendingJobs = mutableMapOf<ChatId, Job>()
|
val chatsSendingJobs = mutableMapOf<ChatId, Job>()
|
||||||
@ -79,52 +80,58 @@ suspend fun main(args: Array<String>) {
|
|||||||
// in this lambda you will be able to call methods without "bot." prefix
|
// in this lambda you will be able to call methods without "bot." prefix
|
||||||
val me = getMe()
|
val me = getMe()
|
||||||
|
|
||||||
|
suspend fun triggerSendForChat(chatId: ChatId, settings: ChatSettings) {
|
||||||
|
val result = let {
|
||||||
|
val result = mutableListOf<BoardImage>()
|
||||||
|
var i = 0
|
||||||
|
while (result.size < settings.count) {
|
||||||
|
val images = settings.makeRequest(i).takeIf { it.isNotEmpty() } ?: break
|
||||||
|
result.addAll(
|
||||||
|
images.filterNot {
|
||||||
|
chatsUrlsSeen.contains(chatId, it.url)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
val toDrop = (result.size - settings.count).takeIf { it > 0 } ?: return@let result
|
||||||
|
result.dropLast(toDrop)
|
||||||
|
}.takeIf { it.isNotEmpty() } ?: return
|
||||||
|
runCatchingSafely {
|
||||||
|
val urls = result.map { it.url.also(::println) }
|
||||||
|
chatsUrlsSeen.add(chatId, urls)
|
||||||
|
when {
|
||||||
|
urls.isEmpty() -> return@runCatchingSafely
|
||||||
|
urls.size == 1 -> sendPhoto(
|
||||||
|
chatId,
|
||||||
|
FileUrl(urls.first())
|
||||||
|
)
|
||||||
|
settings.gallery -> urls.chunked(mediaCountInMediaGroup.last + 1).forEach {
|
||||||
|
sendVisualMediaGroup(
|
||||||
|
chatId,
|
||||||
|
it.map {
|
||||||
|
TelegramMediaPhoto(FileUrl(it))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else -> urls.forEach {
|
||||||
|
sendPhoto(
|
||||||
|
chatId,
|
||||||
|
FileUrl(it)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.onFailure {
|
||||||
|
triggerSendForChat(chatId, settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun refreshChatJob(chatId: ChatId, settings: ChatSettings?) {
|
suspend fun refreshChatJob(chatId: ChatId, settings: ChatSettings?) {
|
||||||
val settings = settings ?: repo.get(chatId)
|
val settings = settings ?: repo.get(chatId)
|
||||||
chatsChangingMutex.withLock {
|
chatsChangingMutex.withLock {
|
||||||
chatsSendingJobs[chatId] ?.cancel()
|
chatsSendingJobs[chatId] ?.cancel()
|
||||||
settings ?.let {
|
settings ?.let {
|
||||||
chatsSendingJobs[chatId] = settings.scheduler.asFlow().subscribeSafelyWithoutExceptions(scope) {
|
chatsSendingJobs[chatId] = settings.scheduler.asFlow().subscribeSafelyWithoutExceptions(scope) {
|
||||||
val result = mutableListOf<BoardImage>()
|
triggerSendForChat(chatId, settings)
|
||||||
let {
|
|
||||||
var i = 0
|
|
||||||
while (result.size < settings.count) {
|
|
||||||
val images = settings.makeRequest(i).takeIf { it.isNotEmpty() } ?: break
|
|
||||||
result.addAll(
|
|
||||||
images.filterNot {
|
|
||||||
chatsUrlsSeen.contains(chatId, it.url)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
when {
|
|
||||||
result.isEmpty() -> return@subscribeSafelyWithoutExceptions
|
|
||||||
result.size == 1 -> sendPhoto(
|
|
||||||
chatId,
|
|
||||||
FileUrl(result.first().url)
|
|
||||||
).also {
|
|
||||||
result.forEach { chatsUrlsSeen.add(chatId, it.url) }
|
|
||||||
}
|
|
||||||
settings.gallery -> result.chunked(mediaCountInMediaGroup.last + 1).forEach {
|
|
||||||
sendVisualMediaGroup(
|
|
||||||
chatId,
|
|
||||||
it.map {
|
|
||||||
TelegramMediaPhoto(FileUrl(it.url))
|
|
||||||
}
|
|
||||||
).also { _ ->
|
|
||||||
it.forEach { chatsUrlsSeen.add(chatId, it.url) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> result.forEach {
|
|
||||||
sendPhoto(
|
|
||||||
chatId,
|
|
||||||
FileUrl(it.url)
|
|
||||||
).also { _ ->
|
|
||||||
chatsUrlsSeen.add(chatId, it.url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user