implemented recursive dedupe and compile
This commit is contained in:
@@ -60,6 +60,10 @@ func compileSource(source data.Source) (data.Source, error) {
|
||||
}
|
||||
|
||||
// find what object this source corresponds to
|
||||
if source.ObjectType != "" {
|
||||
return source, nil
|
||||
}
|
||||
|
||||
sourceType := source.SourceType
|
||||
|
||||
if sourceType.RegexSong.MatchString(source.Url) {
|
||||
@@ -80,7 +84,24 @@ 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -92,15 +113,33 @@ func Fetch(source data.Source) (data.MusicObject, error) {
|
||||
}
|
||||
|
||||
if source.ObjectType == data.SongSource {
|
||||
return plugin.FetchSong(source)
|
||||
song, err := plugin.FetchSong(source)
|
||||
|
||||
if err != nil {
|
||||
return song, err
|
||||
}
|
||||
song.Sources = dedupeSources(append(song.Sources, source))
|
||||
return song, nil
|
||||
}
|
||||
|
||||
if source.ObjectType == data.AlbumSource {
|
||||
return plugin.FetchAlbum(source)
|
||||
album, err := plugin.FetchAlbum(source)
|
||||
|
||||
if err != nil {
|
||||
return album, err
|
||||
}
|
||||
album.Sources = dedupeSources(append(album.Sources, source))
|
||||
return album, nil
|
||||
}
|
||||
|
||||
if source.ObjectType == data.ArtistSource {
|
||||
return plugin.FetchArtist(source)
|
||||
artist, err := plugin.FetchArtist(source)
|
||||
|
||||
if err != nil {
|
||||
return artist, err
|
||||
}
|
||||
artist.Sources = dedupeSources(append(artist.Sources, source))
|
||||
return artist, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user