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)
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -78,6 +78,7 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif
|
||||
spotifyauth.WithScopes(
|
||||
spotifyauth.ScopeUserReadPlaybackState,
|
||||
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
||||
spotifyauth.ScopeUserReadRecentlyPlayed,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user