Compare commits

...

3 Commits

Author SHA1 Message Date
Hazel Noack
4f527f3747 added basic tests 2025-10-07 14:03:12 +02:00
Hazel Noack
b65d68ed86 added compile to fetch 2025-10-07 13:45:32 +02:00
Hazel Noack
f1b075f47b draft 2025-10-07 13:28:45 +02:00
7 changed files with 109 additions and 32 deletions

View File

@ -0,0 +1,37 @@
package common
import "testing"
func TestUnify(t *testing.T) {
if "foo" != Unify("foo") {
t.Errorf(`"foo" has to be untouched by unify`)
}
if "foo" != Unify("Foo") {
t.Errorf(`"Foo" needs to be lowercase after test`)
}
if "foo" != Unify(" Foo") {
t.Errorf(`The spaces need to be stripped`)
}
if "foo" != Unify(" Foo ") {
t.Errorf(`The spaces need to be stripped`)
}
if "foo bar" != Unify("Foo bar") {
t.Errorf(`Single whitespaces need to be left alone`)
}
if "foo bar" != Unify("Foo bar") {
t.Errorf(`Double whitespaces need to be removed`)
}
if "foo bar" != Unify("Foo bar") {
t.Errorf(`Double whitespaces need to be removed`)
}
if "foo bar baz" != Unify("Foo bar baz") {
t.Errorf(`Double whitespaces need to be removed`)
}
}

View File

@ -4,7 +4,6 @@ import (
"strconv" "strconv"
"gitea.elara.ws/Hazel/music-kraken/internal/common" "gitea.elara.ws/Hazel/music-kraken/internal/common"
"github.com/gohugoio/hugo/common"
) )
type MusicObject interface { type MusicObject interface {
@ -106,6 +105,7 @@ func (m Song) Merge(other MusicObject) MusicObject {
func (m Song) Compile() MusicObject { func (m Song) Compile() MusicObject {
m.Sources = dedupeSources(m.Sources) m.Sources = dedupeSources(m.Sources)
m.UnifiedName = common.Unify(m.Name) m.UnifiedName = common.Unify(m.Name)
m.Album = m.Album.Compile().(Album)
m.Artists = dedupeMusicObjects(m.Artists) m.Artists = dedupeMusicObjects(m.Artists)
return m return m
} }

View File

@ -0,0 +1,34 @@
package data
import (
"testing"
)
func TestSong(t *testing.T) {
song := Song{
Name: "song_a",
Album: Album{
Name: "album_a",
Artists: []Artist{
{Name: "artist_a"},
{Name: "artist_a"},
{Name: "artist_b"},
},
},
Artists: []Artist{
{Name: "artist_a"},
{Name: "artist_a"},
{Name: "artist_b"},
},
}
compiled := song.Compile().(Song)
if len(compiled.Artists) != 2 {
t.Errorf(`the two artists with the same name have to be merged, %d`, len(compiled.Artists))
}
if len(compiled.Album.Artists) != 2 {
t.Errorf(`the two artists with the same names in the album have to be merged, %d`, len(compiled.Album.Artists))
}
}

View File

@ -1,9 +1,6 @@
package data package data
import ( /*
"testing"
)
func TestYouTube(t *testing.T) { func TestYouTube(t *testing.T) {
validUrls := []string{ validUrls := []string{
"https://www.youtube.com/watch?v=dQw4w9WgXcQ", "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
@ -68,3 +65,4 @@ func TestMusify(t *testing.T) {
} }
} }
} }
*/

View File

@ -46,6 +46,7 @@ func compileSourceType(source data.Source) (data.Source, error) {
for _, st := range nameSourceType { for _, st := range nameSourceType {
if m := st.Regex.FindString(source.Url); m != "" { if m := st.Regex.FindString(source.Url); m != "" {
source.Url = m source.Url = m
source.SourceType = &st
return source, nil return source, nil
} }
} }
@ -84,22 +85,6 @@ func compileSource(source data.Source) (data.Source, error) {
return source, errors.New("couldn't find corresponding object source on " + sourceType.Name + " for " + source.Url) return source, errors.New("couldn't find corresponding object source on " + sourceType.Name + " for " + source.Url)
} }
func dedupeSources(sources []data.Source) []data.Source {
urls := map[string]interface{}{}
deduped := []data.Source{}
for _, raw := range sources {
parsed, _ := compileSource(raw)
if _, u := urls[parsed.Url]; !u {
urls[parsed.Url] = true
deduped = append(deduped, parsed)
}
}
return deduped
}
func Fetch(source data.Source) (data.MusicObject, error) { func Fetch(source data.Source) (data.MusicObject, error) {
// the fetch function without the post processing of the music objects // the fetch function without the post processing of the music objects
source, err := compileSource(source) source, err := compileSource(source)
@ -118,7 +103,8 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil { if err != nil {
return song, err return song, err
} }
song.Sources = dedupeSources(append(song.Sources, source)) song.Sources = append(song.Sources, source)
song = song.Compile().(data.Song)
return song, nil return song, nil
} }
@ -128,7 +114,8 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil { if err != nil {
return album, err return album, err
} }
album.Sources = dedupeSources(append(album.Sources, source)) album.Sources = append(album.Sources, source)
album = album.Compile().(data.Album)
return album, nil return album, nil
} }
@ -138,7 +125,8 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil { if err != nil {
return artist, err return artist, err
} }
artist.Sources = dedupeSources(append(artist.Sources, source)) artist.Sources = append(artist.Sources, source)
artist = artist.Compile().(data.Artist)
return artist, nil return artist, nil
} }

