diff --git a/backend/handlers/handle_spotify.go b/backend/handlers/handle_spotify.go index 16da31e..7f0772c 100644 --- a/backend/handlers/handle_spotify.go +++ b/backend/handlers/handle_spotify.go @@ -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) } diff --git a/backend/main.go b/backend/main.go index afe0891..c29ad17 100644 --- a/backend/main.go +++ b/backend/main.go @@ -50,7 +50,8 @@ func main() { r.POST("/refresh", store.RefreshToken) r.GET("/callback", store.CompleteSpotifyAuth) - r.GET("/spotify", store.ListeningTo) + r.GET("/spotify/listening", store.ListeningTo) + r.GET("/spotify/recent", store.RecentlyPlayed) // r.POST("/spotify", store.SendSong) r.GET("/", func(c *gin.Context) { diff --git a/backend/services/spotify.go b/backend/services/spotify.go index a7715dd..e7e790f 100644 --- a/backend/services/spotify.go +++ b/backend/services/spotify.go @@ -78,6 +78,7 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif spotifyauth.WithScopes( spotifyauth.ScopeUserReadPlaybackState, spotifyauth.ScopeUserReadCurrentlyPlaying, + spotifyauth.ScopeUserReadRecentlyPlayed, ), )