From 8a0a507e5baef4ca957861111720415043b5f607 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Wed, 4 Jun 2025 15:42:31 +0900 Subject: [PATCH] [feature] some logic fixed. --- .../chzzk_bot/chatbot/chzzk/ChzzkHandler.kt | 6 ++-- .../chzzk_bot/chatbot/chzzk/MessageHandler.kt | 8 ++--- .../chatbot/utils/accessTokenRefresh.kt | 2 +- .../mori/chzzk_bot/common/utils/ChzzkApis.kt | 35 +++++++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt index 09b8384..e6962ca 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt @@ -207,6 +207,7 @@ class UserHandler( var messageHandler: MessageHandler var client: ChzzkClient var listener: ChzzkUserSession + var chatChannelId: String? private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java) private var _isActive: Boolean @@ -225,14 +226,11 @@ class UserHandler( val tokens = Connector.client.refreshAccessToken(user.refreshToken!!) client = Connector.getClient(tokens.first, tokens.second) UserService.setRefreshToken(user, tokens.first, tokens.second) + chatChannelId = getChzzkChannelId(channel.channelId) client.loginAsync().join() - client.refreshTokenAsync().join() - listener = ChzzkSessionBuilder(client).buildUserSession() - listener.createAndConnectAsync().join() - messageHandler = MessageHandler(this@UserHandler) listener.on(SessionChatMessageEvent::class.java) { diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt index aa2e092..f107646 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt @@ -353,15 +353,15 @@ class MessageHandler( // Replace followPattern result = followPattern.replace(result) { _ -> try { - val followingDate = getFollowDate(channel.channelId, msg.senderChannelId) - .content?.streamingProperty?.following?.followDate + val followingDate = handler.chatChannelId?.let { getFollowDate(it, msg.senderChannelId) } + ?.content?.streamingProperty?.following?.followDate ?: LocalDateTime.now().minusDays(1).toString() - val period = followingDate?.let { + val period = followingDate.let { val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val pastDate = LocalDateTime.parse(it, formatter) val today = LocalDateTime.now() ChronoUnit.DAYS.between(pastDate, today) - } ?: 0 + } + 1 period.toString() } catch (e: Exception) { diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/utils/accessTokenRefresh.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/utils/accessTokenRefresh.kt index c8eb9ac..4355427 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/utils/accessTokenRefresh.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/utils/accessTokenRefresh.kt @@ -29,7 +29,7 @@ data class RefreshTokenResponse( ) fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair { - val url = "https://openapi.chzzk.naver.com/auth/v1/token" + val url = "https://chzzk.naver.com/auth/v1/token" val request = Request.Builder() .url(url) .header("Content-Type", "application/json") diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/utils/ChzzkApis.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/utils/ChzzkApis.kt index 04da4ab..d1449ca 100644 --- a/common/src/main/kotlin/space/mori/chzzk_bot/common/utils/ChzzkApis.kt +++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/utils/ChzzkApis.kt @@ -52,6 +52,20 @@ data class NicknameColor( val colorCode: String = "" ) +data class LiveStatus( + val liveTitle: String, + val status: String, + val concurrentUserCount: Int, + val accumulateCount: Int, + val paidPromotion: Boolean, + val adult: Boolean, + val krOnlyViewing: Boolean, + val openDate: String, + val closeDate: String?, + val clipActive: Boolean, + val chatChannelId: String +) + // OkHttpClient에 Interceptor 추가 val client = OkHttpClient.Builder() .addNetworkInterceptor { chain -> @@ -84,3 +98,24 @@ fun getFollowDate(chatID: String, userId: String) : IData { } } } + +fun getChzzkChannelId(channelId: String): String? { + val url = "https://api.chzzk.naver.com/polling/v3/channels/$channelId/live-status?includePlayerRecommendContent=false" + val request = Request.Builder() + .url(url) + .header("Content-Type", "application/json") + .get() + .build() + + client.newCall(request).execute().use { response -> + try { + if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}") + val body = response.body?.string() + val data = gson.fromJson(body, object: TypeToken>() {}) + + return data.content?.chatChannelId + } catch(e: Exception) { + throw e + } + } +} \ No newline at end of file