implemented proper fetching and matching the source

This commit is contained in:
Hazel Noack
2025-10-07 11:08:40 +02:00
parent 62cb5259ac
commit 6fd2478359
4 changed files with 99 additions and 69 deletions

View File

@@ -1,13 +1,15 @@
package data
import (
"errors"
"regexp"
)
type SourceType struct {
Name string
Regex *regexp.Regexp
Name string
Regex *regexp.Regexp
RegexArtist *regexp.Regexp
RegexAlbum *regexp.Regexp
RegexSong *regexp.Regexp
}
var SourceTypes = []SourceType{
@@ -24,39 +26,14 @@ func GetSourceType(name string) *SourceType {
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
Type *SourceType
}
func (st *SourceType) NewSource(url string) (Source, error) {
var err error = nil
if !st.Regex.MatchString(url) {
err = errors.New("url " + url + " didn't match source " + st.Name)
}
return Source{
Url: url,
Type: st,
}, err
}
func NewSource(url string) (Source, error) {
var st *SourceType = nil
for i, source := range SourceTypes {
if source.Regex.MatchString(url) {
st = &SourceTypes[i]
break
}
}
if st == nil {
return Source{}, errors.New("couldn't find a source type for the url " + url)
}
return Source{
Url: url,
Type: st,
}, nil
Url string
SourceType *SourceType
ObjectType ObjectType
}