adding recently played endpoint
This commit is contained in:
@@ -48,31 +48,17 @@ func (store *Store) ListeningTo(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Spotify says "nothing is currently playing"
|
ctx.JSON(200, playing)
|
||||||
if playing == nil || !playing.Playing || playing.Item == nil {
|
}
|
||||||
ctx.JSON(200, gin.H{
|
|
||||||
"playing": false,
|
func (store *Store) RecentlyPlayed(ctx *gin.Context) {
|
||||||
"message": "User is not currently listening to anything",
|
opts := spotify.RecentlyPlayedOptions{Limit: 3}
|
||||||
})
|
|
||||||
|
played, err := store.SpotifyClient.PlayerRecentlyPlayedOpt(ctx, &opts)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract fields safely
|
ctx.JSON(200, played)
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ func main() {
|
|||||||
r.POST("/refresh", store.RefreshToken)
|
r.POST("/refresh", store.RefreshToken)
|
||||||
|
|
||||||
r.GET("/callback", store.CompleteSpotifyAuth)
|
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.POST("/spotify", store.SendSong)
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif
|
|||||||
spotifyauth.WithScopes(
|
spotifyauth.WithScopes(
|
||||||
spotifyauth.ScopeUserReadPlaybackState,
|
spotifyauth.ScopeUserReadPlaybackState,
|
||||||
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
||||||
|
spotifyauth.ScopeUserReadRecentlyPlayed,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user