mpc-extra/mpd/song.go
2023-08-27 20:07:00 -05:00

85 lines
1.5 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
// @todo refactor as a map so there's no dupes
}
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 {
last := ret[len(ret)-1]
return last.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
}
func (s *Song) AddSticker(name, value string) error {
ret := conn.StickerSet(s.Path, name, value)
if ret == nil {
if s.stickers == nil || len(s.stickers) == 0 {
s.GetStickers()
} else {
s.stickers = append(s.stickers, mpd.Sticker{Name: name, Value: value})
}
}
return ret
}