diff --git a/internal/common/strings.go b/internal/common/strings.go index 96abbb4..1237a7f 100644 --- a/internal/common/strings.go +++ b/internal/common/strings.go @@ -1,6 +1,7 @@ package common import ( + "regexp" "strconv" "strings" ) @@ -21,3 +22,9 @@ func ZeroPad(num int, length int) string { } return strings.Repeat("0", length-len(str)) + str } + +var numericRegex = regexp.MustCompile(`^[\d]+$`) + +func IsNumeric(num string) bool { + return numericRegex.MatchString(num) +} diff --git a/internal/plugin/musify.go b/internal/plugin/musify.go index 2205251..2e89d42 100644 --- a/internal/plugin/musify.go +++ b/internal/plugin/musify.go @@ -2,6 +2,8 @@ package plugin import ( "errors" + "fmt" + "net/url" "regexp" "strings" @@ -616,8 +618,46 @@ func (m Musify) FetchAlbum(source data.Source) (data.Album, error) { return album, nil } -func (m Musify) FetchArtist(source data.Source) (data.Artist, error) { - return data.Artist{ - Name: extractName(source.Url), - }, nil +type parsedArtistUrl struct { + id string + name string +} + +func newParsedArtistUrl(rawUrl string) (parsedArtistUrl, error) { + res := parsedArtistUrl{} + + parsed, err := url.Parse(rawUrl) + if err != nil { + return res, err + } + + dirs := strings.Split(parsed.Path, "/") + correctPart := dirs[len(dirs)-1] + split := strings.Split(correctPart, "-") + if len(split) < 2 { + return res, errors.New("last part of path has to consist of at least one - " + correctPart) + } + + res.id = split[len(split)-1] + res.name = strings.Join(split[:len(split)-1], "-") + + if !common.IsNumeric(res.id) { + return res, errors.New("last elem (id) has to be numeric " + res.id) + } + + return res, nil +} + +func (m Musify) FetchArtist(source data.Source) (data.Artist, error) { + res := data.Artist{ + Name: extractName(source.Url), + } + parsed, err := newParsedArtistUrl(source.Url) + if err != nil { + return res, err + } + + fmt.Println(parsed) + + return res, nil }