added base thing
This commit is contained in:
parent
408e6cf944
commit
c43a7cb154
1
internal/data/album.go
Normal file
1
internal/data/album.go
Normal file
@ -0,0 +1 @@
|
||||
package data
|
15
internal/data/song.go
Normal file
15
internal/data/song.go
Normal file
@ -0,0 +1,15 @@
|
||||
package data
|
||||
|
||||
type Song struct {
|
||||
Id int
|
||||
|
||||
Name string
|
||||
UnifiedName string
|
||||
Isrc string
|
||||
Genre string
|
||||
Note string
|
||||
Tracksort int
|
||||
Artwork string
|
||||
|
||||
Sources []Source
|
||||
}
|
61
internal/data/source.go
Normal file
61
internal/data/source.go
Normal file
@ -0,0 +1,61 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type SourceType struct {
|
||||
Name string
|
||||
Regex regexp.Regexp
|
||||
}
|
||||
|
||||
var SourceTypes = []SourceType{
|
||||
{Name: "Youtube", Regex: *regexp.MustCompile(`(?i)\b(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\b`)},
|
||||
}
|
||||
|
||||
func GetSourceType(name string) *SourceType {
|
||||
for i, st := range SourceTypes {
|
||||
if st.Name == name {
|
||||
return &SourceTypes[i]
|
||||
}
|
||||
}
|
||||
panic("couldn't find source type for " + name)
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
Url string
|
||||
Type *SourceType
|
||||
}
|
||||
|
||||
func (st *SourceType) NewSource(url string) (Source, error) {
|
||||
var err error = nil
|
||||
if !st.Regex.MatchString(url) {
|
||||
err = errors.New("url " + url + " didn't match source " + st.Name)
|
||||
}
|
||||
|
||||
return Source{
|
||||
Url: url,
|
||||
Type: st,
|
||||
}, err
|
||||
}
|
||||
|
||||
func NewSource(url string) (Source, error) {
|
||||
var st *SourceType = nil
|
||||
|
||||
for i, source := range SourceTypes {
|
||||
if source.Regex.MatchString(url) {
|
||||
st = &SourceTypes[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if st == nil {
|
||||
return Source{}, errors.New("couldn't find a source type for the url " + url)
|
||||
}
|
||||
|
||||
return Source{
|
||||
Url: url,
|
||||
Type: st,
|
||||
}, nil
|
||||
}
|
40
internal/data/source_test.go
Normal file
40
internal/data/source_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestYouTube(t *testing.T) {
|
||||
validUrls := []string{
|
||||
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
||||
"http://youtube.com/watch?v=dQw4w9WgXcQ",
|
||||
"https://youtu.be/dQw4w9WgXcQ",
|
||||
"www.youtube.com/watch?v=dQw4w9WgXcQ",
|
||||
"youtube.com/watch?v=dQw4w9WgXcQ",
|
||||
"youtu.be/dQw4w9WgXcQ",
|
||||
"https://www.youtube.com/embed/dQw4w9WgXcQ",
|
||||
"https://www.youtube.com/v/dQw4w9WgXcQ",
|
||||
"https://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ",
|
||||
"https://m.youtube.com/watch?v=dQw4w9WgXcQ",
|
||||
}
|
||||
invalidUrls := []string{
|
||||
"invalid.url",
|
||||
"https://example.com/notyoutube",
|
||||
}
|
||||
|
||||
st := GetSourceType("Youtube")
|
||||
|
||||
for _, u := range validUrls {
|
||||
_, err := st.NewSource(u)
|
||||
if err != nil {
|
||||
t.Errorf(`%q is a valid YouTube url`, u)
|
||||
}
|
||||
}
|
||||
|
||||
for _, u := range invalidUrls {
|
||||
_, err := st.NewSource(u)
|
||||
if err == nil {
|
||||
t.Errorf(`%q is an invalid YouTube url`, u)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user