mpc-extra/mpd/song.go
2023-08-27 15:47:46 -05:00

72 lines
1.2 KiB
Go

package mpd
import (
"fmt"
"github.com/akrennmair/slice"
"github.com/fhs/gompd/v2/mpd"
)
type Song struct {
Path string
Attrs mpd.Attrs
stickers []mpd.Sticker
}
func newSong(attrs mpd.Attrs) *Song {
return &Song{
Path: attrs["file"],
Attrs: attrs,
}
}
func (s *Song) GetStickers() ([]mpd.Sticker, error) {
if s.stickers != nil {
return s.stickers, nil
}
if connerror != nil {
return nil, connerror
}
sticks, err := StickersGetFor(s.Path)
s.stickers = sticks
return s.stickers, err
}
func (s *Song) PrintAll(withStickers bool) {
for k, v := range s.Attrs {
fmt.Println(k, ":", v)
}
if withStickers {
sticks, _ := s.GetStickers()
for _, v := range sticks {
fmt.Println(v.Name, "*:", v.Value)
}
}
}
func (s *Song) GetSticker(name string) string {
if s.stickers != nil {
ret := slice.Filter(s.stickers, func(sticker mpd.Sticker) bool {
return sticker.Name == name
})
if ret != nil && len(ret) > 0 {
first := ret[0]
return first.Value
}
}
if connerror != nil {
return ""
}
found, err := conn.StickerGet(s.Path, name)
if err != nil {
return ""
}
s.stickers = append(s.stickers, *found)
return found.Value
}