From 0bf31df81eeab2f54623ec392fc2a95d0acdac73 Mon Sep 17 00:00:00 2001 From: arzumify Date: Mon, 24 Mar 2025 19:35:37 +0000 Subject: [PATCH] Added getChatID --- main.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 43be7c6..ec7442d 100644 --- a/main.go +++ b/main.go @@ -96,13 +96,13 @@ type TextMessageDetails struct { MessageText string `json:"messageText"` } -func scrape(liveChatID string, key string) { +func scrape(liveChatID string) { for { if streaming == true { // Check for chat messages println("Scary, we are expending a Google API credit (on stream)!") - response, err := http.Get("https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId=" + liveChatID + "&part=snippet,authorDetails&key=" + key) + response, err := http.Get("https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId=" + liveChatID + "&part=snippet,authorDetails&key=" + configFile.ApiKey) if err != nil { println("Error getting chat messages: " + err.Error() + ", trying again in 20 seconds") time.Sleep(time.Second * 20) @@ -183,6 +183,23 @@ func getUserSecondDifference(channelID string) (int64, bool) { return int64(time.Now().Sub(earliestSentMessage).Seconds()), true } +func getLiveChatID(videoID string) string { + response, err := http.Get("https://www.googleapis.com/youtube/v3/videos?part=liveStreamingDetails&id=" + videoID + "&key=" + configFile.ApiKey) + if err != nil { + panic("Error getting live chat ID: " + err.Error()) + } + + var responseJSON map[string]interface{} + err = json.NewDecoder(response.Body).Decode(&responseJSON) + if err != nil { + panic("Error decoding JSON: " + err.Error()) + } + + liveChatID := responseJSON["items"].([]interface{})[0].(map[string]interface{})["liveStreamingDetails"].(map[string]interface{})["activeLiveChatId"].(string) + + return liveChatID +} + type PendingSession struct { IP string EventCallback func() @@ -685,16 +702,16 @@ func main() { // Set the live status streaming = true streamingSince = time.Now() - liveChatID, ok := data["liveChatID"].(string) + videoID, ok := data["videoID"].(string) if !ok { c.JSON(400, gin.H{"error": "Invalid JSON"}) return } // Start the chat message checker - println("Starting chat message checker: ", liveChatID) + println("Starting chat message checker: ", videoID) - go scrape(liveChatID, configFile.ApiKey) + go scrape(getLiveChatID(videoID)) // Return 200 c.JSON(200, gin.H{"message": "Stream started"}) @@ -801,13 +818,13 @@ func main() { case "2": if !streaming { streaming = true - var liveChatID string - print("Enter live chat ID: ") - _, err := fmt.Scanln(&liveChatID) + var videoID string + print("Enter video ID: ") + _, err := fmt.Scanln(&videoID) if err != nil { println("Error reading input: " + err.Error()) } - go scrape(liveChatID, configFile.ApiKey) + go scrape(getLiveChatID(videoID)) println("OK: Stream started") } else { println("ERR: Stream already started")