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

Add directory endpoint

This commit is contained in:
dragongoose 2023-05-26 10:17:55 -04:00
parent 75b9075564
commit 1fc11488e3
No known key found for this signature in database
GPG key ID: 50DB99B921579009
6 changed files with 127 additions and 19 deletions

View file

@ -55,3 +55,23 @@ func ParseStream(data string) (*structs.Stream, error) {
return &parsedStream, nil
}
// discover
func ParseCategory(data gjson.Result) (structs.Category, error) {
tags := data.Get("node.tags").Array()
var parsedTags []string
for _, tag := range tags {
parsedTags = append(parsedTags, tag.Get("localizedName").String())
}
return structs.Category{
Name: data.Get("node.name").String(),
DisplayName: data.Get("node.displayName").String(),
Viewers: data.Get("node.viewersCount").String(),
CreatedAt: data.Get("node.originalReleaseDate").Time(),
Tags: parsedTags,
Cursor: data.Get("node.cursor").String(),
Image: ProxyUrl(data.Get("node.avatarURL").String()),
}, nil
}

View file

@ -30,3 +30,13 @@ type Streamer struct {
Id string `json:"id"`
Stream *Stream `json:"stream,omitempty"`
}
type Category struct {
Name string `json:"username"`
DisplayName string `json:"displayName"`
Viewers string `json:"viewers"`
Tags []string `json:"tags"`
CreatedAt time.Time `json:"createdAt"`
Image string `json:"image"`
Cursor string `json:"cursor,omitempty"`
}

View file

@ -24,6 +24,32 @@ func call(url, method string, body io.Reader) (*http.Response, error) {
return resp, err
}
func parseResponse(payload []TwitchPayload) (response *[]structs.TwitchApiResponse, body []byte, err error) {
json_data, err := json.Marshal(payload)
if err != nil {
return nil, nil, nil
}
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
if err != nil {
return nil, nil, nil
}
defer resp.Body.Close()
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, nil
}
var twitchResponse []structs.TwitchApiResponse
err = json.Unmarshal(rawBody, &twitchResponse)
if err != nil {
return nil, nil, nil
}
return &twitchResponse, rawBody, nil
}
type TwitchPayload = map[string]interface{}
func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
@ -90,24 +116,8 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
},
},
}
json_data, err := json.Marshal(payload)
if err != nil {
return structs.Streamer{}, nil
}
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
if err != nil {
return structs.Streamer{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return structs.Streamer{}, err
}
var twitchResponse []structs.TwitchApiResponse
err = json.Unmarshal(body, &twitchResponse)
_, body, err := parseResponse(payload)
if err != nil {
return structs.Streamer{}, err
}
@ -154,3 +164,47 @@ func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
return parsedStreamer, nil
}
func GetDirectory(limit int, cursor string) ([]structs.Category, error) {
payload := []TwitchPayload{
{
"operationName": "BrowsePage_AllDirectories",
"variables": map[string]interface{}{
"limit": limit,
"options": map[string]interface{}{
"recommendationsContext": map[string]interface{}{
"platform": "web",
},
"requestID": "JIRA-VXP-2397",
"sort": "RELEVANCE",
"tags": []string{},
},
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "1d1914ca3cbfaa607ecd5595b2e305e96acf987c8f25328f7713b25f604c4668",
},
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.Category{}, err
}
var parsedCategoryArray []structs.Category
categoryArray := gjson.Get(string(body), "0.data.directoriesWithTags.edges")
for _, categoryRes := range categoryArray.Array() {
parsedCategory, err := ParseCategory(categoryRes)
if err != nil {
return []structs.Category{}, nil
}
parsedCategoryArray = append(parsedCategoryArray, parsedCategory)
}
return parsedCategoryArray, nil
}

View file

@ -0,0 +1,21 @@
package discover
import (
"safetwitch-backend/extractor"
"github.com/gin-gonic/gin"
)
func Routes(route *gin.Engine) {
auth := route.Group("/api/discover")
auth.GET("/", func(context *gin.Context) {
data, err := extractor.GetDirectory(50, "")
if err != nil {
context.Error(err)
return
}
context.JSON(200, extractor.FormatMessage(data, true))
})
}

View file

@ -7,7 +7,7 @@ import (
)
func Routes(route *gin.Engine) {
auth := route.Group("/users")
auth := route.Group("/api/users")
auth.GET("/:streamerName", func(context *gin.Context) {
data, err := extractor.GetStreamerInfo(context.Param("streamerName"))

View file

@ -1,15 +1,18 @@
package routes
import (
"safetwitch-backend/routes/api/discover"
"safetwitch-backend/routes/api/users"
"safetwitch-backend/routes/proxy"
"safetwitch-backend/routes/root"
"safetwitch-backend/routes/users"
"github.com/gin-gonic/gin"
)
func SetRoutes(router *gin.Engine) {
users.Routes(router)
discover.Routes(router)
proxy.Routes(router)
root.Routes(router)
}