From b65d68ed860c7d53c4e2987cc587ab937f6508cf Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Tue, 7 Oct 2025 13:45:32 +0200 Subject: [PATCH] added compile to fetch --- internal/plugin/interface.go | 26 +++++++------------------- internal/plugin/musify.go | 29 ++++++++++++++++++++++------- main.go | 7 ++++++- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/internal/plugin/interface.go b/internal/plugin/interface.go index 7385806..9c9a5ad 100644 --- a/internal/plugin/interface.go +++ b/internal/plugin/interface.go @@ -46,6 +46,7 @@ func compileSourceType(source data.Source) (data.Source, error) { for _, st := range nameSourceType { if m := st.Regex.FindString(source.Url); m != "" { source.Url = m + source.SourceType = &st 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) } -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) { // the fetch function without the post processing of the music objects source, err := compileSource(source) @@ -118,7 +103,8 @@ func Fetch(source data.Source) (data.MusicObject, error) { if err != nil { return song, err } - song.Sources = dedupeSources(append(song.Sources, source)) + song.Sources = append(song.Sources, source) + song = song.Compile().(data.Song) return song, nil } @@ -128,7 +114,8 @@ func Fetch(source data.Source) (data.MusicObject, error) { if err != nil { return album, err } - album.Sources = dedupeSources(append(album.Sources, source)) + album.Sources = append(album.Sources, source) + album = album.Compile().(data.Album) return album, nil } @@ -138,7 +125,8 @@ func Fetch(source data.Source) (data.MusicObject, error) { if err != nil { return artist, err } - artist.Sources = dedupeSources(append(artist.Sources, source)) + artist.Sources = append(artist.Sources, source) + artist = artist.Compile().(data.Artist) return artist, nil } diff --git a/internal/plugin/musify.go b/internal/plugin/musify.go index b301a29..5349856 100644 --- a/internal/plugin/musify.go +++ b/internal/plugin/musify.go @@ -2,10 +2,19 @@ package plugin import ( "regexp" + "strings" "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 { } @@ -29,18 +38,24 @@ func (m Musify) RegexSong() *regexp.Regexp { return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`) } -func (m *Musify) FetchArtist(source data.Source) (data.Artist, error) { - panic("unimplemented") +func (m Musify) FetchSong(source data.Source) (data.Song, error) { + return data.Song{ + Name: extractName(source.Url), + }, nil } -func (m *Musify) FetchAlbum(source data.Source) (data.Album, error) { - panic("unimplemented") +func (m Musify) FetchAlbum(source data.Source) (data.Album, error) { + return data.Album{ + Name: extractName(source.Url), + }, nil } -func (m *Musify) FetchSong(source data.Source) (data.Song, error) { - panic("unimplemented") +func (m Musify) FetchArtist(source data.Source) (data.Artist, error) { + 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") } diff --git a/main.go b/main.go index b0e2d6c..af21e1d 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,14 @@ import ( "fmt" "gitea.elara.ws/Hazel/music-kraken/internal/data" + "gitea.elara.ws/Hazel/music-kraken/internal/plugin" ) func main() { 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", + })) }