viddl/yt.go

77 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"net/http"
"net/url"
"os/exec"
2023-09-14 18:35:10 +02:00
"strings"
)
2023-09-14 18:35:10 +02:00
// yt-dlp flags and options for all links
var ytdlpParams []string = []string{
2023-09-14 18:36:50 +02:00
"--get-url",
2023-09-14 18:35:10 +02:00
"--no-playlist",
}
// yt-dlp flags and options only for youtube links
var ytlinkParams []string = []string{
"-f 22",
}
func getYoutubeDownloadURL(link string) (string, error) {
var dlLink string
2023-09-14 18:35:10 +02:00
params := make([]string, 0)
params = append(params, ytdlpParams...)
if isProbablyYT(link) {
params = append(params, ytlinkParams...)
}
2023-09-14 18:45:33 +02:00
params = append(params, link)
cmd := exec.Command("yt-dlp", params...)
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
}
2023-09-14 18:35:10 +02:00
func isProbablyYT(link string) bool {
return strings.Contains(link, "youtube")
}