mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
some fix
- add searchYoutube function. - add try-catch blocks.
This commit is contained in:
parent
816c82e57b
commit
d329b8bdb3
@ -230,6 +230,7 @@ class MessageHandler(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
val video = getYoutubeVideo(url)
|
val video = getYoutubeVideo(url)
|
||||||
if (video == null) {
|
if (video == null) {
|
||||||
listener.sendChat("유튜브에서 찾을 수 없어요!")
|
listener.sendChat("유튜브에서 찾을 수 없어요!")
|
||||||
@ -251,7 +252,8 @@ class MessageHandler(
|
|||||||
msg.profile?.nickname ?: ""
|
msg.profile?.nickname ?: ""
|
||||||
)
|
)
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
dispatcher.post(SongEvent(
|
dispatcher.post(
|
||||||
|
SongEvent(
|
||||||
user.token,
|
user.token,
|
||||||
SongType.ADD,
|
SongType.ADD,
|
||||||
msg.userId,
|
msg.userId,
|
||||||
@ -260,10 +262,14 @@ class MessageHandler(
|
|||||||
video.author,
|
video.author,
|
||||||
video.length,
|
video.length,
|
||||||
video.url
|
video.url
|
||||||
))
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.sendChat("노래가 추가되었습니다.")
|
listener.sendChat("노래가 추가되었습니다.")
|
||||||
|
} catch(e: Exception) {
|
||||||
|
listener.sendChat("유튜브 영상 주소로 다시 신청해주세요!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun songListCommand(msg: ChatMessage, user: User) {
|
private fun songListCommand(msg: ChatMessage, user: User) {
|
||||||
|
@ -3,6 +3,7 @@ package space.mori.chzzk_bot.common.utils
|
|||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import io.github.cdimascio.dotenv.dotenv
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
import okhttp3.HttpUrl
|
import okhttp3.HttpUrl
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@ -20,11 +21,38 @@ val dotenv = dotenv {
|
|||||||
ignoreIfMissing = true
|
ignoreIfMissing = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun searchYoutube(query: String): String? {
|
||||||
|
val url = HttpUrl.Builder()
|
||||||
|
.scheme("https")
|
||||||
|
.host("youtube-search-results.p.rapidapi.com")
|
||||||
|
.addQueryParameter("q", query)
|
||||||
|
.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.addHeader("x-rapidapi-host", "youtube-search-results.p.rapidapi.com")
|
||||||
|
.addHeader("x-rapidapi-key", dotenv["RAPID_KEY"] ?: "")
|
||||||
|
.build()
|
||||||
|
|
||||||
fun getYoutubeVideoId(url: String): String? {
|
OkHttpClient().newCall(request).execute().use { response ->
|
||||||
val matchResult = regex.find(url)
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
|
|
||||||
return matchResult?.groups?.get(1)?.value
|
val responseBody = response.body?.string()
|
||||||
|
val json = gson.fromJson(responseBody, JsonObject::class.java)
|
||||||
|
val items = json.getAsJsonArray("items")
|
||||||
|
|
||||||
|
if (items.size() == 0) return null
|
||||||
|
|
||||||
|
val firstItem = items[0].asJsonObject
|
||||||
|
val videoUrl = firstItem.get("url").asString
|
||||||
|
|
||||||
|
return videoUrl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getYoutubeVideoId(query: String): String? {
|
||||||
|
val matchResult = regex.find(query)
|
||||||
|
|
||||||
|
return matchResult?.groups?.get(1)?.value ?: searchYoutube(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseDuration(duration: String): Int {
|
fun parseDuration(duration: String): Int {
|
||||||
@ -38,8 +66,8 @@ fun parseDuration(duration: String): Int {
|
|||||||
return hourInSec + minutesInSec + totalSeconds
|
return hourInSec + minutesInSec + totalSeconds
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getYoutubeVideo(url: String): YoutubeVideo? {
|
fun getYoutubeVideo(query: String): YoutubeVideo? {
|
||||||
val videoId = getYoutubeVideoId(url)
|
val videoId = getYoutubeVideoId(query)
|
||||||
|
|
||||||
val api = HttpUrl.Builder()
|
val api = HttpUrl.Builder()
|
||||||
.scheme("https")
|
.scheme("https")
|
||||||
|
1
inc.env
1
inc.env
@ -5,5 +5,6 @@ DB_USER=chzzk
|
|||||||
DB_PASS=chzzk
|
DB_PASS=chzzk
|
||||||
RUN_AGENT=false
|
RUN_AGENT=false
|
||||||
YOUTUBE_API_KEY=
|
YOUTUBE_API_KEY=
|
||||||
|
RAPID_KEY=
|
||||||
NID_AUT=
|
NID_AUT=
|
||||||
NID_SES=
|
NID_SES=
|
@ -79,11 +79,21 @@ fun Routing.wsSongListRoutes() {
|
|||||||
if(data.isStreamerOnly != null) SongConfigService.updateStreamerOnly(user, data.isStreamerOnly)
|
if(data.isStreamerOnly != null) SongConfigService.updateStreamerOnly(user, data.isStreamerOnly)
|
||||||
|
|
||||||
if(data.type == SongType.ADD.value && data.url != null) {
|
if(data.type == SongType.ADD.value && data.url != null) {
|
||||||
|
try {
|
||||||
val youtubeVideo = getYoutubeVideo(data.url)
|
val youtubeVideo = getYoutubeVideo(data.url)
|
||||||
if(youtubeVideo != null) {
|
if (youtubeVideo != null) {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
SongListService.saveSong(user, user.token, data.url, youtubeVideo.name, youtubeVideo.author, youtubeVideo.length, user.username)
|
SongListService.saveSong(
|
||||||
dispatcher.post(SongEvent(
|
user,
|
||||||
|
user.token,
|
||||||
|
data.url,
|
||||||
|
youtubeVideo.name,
|
||||||
|
youtubeVideo.author,
|
||||||
|
youtubeVideo.length,
|
||||||
|
user.username
|
||||||
|
)
|
||||||
|
dispatcher.post(
|
||||||
|
SongEvent(
|
||||||
user.token,
|
user.token,
|
||||||
SongType.ADD,
|
SongType.ADD,
|
||||||
user.token,
|
user.token,
|
||||||
@ -92,9 +102,13 @@ fun Routing.wsSongListRoutes() {
|
|||||||
youtubeVideo.author,
|
youtubeVideo.author,
|
||||||
youtubeVideo.length,
|
youtubeVideo.length,
|
||||||
youtubeVideo.url
|
youtubeVideo.url
|
||||||
))
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch(e: Exception) {
|
||||||
|
logger.debug("SongType.ADD Error: {} / {}", session.token, e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(data.type == SongType.REMOVE.value && data.url != null) {
|
else if(data.type == SongType.REMOVE.value && data.url != null) {
|
||||||
dispatcher.post(SongEvent(
|
dispatcher.post(SongEvent(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user