initial
This commit is contained in:
45
ytid.go
Normal file
45
ytid.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package ytid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
regexpYoutubeDomain = regexp.MustCompile(`youtu\.?be`)
|
||||
regexpURLPatterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(`youtu\.be\/([^#\&\?]{11})`),
|
||||
regexp.MustCompile(`\?v=([^#\&\?]{11})`),
|
||||
regexp.MustCompile(`\&v=([^#\&\?]{11})`),
|
||||
regexp.MustCompile(`embed\/([^#\&\?]{11})`),
|
||||
regexp.MustCompile(`\/v\/([^#\&\?]{11})`),
|
||||
}
|
||||
)
|
||||
|
||||
func GetID(url string) string {
|
||||
if !regexpYoutubeDomain.MatchString(url) {
|
||||
return ""
|
||||
}
|
||||
for _, pattern := range regexpURLPatterns {
|
||||
if pattern.MatchString(url) {
|
||||
return pattern.FindStringSubmatch(url)[1]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetIDWithError(url string) (string, error) {
|
||||
if !regexpYoutubeDomain.MatchString(url) {
|
||||
return "", fmt.Errorf("ytid: not a youtube domain")
|
||||
}
|
||||
for _, pattern := range regexpURLPatterns {
|
||||
if pattern.MatchString(url) {
|
||||
return pattern.FindStringSubmatch(url)[1], nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("ytid: no match found")
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
Reference in New Issue
Block a user