forked from denbeigh2000/goi3bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.go
93 lines (74 loc) · 1.46 KB
/
spotify.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
package memory
import (
i3 "github.com/denbeigh2000/goi3bar"
"github.com/godbus/dbus"
"fmt"
)
var bus *dbus.BusObject
func newBus() *dbus.BusObject {
conn, err := dbus.SessionBus()
// couldnt connect to session bus
if err != nil {
panic(err)
}
obj := conn.Object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
return &obj
}
type Song struct {
Artist string
Title string
}
func (s *Song) Update() {
if bus == nil {
bus = newBus()
}
b := *bus
meta, err := b.GetProperty("org.mpris.MediaPlayer2.Player.Metadata")
if err != nil {
s.Artist = ""
s.Title = ""
return
}
songData, ok := meta.Value().(map[string]dbus.Variant)
if !ok {
return
}
if artist, ok := songData["xesam:artist"]; ok {
tmp := artist.Value().([]string)
if len(tmp) < 1 {
return
}
s.Artist = artist.Value().([]string)[0]
}
if title, ok := songData["xesam:title"]; ok {
s.Title = title.Value().(string)
}
}
func (s *Song) Get() string {
return fmt.Sprintf("%v - %v", s.Artist, s.Title)
}
type Spotify struct {
Name string
}
func (s Spotify) Generate() ([]i3.Output, error) {
song := Song{}
song.Update()
current := song.Get()
output := current
var color string
switch current {
case " - ":
color = i3.DefaultColors.Crit
output = "No music"
default:
color = i3.DefaultColors.OK
}
out := make([]i3.Output, 1)
out[0] = i3.Output{
Name: s.Name,
FullText: output,
Color: color,
Separator: true,
}
return out, nil
}