51 lines
1020 B
Go
Raw Normal View History

2024-02-18 01:30:19 -08:00
package commands
import (
"fmt"
2024-02-18 11:56:07 -08:00
"time"
2024-02-18 01:30:19 -08:00
"github.com/zmb3/spotify/v2"
)
2024-02-18 11:56:07 -08:00
func (c *Commander) NowPlaying(force bool) error {
if force {
2024-02-18 23:33:32 -08:00
current, err := c.Client().PlayerCurrentlyPlaying(c.Context)
2024-02-18 11:56:07 -08:00
if err != nil {
return err
}
str := FormatSong(current)
fmt.Println(str)
_, err = c.Cache.Put("now_playing", str, 5*time.Second)
return err
}
song, err := c.Cache.GetOrDo("now_playing", func() (string, error) {
2024-02-18 23:33:32 -08:00
current, err := c.Client().PlayerCurrentlyPlaying(c.Context)
2024-02-18 11:56:07 -08:00
if err != nil {
return "", err
}
str := FormatSong(current)
return str, nil
}, 5*time.Second)
2024-02-18 01:30:19 -08:00
if err != nil {
return err
}
2024-02-18 11:56:07 -08:00
fmt.Println(song)
2024-02-18 01:30:19 -08:00
return nil
}
func FormatSong(current *spotify.CurrentlyPlaying) string {
out := "▶"
if !current.Playing || current == nil {
out = "⏸"
}
if current != nil {
if current.Item != nil {
out += fmt.Sprintf(" %s", current.Item.Name)
if len(current.Item.Artists) > 0 {
out += fmt.Sprintf(" - %s", current.Item.Artists[0].Name)
}
}
}
return out
}