adding spotify what am I listening to api

This commit is contained in:
2025-11-22 11:29:11 +00:00
parent 491c591c3c
commit 8ed08d8f3d
7 changed files with 536 additions and 54 deletions

View File

@@ -1,21 +1,45 @@
package handlers
import (
"context"
"github.com/gin-gonic/gin"
)
func (store *Store) ListeningTo(c *gin.Context) {
ctx := context.Background()
// Communicate with spotify API
// Return if I'm listening to a song
return
}
func (store *Store) SendSong(c *gin.Context) {
// Communicate with spotify API
// Play song on spotify
// Disallow new song for duration of song
return
playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(ctx)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
// If Spotify says "nothing is currently playing"
if playing == nil || !playing.Playing || playing.Item == nil {
c.JSON(200, gin.H{
"playing": false,
"message": "User is not currently listening to anything",
})
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
}
c.JSON(200, gin.H{
"playing": true,
"song_name": item.Name,
"artist_name": artistName,
"album_image": imgURL,
})
}

View File

@@ -1,9 +1,11 @@
package handlers
import (
"github.com/zmb3/spotify/v2"
"gorm.io/gorm"
)
type Store struct {
DB *gorm.DB
DB *gorm.DB
SpotifyClient *spotify.Client
}