adding recently played endpoint

This commit is contained in:
2025-11-29 00:30:34 +00:00
parent a776c852b2
commit 160dd24bf2
3 changed files with 13 additions and 25 deletions

View File

@@ -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)
}

View File

@@ -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) {

View File

@@ -78,6 +78,7 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif
spotifyauth.WithScopes(
spotifyauth.ScopeUserReadPlaybackState,
spotifyauth.ScopeUserReadCurrentlyPlaying,
spotifyauth.ScopeUserReadRecentlyPlayed,
),
)