-
Notifications
You must be signed in to change notification settings - Fork 17
/
tracklist.go
80 lines (64 loc) · 2.07 KB
/
tracklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package mpris
import (
"fmt"
"time"
"github.com/godbus/dbus/v5"
"github.com/natsukagami/mpd-mpris/mpd"
)
// TrackIDFormat is the formatter string for a track ID.
const TrackIDFormat = "/org/mpd/Tracks/%d"
// This file implements a struct that satisfies the `org.mpris.MediaPlayer2.TrackList` interface.
// TrackList is a DBus object satisfying the `org.mpris.MediaPlayer2.TrackList` interface.
// https://specifications.freedesktop.org/mpris-spec/latest/TrackList_Interface.html
type TrackList struct {
*Instance
}
// URI is an unique resource identifier.
// https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Simple-Type:Uri
type URI string
// MetadataMap is a mapping from metadata attribute names to values.
// https://specifications.freedesktop.org/mpris-spec/latest/Track_List_Interface.html#Mapping:Metadata_Map
type MetadataMap map[string]interface{}
func (m *MetadataMap) nonEmptyString(field, value string) {
if value != "" {
(*m)[field] = value
}
}
func (m *MetadataMap) nonEmptySlice(field string, values []string) {
toAdd := []string{}
for _, v := range values {
if v != "" {
toAdd = append(toAdd, v)
}
}
if len(toAdd) > 0 {
(*m)[field] = toAdd
}
}
// MapFromSong returns a MetadataMap from the Song struct in mpd.
func MapFromSong(s mpd.Song) MetadataMap {
if s.ID == -1 {
// No song
return MetadataMap{
"mpris:trackid": dbus.ObjectPath("/org/mpris/MediaPlayer2/TrackList/NoTrack"),
}
}
m := &MetadataMap{
"mpris:trackid": dbus.ObjectPath(fmt.Sprintf(TrackIDFormat, s.ID)),
"mpris:length": s.Duration / time.Microsecond,
}
m.nonEmptyString("xesam:album", s.Album)
m.nonEmptyString("xesam:title", s.Title)
m.nonEmptyString("xesam:url", s.Filepath)
m.nonEmptyString("xesam:contentCreated", s.Date)
m.nonEmptySlice("xesam:albumArtist", []string{s.AlbumArtist})
m.nonEmptySlice("xesam:artist", []string{s.Artist})
m.nonEmptySlice("xesam:genre", []string{s.Genre})
if artURI, ok := s.AlbumArtURI(); ok {
(*m)["mpris:artUrl"] = artURI
}
if s.Track != 0 {
(*m)["xesam:trackNumber"] = s.Track
}
return *m
}