Skip to content

Commit

Permalink
feat: create URL if it doesn't exist for a given song
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <git@mattglei.ch>
  • Loading branch information
gleich committed Nov 29, 2024
1 parent 8589b0b commit 973dea6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/apis/applemusic/song.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package applemusic

import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"

"github.com/gleich/lumber/v3"
)

type song struct {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 973dea6

Please sign in to comment.