mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
add getUserInfo function
This commit is contained in:
parent
80dbf10e7e
commit
94b87dc87e
@ -146,3 +146,22 @@ fun getStreamInfo(userId: String) : IData<IStreamInfo?> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getUserInfo(userId: String): IData<Channel?> {
|
||||||
|
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<IData<Channel?>>() {})
|
||||||
|
|
||||||
|
return channel
|
||||||
|
} catch(e: Exception) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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.SongConfigService
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
||||||
|
import space.mori.chzzk_bot.common.utils.getUserInfo
|
||||||
import space.mori.chzzk_bot.webserver.UserSession
|
import space.mori.chzzk_bot.webserver.UserSession
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ -133,20 +134,20 @@ fun Routing.apiRoutes() {
|
|||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
|
|
||||||
val status = getStreamInfo(matchedChzzkId)
|
val status = getUserInfo(matchedChzzkId)
|
||||||
if (status.content == null) {
|
if (status.content == null) {
|
||||||
call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest)
|
call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest)
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
UserService.updateUser(
|
UserService.updateUser(
|
||||||
user,
|
user,
|
||||||
status.content!!.channel.channelId,
|
status.content!!.channelId,
|
||||||
status.content!!.channel.channelName
|
status.content!!.channelName
|
||||||
)
|
)
|
||||||
call.respondText("Done!", status = HttpStatusCode.OK)
|
call.respondText("Done!", status = HttpStatusCode.OK)
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
dispatcher.post(UserRegisterEvent(status.content!!.channel.channelId))
|
dispatcher.post(UserRegisterEvent(status.content!!.channelId))
|
||||||
}
|
}
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user