From c43a7cb154f9854dbd739646cffad51bda3fe8ab Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 17 Jul 2025 16:41:50 +0200 Subject: [PATCH] added base thing --- go.mod | 3 ++ internal/data/album.go | 1 + internal/data/song.go | 15 +++++++++ internal/data/source.go | 61 ++++++++++++++++++++++++++++++++++++ internal/data/source_test.go | 40 +++++++++++++++++++++++ main.go | 12 +++++++ 6 files changed, 132 insertions(+) create mode 100644 go.mod create mode 100644 internal/data/album.go create mode 100644 internal/data/song.go create mode 100644 internal/data/source.go create mode 100644 internal/data/source_test.go create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ae32033 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.elara.ws/Hazel/music-kraken + +go 1.24.2 diff --git a/internal/data/album.go b/internal/data/album.go new file mode 100644 index 0000000..0ad59c2 --- /dev/null +++ b/internal/data/album.go @@ -0,0 +1 @@ +package data diff --git a/internal/data/song.go b/internal/data/song.go new file mode 100644 index 0000000..a36d6be --- /dev/null +++ b/internal/data/song.go @@ -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 +} diff --git a/internal/data/source.go b/internal/data/source.go new file mode 100644 index 0000000..f548340 --- /dev/null +++ b/internal/data/source.go @@ -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 +} diff --git a/internal/data/source_test.go b/internal/data/source_test.go new file mode 100644 index 0000000..1d3f7b4 --- /dev/null +++ b/internal/data/source_test.go @@ -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) + } + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..7aed47f --- /dev/null +++ b/main.go @@ -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") +}