Skip to content

Commit

Permalink
add recommend definition (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jul 23, 2023
1 parent 8936507 commit b79f644
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/functions/definitions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package functions

import (
"context"
"fmt"
"strings"

"github.com/zmb3/spotify/v2"
)

const (
RecommendCount = 5
)

func Recommend(ctx context.Context, client *spotify.Client, genres string) (string, error) {
// genres length needs to be less than 5
if len(genres) > 5 {
genres = genres[:5]
}
genresSlice := strings.Split(genres, ",")

seeds := spotify.Seeds{
Genres: genresSlice,
}

trackAttrib := spotify.NewTrackAttributes()

recommendations, err := client.GetRecommendations(ctx, seeds, trackAttrib, spotify.Limit(RecommendCount))
if err != nil {
return "", fmt.Errorf("fail to get recommendations: %v", err)
}

var output string
for i, track := range recommendations.Tracks {
output += fmt.Sprintf("[%d]\n", i+1)
output += fmt.Sprintf("album: %s\n", track.Album.Name)
var artists []string
for _, artist := range track.Artists {
artists = append(artists, artist.Name)
}
output += fmt.Sprintf("artists: %s\n", strings.Join(artists, ","))
output += fmt.Sprintf("url: %s\n", track.ExternalURLs["spotify"])
output += "\n"
}

return output, nil
}

0 comments on commit b79f644

Please sign in to comment.