mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 05:02:58 -05:00
Add chat history fetching
This commit is contained in:
parent
af3a348d99
commit
a991899c7f
4 changed files with 107 additions and 0 deletions
|
@ -132,3 +132,15 @@ type VodComment struct {
|
||||||
Cursor string `json:"cursor"`
|
Cursor string `json:"cursor"`
|
||||||
Badges []VodCommentBadge `json:"badges"`
|
Badges []VodCommentBadge `json:"badges"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StreamMessageBadge struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
SetID string `json:"setId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StreamMessage struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Color string `json:"color"`
|
||||||
|
Badges []StreamMessageBadge `json:"badges"`
|
||||||
|
}
|
||||||
|
|
71
extractor/twitch/Chat.go
Normal file
71
extractor/twitch/Chat.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
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
|
||||||
|
}
|
22
routes/api/chat/chat.go
Normal file
22
routes/api/chat/chat.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"safetwitch-backend/extractor"
|
||||||
|
"safetwitch-backend/extractor/twitch"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Routes(route *gin.Engine) {
|
||||||
|
auth := route.Group("/api/chat")
|
||||||
|
|
||||||
|
auth.GET(":streamerName/history", func(context *gin.Context) {
|
||||||
|
data, err := twitch.GetChatHistory(context.Param("streamerName"))
|
||||||
|
if err != nil {
|
||||||
|
context.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
context.JSON(200, extractor.FormatMessage(data, true))
|
||||||
|
})
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"safetwitch-backend/routes/api/badges"
|
"safetwitch-backend/routes/api/badges"
|
||||||
|
"safetwitch-backend/routes/api/chat"
|
||||||
"safetwitch-backend/routes/api/discover"
|
"safetwitch-backend/routes/api/discover"
|
||||||
"safetwitch-backend/routes/api/search"
|
"safetwitch-backend/routes/api/search"
|
||||||
"safetwitch-backend/routes/api/users"
|
"safetwitch-backend/routes/api/users"
|
||||||
|
@ -18,6 +19,7 @@ func SetRoutes(router *gin.Engine) {
|
||||||
badges.Routes(router)
|
badges.Routes(router)
|
||||||
search.Routes(router)
|
search.Routes(router)
|
||||||
vods.Routes(router)
|
vods.Routes(router)
|
||||||
|
chat.Routes(router)
|
||||||
|
|
||||||
proxy.Routes(router)
|
proxy.Routes(router)
|
||||||
root.Routes(router)
|
root.Routes(router)
|
||||||
|
|
Loading…
Reference in a new issue