-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8936507
commit b79f644
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |