ytlinkprov: Follow 3XX codes to last 200

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2024-07-01 04:50:02 +03:00
parent 156ecd35e9
commit 12b3da09ac
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -2,7 +2,10 @@ package ytlinkprov
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http"
"strings"
"time" "time"
"github.com/lrstanley/go-ytdlp" "github.com/lrstanley/go-ytdlp"
@ -70,11 +73,28 @@ func (c *CacheLinkProv) validCache(l TimedLink) bool {
} }
func getRemoteLink(id string) (string, error) { func getRemoteLink(id string) (string, error) {
var link string
vidUrl := fmt.Sprintf("https://youtube.com/watch?v=%s", id) vidUrl := fmt.Sprintf("https://youtube.com/watch?v=%s", id)
ytCmd := ytdlp.New().ExtractAudio().GetURL() ytCmd := ytdlp.New().ExtractAudio().GetURL()
ytRes, err := ytCmd.Run(context.Background(), vidUrl) ytRes, err := ytCmd.Run(context.Background(), vidUrl)
if err != nil { if err != nil {
return "", err 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
} }