From 12b3da09ac1a797aa535683e427f98bd38cec59c Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 1 Jul 2024 04:50:02 +0300 Subject: [PATCH] ytlinkprov: Follow 3XX codes to last 200 Signed-off-by: HeshamTB --- ytlinkprov/cache_provider.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ytlinkprov/cache_provider.go b/ytlinkprov/cache_provider.go index 6879a57..d01411e 100644 --- a/ytlinkprov/cache_provider.go +++ b/ytlinkprov/cache_provider.go @@ -2,7 +2,10 @@ package ytlinkprov import ( "context" + "errors" "fmt" + "net/http" + "strings" "time" "github.com/lrstanley/go-ytdlp" @@ -70,11 +73,28 @@ func (c *CacheLinkProv) validCache(l TimedLink) bool { } func getRemoteLink(id string) (string, error) { + + var link string + vidUrl := fmt.Sprintf("https://youtube.com/watch?v=%s", id) ytCmd := ytdlp.New().ExtractAudio().GetURL() ytRes, err := ytCmd.Run(context.Background(), vidUrl) if err != nil { return "", err } - return ytRes.Stdout, nil + linkFirst := strings.Split(ytRes.Stdout, "\n")[0] + + /* Get the last link in a chain of 3XX codes*/ + resp, err := http.Get(link) + if err != nil { + return "", err + } + + if resp.StatusCode != http.StatusOK { + return linkFirst, nil + } + + link = resp.Request.URL.String() + return link, nil + }