View File

@ -2,10 +2,19 @@ package plugin
import ( import (
"regexp" "regexp"
"strings"
"gitea.elara.ws/Hazel/music-kraken/internal/data" "gitea.elara.ws/Hazel/music-kraken/internal/data"
) )
func extractName(s string) string {
parts := strings.Split(s, "/")
lastPart := parts[len(parts)-1]
hyphenParts := strings.Split(lastPart, "-")
result := strings.Join(hyphenParts[:len(hyphenParts)-1], " ")
return result
}
type Musify struct { type Musify struct {
} }
@ -29,18 +38,24 @@ func (m Musify) RegexSong() *regexp.Regexp {
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`) return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
} }
func (m *Musify) FetchArtist(source data.Source) (data.Artist, error) { func (m Musify) FetchSong(source data.Source) (data.Song, error) {
panic("unimplemented") return data.Song{
Name: extractName(source.Url),
}, nil
} }
func (m *Musify) FetchAlbum(source data.Source) (data.Album, error) { func (m Musify) FetchAlbum(source data.Source) (data.Album, error) {
panic("unimplemented") return data.Album{
Name: extractName(source.Url),
}, nil
} }
func (m *Musify) FetchSong(source data.Source) (data.Song, error) { func (m Musify) FetchArtist(source data.Source) (data.Artist, error) {
panic("unimplemented") return data.Artist{
Name: extractName(source.Url),
}, nil
} }
func (m *Musify) Search(query string) ([]data.MusicObject, error) { func (m Musify) Search(query string) ([]data.MusicObject, error) {
panic("unimplemented") panic("unimplemented")
} }

View File

@ -4,9 +4,14 @@ import (
"fmt" "fmt"
"gitea.elara.ws/Hazel/music-kraken/internal/data" "gitea.elara.ws/Hazel/music-kraken/internal/data"
"gitea.elara.ws/Hazel/music-kraken/internal/plugin"
) )
func main() { func main() {
fmt.Println("music kraken") fmt.Println("music kraken")
fmt.Println(data.Source{}.SourceType == nil)
plugin.RegisterPlugin(plugin.Musify{})
fmt.Println(plugin.Fetch(data.Source{
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
}))
} }