From 0953928e5e744ce4b5119984d47a870edc1407a0 Mon Sep 17 00:00:00 2001 From: dragongoose Date: Tue, 17 Oct 2023 21:55:07 -0400 Subject: [PATCH] Make clips not need streamer name --- extractor/twitch/Clip.go | 35 ++++++++++++++++++++++++++++++++++- routes/api/clips/clips.go | 5 ++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/extractor/twitch/Clip.go b/extractor/twitch/Clip.go index 0a49bef..30dea14 100644 --- a/extractor/twitch/Clip.go +++ b/extractor/twitch/Clip.go @@ -8,7 +8,39 @@ import ( "github.com/tidwall/gjson" ) -func GetClipMetadata(clipSlug string, streamerName string) (structs.Video, error) { +func GetClipUsername(clipSlug string) (string, error) { + payload := []TwitchPayload{ + { + "operationName": "VideoPlayerStreamInfoOverlayClip", + "variables": map[string]interface{}{ + "slug": clipSlug, + }, + "extensions": map[string]interface{}{ + "persistedQuery": map[string]interface{}{ + "version": 1, + "sha256Hash": "d026f1fc6ec6862869684a9768c20e3f6d51f6fd520a2a351dc274356e9c6eb0", + }, + }, + }, + } + + _, body, err := parseResponse(payload) + if err != nil { + return "", err + } + + videoData := gjson.Get(string(body), "0.data.clip") + streamerName := videoData.Get("broadcaster.login").String() + return streamerName, nil + +} + +func GetClipMetadata(clipSlug string) (structs.Video, error) { + streamerName, err := GetClipUsername(clipSlug) + if err != nil { + return structs.Video{}, err + } + payload := []TwitchPayload{ { "operationName": "ClipMetadata", @@ -56,6 +88,7 @@ func GetClipMetadata(clipSlug string, streamerName string) (structs.Video, error videoData := gjson.Get(string(body), "0.data.clip") return structs.Video{ + Type: "clip", Title: videoData.Get("title").String(), Preview: extractor.ProxyUrl(videoData.Get("previewThumbnailURL").String()), Duration: int(videoData.Get("lengthSeconds").Int()), diff --git a/routes/api/clips/clips.go b/routes/api/clips/clips.go index 13bfafd..8757b2c 100644 --- a/routes/api/clips/clips.go +++ b/routes/api/clips/clips.go @@ -21,10 +21,9 @@ func Routes(route *gin.Engine) { context.JSON(200, extractor.FormatMessage(data, true)) }) - clips.GET("/:streamerName/:slug", func(context *gin.Context) { + clips.GET("/:slug", func(context *gin.Context) { slug := context.Param("slug") - streamerName := context.Param("streamerName") - data, err := twitch.GetClipMetadata(slug, streamerName) + data, err := twitch.GetClipMetadata(slug) if err != nil { context.Error(err) return