Compare commits
No commits in common. "ee4d476b448eaf8e896318e8c1a0a1d3f30f6143" and "6e107f80f950ac11ad5f930cfe5e734963eb69d2" have entirely different histories.
ee4d476b44
...
6e107f80f9
@ -24,13 +24,9 @@ type Plugin interface {
|
|||||||
var namePlugins = map[string]Plugin{}
|
var namePlugins = map[string]Plugin{}
|
||||||
var nameSourceType = map[string]data.SourceType{}
|
var nameSourceType = map[string]data.SourceType{}
|
||||||
|
|
||||||
func RegisterPlugin(plugin Plugin) error {
|
func RegisterPlugin(plugin Plugin) {
|
||||||
name := plugin.Name()
|
name := plugin.Name()
|
||||||
|
|
||||||
if _, ok := namePlugins[name]; ok {
|
|
||||||
return errors.New("plugin " + name + " is already registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
nameSourceType[name] = data.SourceType{
|
nameSourceType[name] = data.SourceType{
|
||||||
Name: name,
|
Name: name,
|
||||||
Regex: plugin.Regex(),
|
Regex: plugin.Regex(),
|
||||||
@ -40,14 +36,10 @@ func RegisterPlugin(plugin Plugin) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namePlugins[name] = plugin
|
namePlugins[name] = plugin
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileSourceType(source data.Source) (data.Source, error) {
|
func compileSourceType(source data.Source) (data.Source, error) {
|
||||||
if source.SourceType != nil {
|
if source.SourceType != nil {
|
||||||
if _, ok := namePlugins[source.SourceType.Name]; !ok {
|
|
||||||
return source, errors.New("source type " + source.SourceType.Name + " not found")
|
|
||||||
}
|
|
||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,170 +0,0 @@
|
|||||||
package plugin
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
|
||||||
)
|
|
||||||
|
|
||||||
func extractNameTest(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 MusifyTest struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) Name() string {
|
|
||||||
return "MusifyTest"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) Regex() *regexp.Regexp {
|
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/(artist|release|track)/[a-z\-0-9]+`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) RegexArtist() *regexp.Regexp {
|
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/artist/[a-z\-0-9]+`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) RegexAlbum() *regexp.Regexp {
|
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/release/[a-z\-0-9]+`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) RegexSong() *regexp.Regexp {
|
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) FetchSong(source data.Source) (data.Song, error) {
|
|
||||||
return data.Song{
|
|
||||||
Name: extractNameTest(source.Url),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) FetchAlbum(source data.Source) (data.Album, error) {
|
|
||||||
return data.Album{
|
|
||||||
Name: extractNameTest(source.Url),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) FetchArtist(source data.Source) (data.Artist, error) {
|
|
||||||
return data.Artist{
|
|
||||||
Name: extractNameTest(source.Url),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m MusifyTest) Search(query string) ([]data.MusicObject, error) {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRegister(t *testing.T) {
|
|
||||||
if RegisterPlugin(MusifyTest{}) != nil {
|
|
||||||
t.Errorf(`registering first plugin shouldn't return an error`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if RegisterPlugin(MusifyTest{}) == nil {
|
|
||||||
t.Errorf(`registering same plugin twice should return an error`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(namePlugins) != 1 {
|
|
||||||
t.Errorf(`%d plugins where registered`, len(namePlugins))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(nameSourceType) != 1 {
|
|
||||||
t.Errorf(`%d source types were registered`, len(nameSourceType))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFetchSong(t *testing.T) {
|
|
||||||
RegisterPlugin(MusifyTest{})
|
|
||||||
|
|
||||||
s, err := Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
song, ok := s.(data.Song)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf(`fetch should have returned song`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if song.UnifiedName != "linkin park in the end" {
|
|
||||||
t.Errorf(`didn't correctly parse the name`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFetchAlbum(t *testing.T) {
|
|
||||||
RegisterPlugin(MusifyTest{})
|
|
||||||
|
|
||||||
a, err := Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/release/linkin-park-hybrid-theory-2000-188",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
album, ok := a.(data.Album)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf(`fetch should have returned song`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if album.UnifiedName != "linkin park hybrid theory 2000" {
|
|
||||||
t.Errorf(`didn't correctly parse the name`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFetchArtist(t *testing.T) {
|
|
||||||
RegisterPlugin(MusifyTest{})
|
|
||||||
|
|
||||||
a, err := Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/artist/linkin-park-5",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
artist, ok := a.(data.Artist)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf(`fetch should have returned song`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if artist.UnifiedName != "linkin park" {
|
|
||||||
t.Errorf(`didn't correctly parse the name`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFetchWrongUrl(t *testing.T) {
|
|
||||||
RegisterPlugin(MusifyTest{})
|
|
||||||
|
|
||||||
_, err := Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf(`trying to fetch an unknown url should result in error`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNonExistentSourceType(t *testing.T) {
|
|
||||||
RegisterPlugin(MusifyTest{})
|
|
||||||
|
|
||||||
_, err := Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/",
|
|
||||||
SourceType: &data.SourceType{
|
|
||||||
Name: "doesn't exist",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf(`trying to fetch a not recognized source type should result in error`)
|
|
||||||
}
|
|
||||||
}
|
|
9
main.go
9
main.go
@ -11,10 +11,7 @@ func main() {
|
|||||||
fmt.Println("music kraken")
|
fmt.Println("music kraken")
|
||||||
|
|
||||||
plugin.RegisterPlugin(plugin.Musify{})
|
plugin.RegisterPlugin(plugin.Musify{})
|
||||||
|
fmt.Println(plugin.Fetch(data.Source{
|
||||||
a, _ := plugin.Fetch(data.Source{
|
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
|
||||||
Url: "https://musify.club/artist/linkin-park-5",
|
}))
|
||||||
})
|
|
||||||
artist := a.(data.Artist)
|
|
||||||
fmt.Println(artist.UnifiedName)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user