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

Add VOD preview support

This commit is contained in:
dragongoose 2023-07-04 20:54:12 -04:00
parent e3aa287050
commit 4e89ba8637
No known key found for this signature in database
GPG key ID: 50DB99B921579009
5 changed files with 172 additions and 0 deletions

View file

@ -83,3 +83,33 @@ type SearchResult struct {
RelatedLiveChannels []Streamer `json:"relatedChannels"`
ChannelsWithTag []Streamer `json:"channelsWithTag"`
}
type MinifiedCategory struct {
Image string `json:"image"`
Id string `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
}
type MinifiedStreamer struct {
Name string `json:"name"`
Login string `json:"login"`
Pfp string `json:"pfp"`
ColorHex string `json:"colorHex"`
}
type Video struct {
Preview string `json:"preview"`
Game MinifiedCategory `json:"game"`
Duration int `json:"duration"`
Title string `json:"title"`
PublishedAt time.Time `json:"publishedAt"`
Views int `json:"views"`
Tag []string `json:"tags,omitempty"`
Streamer MinifiedStreamer `json:"streamer"`
}
type Shelve struct {
Title string `json:"title"`
Videos []Video `json:"videos"`
}

View file

@ -211,3 +211,77 @@ func ProxyPlaylistFile(playlist string, isSubPlaylist bool) string {
return modifiedPlaylist
}
func ParseMinifiedCategory(data gjson.Result) structs.MinifiedCategory {
return structs.MinifiedCategory{
Image: extractor.ProxyUrl(data.Get("boxArtURL").String()),
Name: data.Get("name").String(),
DisplayName: data.Get("displayName").String(),
Id: data.Get("id").String(),
}
}
func ParseMinifiedStreamer(data gjson.Result) structs.MinifiedStreamer {
return structs.MinifiedStreamer{
Name: data.Get("displayName").String(),
Login: data.Get("login").String(),
Pfp: extractor.ProxyUrl(data.Get("profileImageURL").String()),
ColorHex: data.Get("primaryColorHex").String(),
}
}
func ParseVideo(data gjson.Result) structs.Video {
tags := []string{}
for _, tag := range data.Get("contentTags").Array() {
tags = append(tags, tag.Get("localizedName").String())
}
return structs.Video{
Preview: extractor.ProxyUrl(data.Get("previewThumbnailURL").String()),
Game: ParseMinifiedCategory(data.Get("game")),
Duration: int(data.Get("lengthSeconds").Int()),
Title: data.Get("title").String(),
PublishedAt: data.Get("createdAt").Time(),
Views: int(data.Get("viewCount").Int()),
Tag: tags,
Streamer: ParseMinifiedStreamer(data.Get("owner")),
}
}
func ParseClip(data gjson.Result) structs.Video {
tags := []string{}
for _, tag := range data.Get("contentTags").Array() {
tags = append(tags, tag.Get("localizedName").String())
}
return structs.Video{
Preview: extractor.ProxyUrl(data.Get("thumbnailURL").String()),
Game: ParseMinifiedCategory(data.Get("clipGame")),
Duration: int(data.Get("lengthSeconds").Int()),
Title: data.Get("clipTitle").String(),
PublishedAt: data.Get("publishedAt").Time(),
Views: int(data.Get("clipViewCount").Int()),
Tag: tags,
Streamer: ParseMinifiedStreamer(data.Get("broadcaster")),
}
}
func ParseShelve(data gjson.Result) structs.Shelve {
rawVideos := data.Get("items").Array()
parsedVideos := []structs.Video{}
isClip := data.Get("type").String() == "TOP_CLIPS"
for _, video := range rawVideos {
if isClip {
parsedVideos = append(parsedVideos, ParseClip(video))
} else {
parsedVideos = append(parsedVideos, ParseVideo(video))
}
}
return structs.Shelve{
Title: data.Get("title").String(),
Videos: parsedVideos,
}
}

View file

@ -580,3 +580,52 @@ func GetStreamerId(channelName string) (string, error) {
return "", errors.New("could not find user")
}
}
/*
If you don't understand the meaning of shelve in
this context, go to an offline streamer on twitch
and look under the videos tag. Each row is considered
a shelve, and each can be expanded. This is to be used
on a streamers profile page
*/
func GetStreamerVideoShelves(channelName string) ([]structs.Shelve, error) {
payload := []TwitchPayload{
{
"operationName": "ChannelVideoShelvesQuery",
"variables": map[string]interface{}{
"channelLogin": channelName,
"first": 5,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "8afefb1ed16c4d8e20fa55024a7ed1727f63b6eca47d8d33a28500770bad8479",
},
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.Shelve{}, err
}
/*
Twitch seperates videos by shelves.
There are 4 shells:
- Recent broadcasts
- Recent highlights and uploads
- Popular clips
- All videos
Each one of these shelves can be expanded to get more data
*/
shelves := gjson.Get(string(body), "0.data.user.videoShelves.edges")
parsedShelves := []structs.Shelve{}
for _, shelve := range shelves.Array() {
parsedShelves = append(parsedShelves, ParseShelve(shelve.Get("node")))
}
return parsedShelves, nil
}

View file

@ -0,0 +1,17 @@
package vods
import (
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/twitch"
"github.com/gin-gonic/gin"
)
func Routes(route *gin.Engine) {
auth := route.Group("/api/vods")
auth.GET("/shelve/:streamerName", func(context *gin.Context) {
data, _ := twitch.GetStreamerVideoShelves(context.Param("streamerName"))
context.JSON(200, extractor.FormatMessage(data, true))
})
}

View file

@ -5,6 +5,7 @@ import (
"safetwitch-backend/routes/api/discover"
"safetwitch-backend/routes/api/search"
"safetwitch-backend/routes/api/users"
"safetwitch-backend/routes/api/vods"
"safetwitch-backend/routes/proxy"
"safetwitch-backend/routes/root"
@ -16,6 +17,7 @@ func SetRoutes(router *gin.Engine) {
discover.Routes(router)
badges.Routes(router)
search.Routes(router)
vods.Routes(router)
proxy.Routes(router)
root.Routes(router)