mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 05:02:58 -05:00
Working bulk stream info function, needs cleaning
This commit is contained in:
parent
ffda13f006
commit
8d5ca942a6
4 changed files with 88 additions and 22 deletions
|
@ -2,6 +2,7 @@ package twitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"safetwitch-backend/extractor"
|
||||||
"safetwitch-backend/extractor/structs"
|
"safetwitch-backend/extractor/structs"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
@ -112,7 +113,7 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
|
||||||
return structs.Streamer{}, err
|
return structs.Streamer{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedStream, err := ParseStream(string(body))
|
parsedStream, err := ParseStream(string(body), 0)
|
||||||
var isLive bool
|
var isLive bool
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "streamer is not live" {
|
if err.Error() == "streamer is not live" {
|
||||||
|
@ -168,7 +169,7 @@ func GetStreamerId(channelName string) (string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBulkStreamerInfo(streamers []string) string {
|
func GetBulkStreamerInfo(streamers []string) ([]structs.Streamer, error) {
|
||||||
var payload []TwitchPayload
|
var payload []TwitchPayload
|
||||||
for _, streamer := range streamers {
|
for _, streamer := range streamers {
|
||||||
tmp := []TwitchPayload{
|
tmp := []TwitchPayload{
|
||||||
|
@ -261,7 +262,56 @@ func GetBulkStreamerInfo(streamers []string) string {
|
||||||
payload = append(payload, tmp...)
|
payload = append(payload, tmp...)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _ := parseResponse(payload)
|
_, body, err := parseResponse(payload)
|
||||||
|
if err != nil {
|
||||||
|
return []structs.Streamer{}, err
|
||||||
|
}
|
||||||
|
|
||||||
return string(body)
|
len := len(streamers) * 6
|
||||||
|
|
||||||
|
var foundStreamers []structs.Streamer
|
||||||
|
|
||||||
|
for i := 0; i < (len / 6); i++ {
|
||||||
|
offset := i * 6
|
||||||
|
|
||||||
|
// begin parsing response
|
||||||
|
streamerFound := gjson.Get(string(body), extractor.GenGjsonQuery(offset, ".data.user"))
|
||||||
|
if streamerFound.String() == "" {
|
||||||
|
// return structs.Streamer{}, errors.New("streamer not found")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
streamerData := gjson.Get(string(body), extractor.GenGjsonQuery(offset, ".data"))
|
||||||
|
parsedSocials, err := ParseSocials(streamerData.String())
|
||||||
|
if err != nil {
|
||||||
|
// return structs.Streamer{}, err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedStream, err := ParseStream(string(body), offset)
|
||||||
|
var isLive bool
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "streamer is not live" {
|
||||||
|
parsedStream = nil
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if parsedStream != nil {
|
||||||
|
isLive = true
|
||||||
|
} else {
|
||||||
|
isLive = false
|
||||||
|
}
|
||||||
|
|
||||||
|
streamerBanner := gjson.Get(string(body), extractor.GenGjsonQuery(offset+5, ".data.userOrError.bannerImageURL")).String()
|
||||||
|
parsedStreamer, err := ParseStreamer(streamerData, isLive, parsedSocials, parsedStream, streamers[i], streamerBanner)
|
||||||
|
if err != nil {
|
||||||
|
return []structs.Streamer{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
foundStreamers = append(foundStreamers, parsedStreamer)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundStreamers, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,15 +66,20 @@ func ParseSocials(data string) ([]structs.Social, error) {
|
||||||
return parsedSocials, nil
|
return parsedSocials, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseStream(data string) (*structs.Stream, error) {
|
// Takes in all of the data returned from twitch (see payload from Streamer.go)
|
||||||
|
// and parses it into a stream. Offset is used for bulk processing
|
||||||
|
// when the offsets are different. Use offset 0 for normal uses
|
||||||
|
func ParseStream(data string, offset int) (*structs.Stream, error) {
|
||||||
// check if live
|
// check if live
|
||||||
stream := gjson.Get(data, "1.data.user.stream")
|
// default "1.data.user.stream"
|
||||||
|
stream := gjson.Get(data, extractor.GenGjsonQuery(offset+1, ".data.user.stream"))
|
||||||
if !stream.IsObject() {
|
if !stream.IsObject() {
|
||||||
return nil, errors.New("streamer is not live")
|
return nil, errors.New("streamer is not live")
|
||||||
}
|
}
|
||||||
|
|
||||||
var tags []string
|
var tags []string
|
||||||
tagArea := gjson.Get(data, "2.data.user.stream.freeformTags").Array()
|
// default "2.data.user.stream.freeformTags"
|
||||||
|
tagArea := gjson.Get(data, extractor.GenGjsonQuery(offset+2, ".data.user.stream.freeformTags")).Array()
|
||||||
for _, tag := range tagArea {
|
for _, tag := range tagArea {
|
||||||
tags = append(tags, tag.Get("name").String())
|
tags = append(tags, tag.Get("name").String())
|
||||||
}
|
}
|
||||||
|
@ -85,12 +90,15 @@ func ParseStream(data string) (*structs.Stream, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedStream := structs.Stream{
|
parsedStream := structs.Stream{
|
||||||
Title: gjson.Get(data, "1.data.user.lastBroadcast.title").String(),
|
// default "1.data.user.lastBroadcast.title"
|
||||||
|
Title: gjson.Get(data, extractor.GenGjsonQuery(offset+1, ".data.user.lastBroadcast.title")).String(),
|
||||||
Topic: stream.Get("game.name").String(),
|
Topic: stream.Get("game.name").String(),
|
||||||
StartedAt: time,
|
StartedAt: time,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Viewers: int(gjson.Get(data, "4.data.user.stream.viewersCount").Int()),
|
// default "4.data.user.stream.viewersCount"
|
||||||
Preview: extractor.ProxyUrl(gjson.Get(data, "3.data.user.stream.previewImageURL").String()),
|
Viewers: int(gjson.Get(data, extractor.GenGjsonQuery(offset+4, ".data.user.stream.viewersCount")).Int()),
|
||||||
|
// default "3.data.user.stream.previewImageURL"
|
||||||
|
Preview: extractor.ProxyUrl(gjson.Get(data, extractor.GenGjsonQuery(offset+3, ".data.user.stream.previewImageURL")).String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &parsedStream, nil
|
return &parsedStream, nil
|
||||||
|
|
|
@ -2,9 +2,14 @@ package extractor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
b64 "encoding/base64"
|
b64 "encoding/base64"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GenGjsonQuery(i int, query string) string {
|
||||||
|
return fmt.Sprintf("%d%s", i, query)
|
||||||
|
}
|
||||||
|
|
||||||
func ProxyUrl(url string) string {
|
func ProxyUrl(url string) string {
|
||||||
encodedUrl := b64.URLEncoding.EncodeToString([]byte(url))
|
encodedUrl := b64.URLEncoding.EncodeToString([]byte(url))
|
||||||
backendUrl := os.Getenv("URL")
|
backendUrl := os.Getenv("URL")
|
||||||
|
|
|
@ -28,18 +28,21 @@ func Routes(route *gin.Engine) {
|
||||||
context.JSON(200, extractor.FormatMessage(data, true))
|
context.JSON(200, extractor.FormatMessage(data, true))
|
||||||
})
|
})
|
||||||
|
|
||||||
// type postData struct {
|
type postData struct {
|
||||||
// Streamers []string `json:"streamers"`
|
Streamers []string `json:"streamers"`
|
||||||
// }
|
}
|
||||||
|
|
||||||
// auth.POST("/bulk", func(context *gin.Context) {
|
auth.POST("/bulk", func(context *gin.Context) {
|
||||||
// var f postData
|
var f postData
|
||||||
// err := context.ShouldBindJSON(&f)
|
err := context.ShouldBindJSON(&f)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// context.Error(err)
|
context.Error(err)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// data := twitch.GetBulkStreamerInfo(f.Streamers)
|
data, err := twitch.GetBulkStreamerInfo(f.Streamers)
|
||||||
// context.JSON(200, extractor.FormatMessage(data, true))
|
if err != nil {
|
||||||
// })
|
context.Error(err)
|
||||||
|
}
|
||||||
|
context.JSON(200, extractor.FormatMessage(data, true))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue