2023-09-11 23:09:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os/exec"
|
2023-09-14 18:35:10 +02:00
|
|
|
"strings"
|
2023-09-11 23:09:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-14 18:35:10 +02:00
|
|
|
// yt-dlp flags and options for all links
|
|
|
|
var ytdlpParams []string = []string{
|
|
|
|
"--no-playlist",
|
|
|
|
}
|
|
|
|
|
|
|
|
// yt-dlp flags and options only for youtube links
|
|
|
|
var ytlinkParams []string = []string{
|
|
|
|
"--get-url",
|
|
|
|
"-f 22",
|
|
|
|
}
|
|
|
|
|
2023-09-11 23:09:24 +02:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("yt-dlp", link)
|
2023-09-11 23:09:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-13 12:43:44 +02:00
|
|
|
httpClient := &http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
2023-09-11 23:09:24 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|