request listening to song && create client
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/zmb3/spotify/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (store *Store) CompleteAuth(c *gin.Context) {
|
func (store *Store) CompleteAuth(c *gin.Context) {
|
||||||
@@ -17,6 +18,11 @@ func (store *Store) CompleteAuth(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
store.SpotifyToken = token
|
store.SpotifyToken = token
|
||||||
|
|
||||||
|
client := spotify.New(store.SpotifyAuth.Client(c.Request.Context(), token))
|
||||||
|
|
||||||
|
store.SpotifyClient = client
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "Authentication successful",
|
"message": "Authentication successful",
|
||||||
"token": token.AccessToken,
|
"token": token.AccessToken,
|
||||||
@@ -25,40 +31,40 @@ func (store *Store) CompleteAuth(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (store *Store) ListeningTo(c *gin.Context) {
|
func (store *Store) ListeningTo(c *gin.Context) {
|
||||||
// ctx := context.Background()
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
// playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(ctx)
|
playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(ctx)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// c.JSON(500, gin.H{"error": err.Error()})
|
c.JSON(500, gin.H{"error": err.Error()})
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // If Spotify says "nothing is currently playing"
|
// If Spotify says "nothing is currently playing"
|
||||||
// if playing == nil || !playing.Playing || playing.Item == nil {
|
if playing == nil || !playing.Playing || playing.Item == nil {
|
||||||
// c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
// "playing": false,
|
"playing": false,
|
||||||
// "message": "User is not currently listening to anything",
|
"message": "User is not currently listening to anything",
|
||||||
// })
|
})
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Extract fields safely
|
// Extract fields safely
|
||||||
// item := playing.Item
|
item := playing.Item
|
||||||
// artistName := ""
|
artistName := ""
|
||||||
// if len(item.Artists) > 0 {
|
if len(item.Artists) > 0 {
|
||||||
// artistName = item.Artists[0].Name
|
artistName = item.Artists[0].Name
|
||||||
// }
|
}
|
||||||
|
|
||||||
// imgURL := ""
|
imgURL := ""
|
||||||
// if len(item.Album.Images) > 0 {
|
if len(item.Album.Images) > 0 {
|
||||||
// imgURL = item.Album.Images[0].URL
|
imgURL = item.Album.Images[0].URL
|
||||||
// }
|
}
|
||||||
|
|
||||||
// c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
// "playing": true,
|
"playing": true,
|
||||||
// "song_name": item.Name,
|
"song_name": item.Name,
|
||||||
// "artist_name": artistName,
|
"artist_name": artistName,
|
||||||
// "album_image": imgURL,
|
"album_image": imgURL,
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user