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

Add streamer banner

This commit is contained in:
dragongoose 2023-07-03 15:28:20 -04:00
parent 4a333eed57
commit 12116d7c9b
No known key found for this signature in database
GPG key ID: 50DB99B921579009
3 changed files with 36 additions and 4 deletions

View file

@ -23,6 +23,7 @@ type Streamer struct {
Login string `json:"login"`
About string `json:"about"`
Pfp string `json:"pfp"`
Banner *string `json:"banner"`
Followers int `json:"followers"`
Socials []Social `json:"socials"`
IsLive bool `json:"isLive"`

View file

@ -3,7 +3,6 @@ package twitch
import (
"encoding/base64"
"errors"
"fmt"
"os"
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/structs"
@ -13,7 +12,7 @@ import (
"github.com/tidwall/gjson"
)
func ParseStreamer(streamerData gjson.Result, isLive bool, socials []structs.Social, stream *structs.Stream, login string) (structs.Streamer, error) {
func ParseStreamer(streamerData gjson.Result, isLive bool, socials []structs.Social, stream *structs.Stream, login string, streamerBanner string) (structs.Streamer, error) {
// Store streamerColorHex in memory to use as pointer
var streamerColorHex *string
rawStreamerColorHex := streamerData.Get("user.primaryColorHex").String()
@ -23,13 +22,20 @@ func ParseStreamer(streamerData gjson.Result, isLive bool, socials []structs.Soc
streamerColorHex = &rawStreamerColorHex
}
fmt.Println(streamerData.String())
var bannerUrl *string = &streamerBanner
if *bannerUrl == "" {
bannerUrl = nil
} else {
proxied := extractor.ProxyUrl(*bannerUrl)
bannerUrl = &proxied
}
parsedStreamer := structs.Streamer{
Username: streamerData.Get("user.displayName").String(),
Login: login,
About: streamerData.Get("user.description").String(),
Pfp: extractor.ProxyUrl(streamerData.Get("user.profileImageURL").String()),
Banner: bannerUrl,
Followers: int(streamerData.Get("user.followers.totalCount").Int()),
Socials: socials,
IsLive: isLive,

View file

@ -71,6 +71,7 @@ type TwitchPayload = map[string]interface{}
func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
payload := []TwitchPayload{
// Streamer data
{
"operationName": "ChannelRoot_AboutPanel",
"variables": map[string]interface{}{
@ -84,6 +85,8 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
},
// Stream metadata
{
"operationName": "StreamMetadata",
"variables": map[string]interface{}{
@ -96,6 +99,8 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
},
// Stream tags
{
"operationName": "StreamTagsTrackingChannel",
"variables": map[string]interface{}{
@ -108,6 +113,8 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
},
// Stream preview image
{
"operationName": "VideoPreviewOverlay",
"variables": map[string]interface{}{
@ -120,6 +127,8 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
},
// Current views
{
"operationName": "UseViewCount",
"variables": map[string]interface{}{
@ -132,6 +141,20 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
},
// Get offline banner image
{
"operationName": "ChannelShell",
"variables": map[string]interface{}{
"login": streamerName,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "580ab410bcd0c1ad194224957ae2241e5d252b2c5173d8e0cce9d32d5bb14efe",
},
},
},
}
_, body, err := parseResponse(payload)
@ -166,7 +189,9 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
isLive = false
}
parsedStreamer, err := ParseStreamer(streamerData, isLive, parsedSocials, parsedStream, streamerName)
streamerBanner := gjson.Get(string(body), "5.data.userOrError.bannerImageURL").String()
parsedStreamer, err := ParseStreamer(streamerData, isLive, parsedSocials, parsedStream, streamerName, streamerBanner)
if err != nil {
return structs.Streamer{}, err
}