mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
commit
c9ad739d05
@ -230,40 +230,46 @@ class MessageHandler(
|
||||
return
|
||||
}
|
||||
|
||||
val video = getYoutubeVideo(url)
|
||||
if (video == null) {
|
||||
listener.sendChat("유튜브에서 찾을 수 없어요!")
|
||||
return
|
||||
}
|
||||
try {
|
||||
val video = getYoutubeVideo(url)
|
||||
if (video == null) {
|
||||
listener.sendChat("유튜브에서 찾을 수 없어요!")
|
||||
return
|
||||
}
|
||||
|
||||
if (songs.any { it.url == video.url }) {
|
||||
listener.sendChat("같은 노래가 이미 신청되어 있습니다.")
|
||||
return
|
||||
}
|
||||
if (songs.any { it.url == video.url }) {
|
||||
listener.sendChat("같은 노래가 이미 신청되어 있습니다.")
|
||||
return
|
||||
}
|
||||
|
||||
SongListService.saveSong(
|
||||
user,
|
||||
msg.userId,
|
||||
video.url,
|
||||
video.name,
|
||||
video.author,
|
||||
video.length,
|
||||
msg.profile?.nickname ?: ""
|
||||
)
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
dispatcher.post(SongEvent(
|
||||
user.token,
|
||||
SongType.ADD,
|
||||
SongListService.saveSong(
|
||||
user,
|
||||
msg.userId,
|
||||
msg.profile?.nickname ?: "",
|
||||
video.url,
|
||||
video.name,
|
||||
video.author,
|
||||
video.length,
|
||||
video.url
|
||||
))
|
||||
}
|
||||
msg.profile?.nickname ?: ""
|
||||
)
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
dispatcher.post(
|
||||
SongEvent(
|
||||
user.token,
|
||||
SongType.ADD,
|
||||
msg.userId,
|
||||
msg.profile?.nickname ?: "",
|
||||
video.name,
|
||||
video.author,
|
||||
video.length,
|
||||
video.url
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
listener.sendChat("노래가 추가되었습니다.")
|
||||
listener.sendChat("노래가 추가되었습니다.")
|
||||
} catch(e: Exception) {
|
||||
listener.sendChat("유튜브 영상 주소로 다시 신청해주세요!")
|
||||
}
|
||||
}
|
||||
|
||||
private fun songListCommand(msg: ChatMessage, user: User) {
|
||||
|
@ -3,6 +3,7 @@ package space.mori.chzzk_bot.common.utils
|
||||
import com.google.gson.JsonObject
|
||||
import io.github.cdimascio.dotenv.dotenv
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.IOException
|
||||
|
||||
@ -20,11 +21,38 @@ val dotenv = dotenv {
|
||||
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? {
|
||||
val matchResult = regex.find(url)
|
||||
OkHttpClient().newCall(request).execute().use { response ->
|
||||
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 {
|
||||
@ -38,8 +66,8 @@ fun parseDuration(duration: String): Int {
|
||||
return hourInSec + minutesInSec + totalSeconds
|
||||
}
|
||||
|
||||
fun getYoutubeVideo(url: String): YoutubeVideo? {
|
||||
val videoId = getYoutubeVideoId(url)
|
||||
fun getYoutubeVideo(query: String): YoutubeVideo? {
|
||||
val videoId = getYoutubeVideoId(query)
|
||||
|
||||
val api = HttpUrl.Builder()
|
||||
.scheme("https")
|
||||
|
1
inc.env
1
inc.env
@ -5,5 +5,6 @@ DB_USER=chzzk
|
||||
DB_PASS=chzzk
|
||||
RUN_AGENT=false
|
||||
YOUTUBE_API_KEY=
|
||||
RAPID_KEY=
|
||||
NID_AUT=
|
||||
NID_SES=
|
@ -79,21 +79,35 @@ fun Routing.wsSongListRoutes() {
|
||||
if(data.isStreamerOnly != null) SongConfigService.updateStreamerOnly(user, data.isStreamerOnly)
|
||||
|
||||
if(data.type == SongType.ADD.value && data.url != null) {
|
||||
val youtubeVideo = getYoutubeVideo(data.url)
|
||||
if(youtubeVideo != null) {
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
SongListService.saveSong(user, user.token, data.url, youtubeVideo.name, youtubeVideo.author, youtubeVideo.length, user.username)
|
||||
dispatcher.post(SongEvent(
|
||||
user.token,
|
||||
SongType.ADD,
|
||||
user.token,
|
||||
user.username,
|
||||
youtubeVideo.name,
|
||||
youtubeVideo.author,
|
||||
youtubeVideo.length,
|
||||
youtubeVideo.url
|
||||
))
|
||||
try {
|
||||
val youtubeVideo = getYoutubeVideo(data.url)
|
||||
if (youtubeVideo != null) {
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
SongListService.saveSong(
|
||||
user,
|
||||
user.token,
|
||||
data.url,
|
||||
youtubeVideo.name,
|
||||
youtubeVideo.author,
|
||||
youtubeVideo.length,
|
||||
user.username
|
||||
)
|
||||
dispatcher.post(
|
||||
SongEvent(
|
||||
user.token,
|
||||
SongType.ADD,
|
||||
user.token,
|
||||
user.username,
|
||||
youtubeVideo.name,
|
||||
youtubeVideo.author,
|
||||
youtubeVideo.length,
|
||||
youtubeVideo.url
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
logger.debug("SongType.ADD Error: {} / {}", session.token, e)
|
||||
}
|
||||
}
|
||||
else if(data.type == SongType.REMOVE.value && data.url != null) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user