0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 05:02:58 -05:00
safetwitch-backend/extractor/twitch/Chat.go
2023-07-30 19:12:11 -04:00

71 lines
1.7 KiB
Go

package twitch
import (
"encoding/base64"
"safetwitch-backend/extractor/structs"
"strings"
"github.com/tidwall/gjson"
)
func GetChatHistory(streamerName string) ([]structs.StreamMessage, error) {
id, err := GetStreamerId(streamerName)
if err != nil {
return []structs.StreamMessage{}, err
}
payload := []TwitchPayload{
{
"operationName": "MessageBufferChatHistory",
"variables": map[string]interface{}{
"channelLogin": streamerName,
"channelID": id,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "432ef3ec504a750d797297630052ec7c775f571f6634fdbda255af9ad84325ae",
},
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.StreamMessage{}, err
}
messages := gjson.Get(string(body), "0.data.channel.recentChatMessages").Array()
parsedMessages := []structs.StreamMessage{}
for _, message := range messages {
badges := []structs.StreamMessageBadge{}
for _, badge := range message.Get("senderBadges").Array() {
id := badge.Get("id").String()
decodedId, err := base64.StdEncoding.DecodeString(id)
if err != nil {
return []structs.StreamMessage{}, err
}
splitId := strings.Split(string(decodedId), ";")
parsedBadge := structs.StreamMessageBadge{
Version: badge.Get("version").String(),
SetID: splitId[0],
}
badges = append(badges, parsedBadge)
}
parsedMessage := structs.StreamMessage{
Message: message.Get("content.text").String(),
Username: message.Get("sender.displayName").String(),
Color: message.Get("sender.chatColor").String(),
Badges: badges,
}
parsedMessages = append(parsedMessages, parsedMessage)
}
return parsedMessages, nil
}