-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
386 lines (316 loc) · 9.22 KB
/
types.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package mpris
import (
"errors"
"fmt"
"time"
"github.com/godbus/dbus/v5"
)
const timeFormat = "2006-01-02T15:04-07:00"
// ErrTypeNotParsable indicates, that the given type is not parable.
var ErrTypeNotParsable = errors.New("the given type is not as expected")
// PlaybackStatus represents the playback status.
type PlaybackStatus string
const (
// PlaybackStatusPlaying represents the playback status "Playing".
PlaybackStatusPlaying PlaybackStatus = "Playing"
// PlaybackStatusStopped represents the playback status "Paused".
PlaybackStatusStopped PlaybackStatus = "Stopped"
// PlaybackStatusPaused represents the playback status "Stopped".
PlaybackStatusPaused PlaybackStatus = "Paused"
)
// LoopStatus represents the loop status.
type LoopStatus string
const (
// LoopStatusNone represents the loop status "None".
LoopStatusNone LoopStatus = "None"
// LoopStatusTrack represents the loop status "Track".
LoopStatusTrack LoopStatus = "Track"
// LoopStatusPlaylist represents the loop status "Playlist".
LoopStatusPlaylist LoopStatus = "Playlist"
)
// Metadata represents the mpris-metadata
// see: https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata/
type Metadata map[string]dbus.Variant
// MPRISTrackID returns a unique identity for this track within the context of an MPRIS object (eg: tracklist).
func (md Metadata) MPRISTrackID() (dbus.ObjectPath, error) {
vt := md["mpris:trackid"].Value()
if vt == nil {
return "", nil
}
v, ok := vt.(dbus.ObjectPath)
if !ok {
return "", fmt.Errorf("%T could not be parsed to dbus.ObjectPath: %w", vt, ErrTypeNotParsable)
}
return v, nil
}
// MPRISLength returns the duration of the track in microseconds.
func (md Metadata) MPRISLength() (int64, error) {
vl := md["mpris:length"].Value()
if vl == nil {
return 0, nil
}
v, ok := vl.(int64)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to int64: %w", vl, ErrTypeNotParsable)
}
return v, nil
}
// MPRISArtURL returns the location of an image representing the track or album. Clients should not assume this will
// continue to exist when the media player stops giving out the URL.
func (md Metadata) MPRISArtURL() (string, error) {
va := md["mpris:artUrl"].Value()
if va == nil {
return "", nil
}
v, ok := va.(string)
if !ok {
return "", fmt.Errorf("%T could not be parsed to string: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMAlbum returns the album name.
func (md Metadata) XESAMAlbum() (string, error) {
va := md["xesam:album"].Value()
if va == nil {
return "", nil
}
v, ok := va.(string)
if !ok {
return "", fmt.Errorf("%T could not be parsed to string: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMAlbumArtist returns the album artist(s)
func (md Metadata) XESAMAlbumArtist() ([]string, error) {
va := md["xesam:albumArtist"].Value()
if va == nil {
return nil, nil
}
v, ok := va.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMArtist returns the track artist(s).
func (md Metadata) XESAMArtist() ([]string, error) {
va := md["xesam:artist"].Value()
if va == nil {
return nil, nil
}
v, ok := va.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMAsText returns the track lyrics.
func (md Metadata) XESAMAsText() (string, error) {
vt := md["xesam:asText"].Value()
if vt == nil {
return "", nil
}
v, ok := vt.(string)
if !ok {
return "", fmt.Errorf("%T could not be parsed to string: %w", vt, ErrTypeNotParsable)
}
return v, nil
}
// XESAMAudioBPM returns the speed of the music, in beats per minute.
func (md Metadata) XESAMAudioBPM() (int, error) {
va := md["xesam:audioBPM"].Value()
if va == nil {
return 0, nil
}
v, ok := va.(int)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to int: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMAutoRating returns an automatically-generated rating, based on things such as how often it has been played.
// This should be in the range 0.0 to 1.0.
func (md Metadata) XESAMAutoRating() (float64, error) {
va := md["xesam:autoRating"].Value()
if va == nil {
return 0, nil
}
v, ok := va.(float64)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to float64: %w", va, ErrTypeNotParsable)
}
return v, nil
}
// XESAMComment returns a (list of) freeform comment(s).
func (md Metadata) XESAMComment() ([]string, error) {
vc := md["xesam:comment"].Value()
if vc == nil {
return nil, nil
}
v, ok := vc.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", vc, ErrTypeNotParsable)
}
return v, nil
}
// XESAMComposer returns the composer(s) of the track.
func (md Metadata) XESAMComposer() ([]string, error) {
vc := md["xesam:composer"].Value()
if vc == nil {
return nil, nil
}
v, ok := vc.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", vc, ErrTypeNotParsable)
}
return v, nil
}
// XESAMContentCreated returns when the track was created. Usually only the year component will be useful.
func (md Metadata) XESAMContentCreated() (time.Time, error) {
vc := md["xesam:contentCreated"].Value()
if vc == nil {
return time.Time{}, nil
}
vs, ok := vc.(string)
if !ok {
return time.Time{}, fmt.Errorf("%T could not be parsed to string: %w", vc, ErrTypeNotParsable)
}
t, err := time.Parse(timeFormat, vs)
if err != nil {
return time.Time{}, fmt.Errorf("cound not parse time: %s: %w", err, ErrTypeNotParsable)
}
return t, nil
}
// XESAMDiscNumber returns the disc number on the album that this track is from.
func (md Metadata) XESAMDiscNumber() (int, error) {
vn := md["xesam:discNumber"].Value()
if vn == nil {
return 0, nil
}
v, ok := vn.(int)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to int: %w", vn, ErrTypeNotParsable)
}
return v, nil
}
// XESAMFirstUsed returns when the track was first played.
func (md Metadata) XESAMFirstUsed() (time.Time, error) {
vu := md["xesam:firstUsed"].Value()
if vu == nil {
return time.Time{}, nil
}
vs, ok := vu.(string)
if !ok {
return time.Time{}, fmt.Errorf("%T could not be parsed to string: %w", vu, ErrTypeNotParsable)
}
t, err := time.Parse(timeFormat, vs)
if err != nil {
return time.Time{}, fmt.Errorf("cound not parse time: %s: %w", err, ErrTypeNotParsable)
}
return t, nil
}
// XESAMGenre returns the genre(s) of the track.
func (md Metadata) XESAMGenre() ([]string, error) {
vg := md["xesam:genre"].Value()
if vg == nil {
return nil, nil
}
v, ok := vg.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", vg, ErrTypeNotParsable)
}
return v, nil
}
// XESAMLastUsed returns when the track was last played.
func (md Metadata) XESAMLastUsed() (time.Time, error) {
vu := md["xesam:lastUsed"].Value()
if vu == nil {
return time.Time{}, nil
}
vs, ok := vu.(string)
if !ok {
return time.Time{}, fmt.Errorf("%T could not be parsed to string: %w", vu, ErrTypeNotParsable)
}
t, err := time.Parse(timeFormat, vs)
if err != nil {
return time.Time{}, fmt.Errorf("cound not parse time: %s: %w", err, ErrTypeNotParsable)
}
return t, nil
}
// XESAMLyricist returns the lyricist(s) of the track.
func (md Metadata) XESAMLyricist() ([]string, error) {
vl := md["xesam:lyricist"].Value()
if vl == nil {
return nil, nil
}
v, ok := vl.([]string)
if !ok {
return nil, fmt.Errorf("%T could not be parsed to []string: %w", vl, ErrTypeNotParsable)
}
return v, nil
}
// XESAMTitle returns the track title.
func (md Metadata) XESAMTitle() (string, error) {
vt := md["xesam:title"].Value()
if vt == nil {
return "", nil
}
v, ok := vt.(string)
if !ok {
return "", fmt.Errorf("%T could not be parsed to string: %w", vt, ErrTypeNotParsable)
}
return v, nil
}
// XESAMTrackNumber returns the track number on the album disc.
func (md Metadata) XESAMTrackNumber() (int, error) {
vn := md["xesam:trackNumber"].Value()
if vn == nil {
return 0, nil
}
v, ok := vn.(int)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to int: %w", vn, ErrTypeNotParsable)
}
return v, nil
}
// XESAMURL returns the location of the media file.
func (md Metadata) XESAMURL() (string, error) {
vu := md["xesam:url"].Value()
if vu == nil {
return "", nil
}
v, ok := vu.(string)
if !ok {
return "", fmt.Errorf("%T could not be parsed to string: %w", vu, ErrTypeNotParsable)
}
return v, nil
}
// XESAMUseCount returns hte number of times the track has been played.
func (md Metadata) XESAMUseCount() (int, error) {
vc := md["xesam:useCount"].Value()
if vc == nil {
return 0, nil
}
v, ok := vc.(int)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to int: %w", vc, ErrTypeNotParsable)
}
return v, nil
}
// XESAMUserRating returns a user-specified rating. This should be in the range 0.0 to 1.0.
func (md Metadata) XESAMUserRating() (float64, error) {
vr := md["xesam:userRating"].Value()
if vr == nil {
return 0, nil
}
v, ok := vr.(float64)
if !ok {
return 0, fmt.Errorf("%T could not be parsed to float64: %w", vr, ErrTypeNotParsable)
}
return v, nil
}
// Find returns a generic representation of the requested value when present
func (md Metadata) Find(key string) (dbus.Variant, bool) {
variant, found := md[key]
return variant, found
}