forked from sticreations/spotigopher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotigopher.go
203 lines (181 loc) · 5.46 KB
/
spotigopher.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
package spotigopher
import (
"errors"
"fmt"
"log"
"os/exec"
"runtime"
"strconv"
"strings"
"github.com/godbus/dbus"
)
type SpotifyClient struct {
}
/*
Info holds all available Information we get over the dbus
*/
type Info struct {
Artist []string
Title string
Album string
Rating string
TrackID string
TrackNumber int32
URL string
ArtworkURL string
Status string
}
var sdbus dbus.Object
/*
NewClient returns an Instance of Spotify Cleint
*/
func NewClient() SpotifyClient {
client := SpotifyClient{}
return client
}
func getSpotifyBus() dbus.BusObject {
con, err := dbus.SessionBus()
if err != nil {
log.Fatalf("Could not get SessionBus: %v", err)
}
//TODO: Test what happens when Spotify is Closed!
spotifyBus := con.Object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
return spotifyBus
}
/*
PlayPause sends a PlayPause Command on the DBus
*/
func (s *SpotifyClient) PlayPause() {
if runtime.GOOS == "darwin" {
sendDarwinAction("playpause")
} else {
sendAction("org.mpris.MediaPlayer2.Player.PlayPause")
}
}
/*
Next sends a Next Command on the DBus
*/
func (s *SpotifyClient) Next() {
if runtime.GOOS == "darwin" {
sendDarwinAction("next")
} else {
sendAction("org.mpris.MediaPlayer2.Player.Next")
}
}
/*
Previous sends a Previous Command on the DBus
*/
func (s *SpotifyClient) Previous() {
if runtime.GOOS == "darwin" {
sendDarwinAction("previous")
} else {
sendAction("org.mpris.MediaPlayer2.Player.Previous")
}
}
/*
Stop sends a Stop Command on the DBus
*/
func (s *SpotifyClient) Stop() {
if runtime.GOOS == "darwin" {
sendDarwinAction("stop")
} else {
sendAction("org.mpris.MediaPlayer2.Player.Stop")
}
}
/*
GetInfo returns all Spotify related Information, when Spotify is running
*/
func (s *SpotifyClient) GetInfo() (Info, error) {
info := Info{}
if runtime.GOOS == "linux" {
spotifyBus := getSpotifyBus()
props, err := spotifyBus.GetProperty("org.mpris.MediaPlayer2.Player.Metadata")
if err != nil {
return Info{}, errors.New("Could not get any Info from Spotify. Are you sure Spotify is running?")
}
songData := props.Value().(map[string]dbus.Variant)
info.TrackID = songData["mpris:trackid"].Value().(string)
info.Artist = songData["xesam:artist"].Value().([]string)
info.Title = songData["xesam:title"].Value().(string)
info.Album = songData["xesam:album"].Value().(string)
info.TrackNumber = songData["xesam:trackNumber"].Value().(int32)
info.URL = songData["xesam:url"].Value().(string)
info.ArtworkURL = songData["mpris:artUrl"].Value().(string)
status, err := spotifyBus.GetProperty("org.mpris.MediaPlayer2.Player.PlaybackStatus")
if err != nil {
log.Fatalf("Could not get Playback Status : %v", err)
}
info.Status = status.Value().(string)
} else if runtime.GOOS == "darwin" {
info.TrackID = getDarwinInfo("trackid")
info.Artist = strings.Split(getDarwinInfo("artist"), ",")
info.Album = getDarwinInfo("album")
info.Title = getDarwinInfo("track")
tmpTrackNumber, err := strconv.ParseInt(getDarwinInfo("tracknumber"), 0, 32)
if err != nil {
log.Fatalf("Could not get track number: %s", err)
}
info.TrackNumber = int32(tmpTrackNumber)
info.URL = getDarwinInfo("url")
info.ArtworkURL = getDarwinInfo("artworkurl")
info.Status = getDarwinInfo("status")
}
return info, nil
}
func getDarwinInfo(infoType string) string {
args := []string{`-etell application "Spotify" to name of current track as string`}
switch infoType {
case "trackid":
args = []string{`-etell application "Spotify" to id of current track as string`}
case "artist":
args = []string{`-etell application "Spotify" to artist of current track as string`}
case "title":
args = []string{`-etell application "Spotify" to name of current track as string`}
case "album":
args = []string{`-etell application "Spotify" to album of current track as string`}
case "tracknumber":
args = []string{`-etell application "Spotify" to track number of current track as string`}
case "url":
args = []string{`-etell application "Spotify" to spotify url of current track as string`}
case "artworkurl":
args = []string{`-etell application "Spotify" to artwork url of current track as string`}
case "status":
args = []string{`-etell application "Spotify" to player state as string`}
default:
args = []string{`-etell application "Spotify" to name of current track as string`}
}
info, err := exec.Command("osascript", args...).Output()
if err != nil {
// There is likely no current track yet, inform the user
return fmt.Sprintf("Could not get info for: %s", infoType)
}
return strings.Trim(string(info), "\n")
}
func sendDarwinAction(action string) {
args := []string{`-etell application "Spotify" to pause`}
switch action {
case "play":
args = []string{`-etell application "Spotify" to play`}
case "pause":
args = []string{`-etell application "Spotify" to pause`}
case "stop":
args = []string{`-etell application "Spotify" to pause`}
case "playpause":
args = []string{`-etell application "Spotify" to playpause`}
case "next":
args = []string{`-etell application "Spotify" to next track`}
case "previous":
args = []string{`-etell application "Spotify" to previous track`}
default:
args = []string{`-etell application "Spotify" to pause`}
}
err := exec.Command("osascript", args...).Run()
if err != nil {
log.Fatalf("Could not completed action: %s", action)
}
}
func sendAction(method string) error {
sdbus := getSpotifyBus()
call := sdbus.Call(method, 0)
return call.Err
}