adding recently played endpoint
This commit is contained in:
@@ -48,31 +48,17 @@ func (store *Store) ListeningTo(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// If Spotify says "nothing is currently playing"
|
||||
if playing == nil || !playing.Playing || playing.Item == nil {
|
||||
ctx.JSON(200, gin.H{
|
||||
"playing": false,
|
||||
"message": "User is not currently listening to anything",
|
||||
})
|
||||
ctx.JSON(200, playing)
|
||||
}
|
||||
|
||||
func (store *Store) RecentlyPlayed(ctx *gin.Context) {
|
||||
opts := spotify.RecentlyPlayedOptions{Limit: 3}
|
||||
|
||||
played, err := store.SpotifyClient.PlayerRecentlyPlayedOpt(ctx, &opts)
|
||||
if err != nil {
|
||||
ctx.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Extract fields safely
|
||||
item := playing.Item
|
||||
artistName := ""
|
||||
if len(item.Artists) > 0 {
|
||||
artistName = item.Artists[0].Name
|
||||
}
|
||||
|
||||
imgURL := ""
|
||||
if len(item.Album.Images) > 0 {
|
||||
imgURL = item.Album.Images[0].URL
|
||||
}
|
||||
|
||||
ctx.JSON(200, gin.H{
|
||||
"playing": true,
|
||||
"song_name": item.Name,
|
||||
"artist_name": artistName,
|
||||
"album_image": imgURL,
|
||||
})
|
||||
ctx.JSON(200, played)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user