diff --git a/internal/apis/applemusic/song.go b/internal/apis/applemusic/song.go index 2ddd711..f02d361 100644 --- a/internal/apis/applemusic/song.go +++ b/internal/apis/applemusic/song.go @@ -1,8 +1,13 @@ package applemusic import ( + "fmt" + "net/url" + "regexp" "strconv" "strings" + + "github.com/gleich/lumber/v3" ) type song struct { @@ -39,6 +44,25 @@ type songResponse struct { } func songFromSongResponse(s songResponse) song { + if s.Attributes.URL == "" { + // remove special characters + re := regexp.MustCompile(`[^\w\s-]`) + slugURL := re.ReplaceAllString(s.Attributes.Name, "") + // replace spaces with hyphens + re = regexp.MustCompile(`\s+`) + slugURL = re.ReplaceAllString(slugURL, "-") + + u, err := url.JoinPath( + "https://music.apple.com/us/song/", + strings.ToLower(slugURL), + fmt.Sprint(s.ID), + ) + if err != nil { + lumber.Error(err, "failed to create URL for song", s.Attributes.Name) + } + s.Attributes.URL = u + } + return song{ Track: s.Attributes.Name, Artist: s.Attributes.ArtistName,