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

57 lines
1.5 KiB
Go

package extractor
import (
"errors"
"safetwitch-backend/extractor/structs"
"time"
"github.com/tidwall/gjson"
)
func ParseSocials(data string) ([]structs.Social, error) {
var parsedSocials []structs.Social
result := gjson.Get(data, "user.channel.socialMedias")
for _, social := range result.Array() {
parsedSocials = append(parsedSocials, structs.Social{
Title: social.Get("title").String(),
Type: social.Get("name").String(),
Url: social.Get("url").String(),
})
}
if !result.Exists() {
return parsedSocials, errors.New("error while parsing socials, path does not exist")
}
return parsedSocials, nil
}
func ParseStream(data string) (*structs.Stream, error) {
// check if live
stream := gjson.Get(data, "1.data.user.stream")
if !stream.IsObject() {
return nil, errors.New("streamer is not live")
}
var tags []string
tagArea := gjson.Get(data, "2.data.user.stream.freeformTags").Array()
for _, tag := range tagArea {
tags = append(tags, tag.Get("name").String())
}
time, err := time.Parse(time.RFC3339, stream.Get("createdAt").String())
if err != nil {
return &structs.Stream{}, err
}
parsedStream := structs.Stream{
Title: gjson.Get(data, "1.data.user.lastBroadcast.title").String(),
Topic: stream.Get("game.name").String(),
StartedAt: time,
Tags: tags,
Viewers: int(gjson.Get(data, "4.data.user.stream.viewersCount").Int()),
Preview: ProxyUrl(gjson.Get(data, "3.data.user.stream.previewImageURL").String()),
}
return &parsedStream, nil
}