-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify-dbus.js
68 lines (61 loc) · 1.81 KB
/
spotify-dbus.js
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
import Gio from "gi://Gio";
const spotifyDbus = `<node>
<interface name="org.mpris.MediaPlayer2.Player">
<property name="PlaybackStatus" type="s" access="read"/>
<property name="Metadata" type="a{sv}" access="read"/>
</interface>
</node>`;
export class SpotifyDBus {
constructor(panelButton) {
this.proxy = null;
this.panelButton = panelButton;
this.initProxy();
}
initProxy() {
try {
this.proxy = Gio.DBusProxy.new_for_bus_sync(
Gio.BusType.SESSION,
Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES,
Gio.DBusInterfaceInfo.new_for_xml(spotifyDbus),
"org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player",
null,
);
this.proxy.connect(
"g-properties-changed",
(proxy, changed, invalidated) => {
const props = changed.deepUnpack();
if ("Metadata" in props) {
this.panelButton.updateLabel();
}
},
);
} catch (e) {
logError(e, "Failed to create DBus proxy");
this.proxy = null;
}
}
getMetadata() {
if (!this.proxy || !this.proxy.Metadata) {
return { title: "", artist: "", url: "", success: false };
}
try {
const metadata = this.proxy.Metadata;
return {
title: metadata["xesam:title"] ? metadata["xesam:title"].unpack() : "",
artist: metadata["xesam:artist"]
? metadata["xesam:artist"].get_strv()[0]
: "",
url: metadata["xesam:url"] ? metadata["xesam:url"].unpack() : "",
success: true,
};
} catch (e) {
logError(e, "Failed to extract metadata");
return { title: "", artist: "", url: "", success: false };
}
}
spotifyIsActive() {
return this.proxy !== null && this.proxy.Metadata !== null;
}
}