add new youtubedl dependency

This commit is contained in:
2024-11-05 15:29:49 +01:00
parent 584084c1bc
commit 60dc43fd9b
27 changed files with 2780 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
package youtube
import (
"regexp"
"strings"
)
var videoRegexpList = []*regexp.Regexp{
regexp.MustCompile(`(?:v|embed|shorts|watch\?v)(?:=|/)([^"&?/=%]{11})`),
regexp.MustCompile(`(?:=|/)([^"&?/=%]{11})`),
regexp.MustCompile(`([^"&?/=%]{11})`),
}
// ExtractVideoID extracts the videoID from the given string
func ExtractVideoID(videoID string) (string, error) {
if strings.Contains(videoID, "youtu") || strings.ContainsAny(videoID, "\"?&/<%=") {
for _, re := range videoRegexpList {
if isMatch := re.MatchString(videoID); isMatch {
subs := re.FindStringSubmatch(videoID)
videoID = subs[1]
}
}
}
if strings.ContainsAny(videoID, "?&/<%=") {
return "", ErrInvalidCharactersInVideoID
}
if len(videoID) < 10 {
return "", ErrVideoIDMinLength
}
return videoID, nil
}