Compare commits

..

No commits in common. "4f527f374737c6f87464e59dc8e7d10d936ff5cd" and "e833af1ecc745abb4ebaf675dffb90de4cea9eec" have entirely different histories.

7 changed files with 38 additions and 115 deletions

View File

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

View File

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

View File

@ -46,7 +46,6 @@ 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
}
}
@ -85,6 +84,22 @@ 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)
@ -103,8 +118,7 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil {
return song, err
}
song.Sources = append(song.Sources, source)
song = song.Compile().(data.Song)
song.Sources = dedupeSources(append(song.Sources, source))
return song, nil
}
@ -114,8 +128,7 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil {
return album, err
}
album.Sources = append(album.Sources, source)
album = album.Compile().(data.Album)
album.Sources = dedupeSources(append(album.Sources, source))
return album, nil
}
@ -125,8 +138,7 @@ func Fetch(source data.Source) (data.MusicObject, error) {
if err != nil {
return artist, err
}
artist.Sources = append(artist.Sources, source)
artist = artist.Compile().(data.Artist)
artist.Sources = dedupeSources(append(artist.Sources, source))
return artist, nil
}

View File

@ -2,19 +2,10 @@ 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 {
}
@ -38,24 +29,18 @@ func (m Musify) RegexSong() *regexp.Regexp {
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
}
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) {
return data.Album{
Name: extractName(source.Url),
}, nil
}
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) FetchArtist(source data.Source) (data.Artist, error) {
panic("unimplemented")
}
func (m *Musify) FetchAlbum(source data.Source) (data.Album, error) {
panic("unimplemented")
}
func (m *Musify) FetchSong(source data.Source) (data.Song, error) {
panic("unimplemented")
}
func (m *Musify) Search(query string) ([]data.MusicObject, error) {
panic("unimplemented")
}

View File

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