75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package data
|
|
|
|
import (
|
|
"regexp"
|
|
)
|
|
|
|
type SourceType struct {
|
|
Name string
|
|
Regex *regexp.Regexp
|
|
RegexArtist *regexp.Regexp
|
|
RegexAlbum *regexp.Regexp
|
|
RegexSong *regexp.Regexp
|
|
}
|
|
|
|
var SourceTypes = []SourceType{
|
|
{Name: "Youtube", Regex: regexp.MustCompile(`(?i)\b(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\b`)},
|
|
{Name: "Musify", Regex: regexp.MustCompile(`(?i)https?://musify\.club/(artist|track|release)/[a-z\-0-9]+`)},
|
|
}
|
|
|
|
func GetSourceType(name string) *SourceType {
|
|
for i, st := range SourceTypes {
|
|
if st.Name == name {
|
|
return &SourceTypes[i]
|
|
}
|
|
}
|
|
panic("couldn't find source type for " + name)
|
|
}
|
|
|
|
type ObjectType string
|
|
|
|
const SongSource = ObjectType("song")
|
|
const AlbumSource = ObjectType("album")
|
|
const ArtistSource = ObjectType("artist")
|
|
|
|
type Source struct {
|
|
Url string
|
|
SourceType *SourceType
|
|
ObjectType ObjectType
|
|
}
|
|
|
|
func dedupeSources(inputSources []Source) []Source {
|
|
urlMapping := map[string]int{}
|
|
|
|
deduped := []Source{}
|
|
|
|
for _, source := range inputSources {
|
|
if mergeWithIndex, ok := urlMapping[source.Url]; ok {
|
|
// has to merge current source with source at index
|
|
if source.ObjectType != "" {
|
|
deduped[mergeWithIndex].ObjectType = source.ObjectType
|
|
}
|
|
if source.SourceType != nil {
|
|
deduped[mergeWithIndex].SourceType = source.SourceType
|
|
}
|
|
|
|
} else {
|
|
// just appending
|
|
urlMapping[source.Url] = len(deduped)
|
|
deduped = append(deduped, source)
|
|
}
|
|
}
|
|
|
|
return deduped
|
|
}
|
|
|
|
func sourceIndices(sources []Source) []string {
|
|
res := []string{}
|
|
|
|
for _, source := range sources {
|
|
res = append(res, "url"+source.Url)
|
|
}
|
|
|
|
return res
|
|
}
|