added base thing

This commit is contained in:
Hazel Noack 2025-07-17 16:41:50 +02:00
parent 408e6cf944
commit c43a7cb154
6 changed files with 132 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.elara.ws/Hazel/music-kraken
go 1.24.2

1
internal/data/album.go Normal file
View File

@ -0,0 +1 @@
package data

15
internal/data/song.go Normal file
View 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
View 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
}

View 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)
}
}
}

12
main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"fmt"
"gitea.elara.ws/Hazel/music-kraken/internal/data"
)
func main() {
fmt.Println("music kraken")
data.GetSourceType("Youtube")
}