0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00

Add badges endpoint

This commit is contained in:
dragongoose 2023-05-31 19:39:07 -04:00
parent ec2a49b5bb
commit 4f277a2743
No known key found for this signature in database
GPG key ID: 50DB99B921579009
5 changed files with 130 additions and 1 deletions

View file

@ -66,3 +66,11 @@ type CategoryData struct {
Streams []CategoryMinifiedStream `json:"streams"`
Cover string `json:"cover"`
}
type Badge struct {
Id string `json:"id"`
Title string `json:"title"`
SetId string `json:"setId"`
Version string `json:"version"`
Images map[string]string `json:"images"`
}

View file

@ -1,6 +1,7 @@
package twitch
import (
"encoding/base64"
"errors"
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/structs"
@ -79,7 +80,7 @@ func ParseCategory(data gjson.Result) (structs.CategoryPreview, error) {
func ParseMinifiedStream(data gjson.Result) (structs.CategoryMinifiedStream, error) {
var tags []string
tagArea := data.Get("node.freeformTags").Array()
tagArea := data.Get("node.freeformTacgs").Array()
for _, tag := range tagArea {
tags = append(tags, tag.Get("name").String())
}
@ -108,3 +109,29 @@ func ParseMinifiedStream(data gjson.Result) (structs.CategoryMinifiedStream, err
return parsedStream, nil
}
func ParseBadges(data gjson.Result) ([]structs.Badge, error) {
var formattedBadges = []structs.Badge{}
for _, badge := range data.Array() {
id := badge.Get("id").String()
decodedId, err := base64.StdEncoding.DecodeString(id)
if err != nil {
return []structs.Badge{}, nil
}
formattedBadges = append(formattedBadges, structs.Badge{
Id: string(decodedId),
SetId: badge.Get("setID").String(),
Title: badge.Get("title").String(),
Version: badge.Get("version").String(),
Images: map[string]string{
"image1x": extractor.ProxyUrl(badge.Get("image1x").String()),
"image2x": extractor.ProxyUrl(badge.Get("image2x").String()),
"image4x": extractor.ProxyUrl(badge.Get("image4x").String()),
},
})
}
return formattedBadges, nil
}

View file

@ -295,3 +295,62 @@ func GetDiscoveryItem(name string, streamLimit int, cursor string) (structs.Cate
return parsedCategory, err
}
func GetTwitchBadges() ([]structs.Badge, error) {
payload := []TwitchPayload{
{
"operationName": "ChannelPointsPredictionBadges",
"variables": map[string]interface{}{},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"sha256Hash": "36995b30b22c31d1cd0aa329987ac9b5368bb7e6e1ab1df42808bdaa80a6dbf9",
"version": 1,
},
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.Badge{}, err
}
rawBadges := gjson.Get(string(body), "0.data.badges")
formattedBadges, err := ParseBadges(rawBadges)
if err != nil {
return []structs.Badge{}, err
}
return formattedBadges, nil
}
func GetStreamerBadges(streamerName string) ([]structs.Badge, error) {
payload := []TwitchPayload{
{
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"sha256Hash": "86f43113c04606e6476e39dcd432dee47c994d77a83e54b732e11d4935f0cd08",
"version": 1,
},
},
"operationName": "ChatList_Badges",
"variables": map[string]interface{}{
"channelLogin": streamerName,
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.Badge{}, err
}
rawBadges := gjson.Get(string(body), "0.data.user.broadcastBadges")
formattedBadges, err := ParseBadges(rawBadges)
if err != nil {
return []structs.Badge{}, err
}
return formattedBadges, nil
}

View file

@ -0,0 +1,33 @@
package badges
import (
"github.com/gin-gonic/gin"
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/twitch"
)
func Routes(route *gin.Engine) {
auth := route.Group("/api/badges")
auth.GET("/", func(context *gin.Context) {
query := context.Query("streamerName")
if query != "" {
data, err := twitch.GetStreamerBadges(query)
if err != nil {
context.Error(err)
return
}
context.JSON(200, extractor.FormatMessage(data, true))
} else {
data, err := twitch.GetTwitchBadges()
if err != nil {
context.Error(err)
return
}
context.JSON(200, extractor.FormatMessage(data, true))
}
})
}

View file

@ -1,6 +1,7 @@
package routes
import (
"safetwitch-backend/routes/api/badges"
"safetwitch-backend/routes/api/discover"
"safetwitch-backend/routes/api/users"
"safetwitch-backend/routes/proxy"
@ -12,6 +13,7 @@ import (
func SetRoutes(router *gin.Engine) {
users.Routes(router)
discover.Routes(router)
badges.Routes(router)
proxy.Routes(router)
root.Routes(router)