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 3a9f366..42c9d61 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 @@ -146,3 +146,22 @@ fun getStreamInfo(userId: String) : IData { } } } + +fun getUserInfo(userId: String): IData { + val url = "https://api.chzzk.naver.com/service/v1/channels/${userId}" + val request = Request.Builder() + .url(url) + .build() + + client.newCall(request).execute().use { response -> + try { + if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}") + val body = response.body?.string() + val channel = gson.fromJson(body, object: TypeToken>() {}) + + return channel + } catch(e: Exception) { + throw e + } + } +} diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt index e3abf4d..a98729f 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt @@ -16,6 +16,7 @@ import space.mori.chzzk_bot.common.events.UserRegisterEvent import space.mori.chzzk_bot.common.services.SongConfigService import space.mori.chzzk_bot.common.services.UserService import space.mori.chzzk_bot.common.utils.getStreamInfo +import space.mori.chzzk_bot.common.utils.getUserInfo import space.mori.chzzk_bot.webserver.UserSession @Serializable @@ -133,20 +134,20 @@ fun Routing.apiRoutes() { return@post } - val status = getStreamInfo(matchedChzzkId) + val status = getUserInfo(matchedChzzkId) if (status.content == null) { call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest) return@post } UserService.updateUser( user, - status.content!!.channel.channelId, - status.content!!.channel.channelName + status.content!!.channelId, + status.content!!.channelName ) call.respondText("Done!", status = HttpStatusCode.OK) CoroutineScope(Dispatchers.Default).launch { - dispatcher.post(UserRegisterEvent(status.content!!.channel.channelId)) + dispatcher.post(UserRegisterEvent(status.content!!.channelId)) } return@post }