viddl/yt.go
HeshamTB 13269a7b84 feat: better url validation:
- Do not accept redirects. Usually
	on yt at least, redirect means the link
	is invalid. Hence, it redirects to the home
	page.

	- This is tested and it works well. trust.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
2023-09-13 13:43:44 +03:00

52 lines
819 B
Go

package main
import (
"net/http"
"net/url"
"os/exec"
)
func getYoutubeDownloadURL(link string) (string, error) {
var dlLink string
cmd := exec.Command("yt-dlp", "--get-url", "-f 22", link)
result, err := cmd.Output()
if err != nil {
return "", err
}
dlLink = string(result)
return dlLink, nil
}
func isValidURL(data string) bool {
_, err := url.ParseRequestURI(data)
if err != nil {
return false
}
httpClient := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := httpClient.Get(data)
if err != nil {
return false
}
if resp.StatusCode != 200 {
return false
}
return true
}