forked from jtpox/discord-presence-roon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
306 lines (266 loc) · 9.56 KB
/
index.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
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
#!/usr/bin/env node
/** @module main */
const { version, author, homepage } = require('../package.json');
const { ActivityType } = require('discord-api-types/v10');
const RoonApi = require('@roonlabs/node-roon-api');
const RoonApiTransport = require('node-roon-api-transport');
const RoonApiImage = require('node-roon-api-image');
const semver = require('semver');
const { DEFAULT_IMAGE, UPDATE_CHECK, UPDATE_CHECK_URL } = require('./common');
const RoonSettings = require('./settings');
const Discord = require('./discord');
const Discogs = require('./discogs');
const Imgur = require('./imgur');
const { Info, Debug, Warn, Error } = require('./console');
var roon = new RoonApi({
extension_id: 'com.jtpox.discord-roon',
display_name: `Discord Presence Integration`,
display_version: version,
publisher: author.name,
email: author.email,
website: homepage,
core_paired: Paired,
core_unpaired: Unpaired,
});
/** @type {import('./settings').TSettings} */
let Settings;
/**
* The package constructor.
* @function Initiate
*/
function Initiate() {
CheckVersion();
RoonSettings.Initiate(roon);
InitiateIntegrations();
roon.init_services({
required_services: [RoonApiTransport, RoonApiImage],
provided_services: [RoonSettings.Service(InitiateIntegrations)],
});
roon.start_discovery();
setInterval(CheckVersion, UPDATE_CHECK);
}
/**
* Initiate Discord, Discogs and Imgur integrations.
* @function InitiateIntegrations
*/
function InitiateIntegrations() {
Info('Extension: Reloading settings');
Settings = roon.load_config('settings') || RoonSettings.DefaultSettings;
Discord.Initiate(Settings);
Discogs.Initiate(roon, Settings);
Imgur.Initiate(roon, Settings);
}
/**
* Check for latest version
* @function CheckVersion
*/
async function CheckVersion() {
const latest_package_json = await fetch(UPDATE_CHECK_URL);
if(!latest_package_json.ok) return;
const package = await latest_package_json.json();
if(semver.gt(package.version, version)) Warn(`New discord-presence-roon update available! | Running version: ${version} | Latest version: ${package.version}`);
}
/**
* Paired event callback for Roon.
* @function Paired
* @param {object} core The Roon core.
*/
function Paired(core) {
let transport = core.services.RoonApiTransport;
Info('Roon Paired');
let zone_info = {
zone_id: null,
display_name: null,
outputs: [],
state: '',
is_next_allowed: true,
is_previous_allowed: true,
is_pause_allowed: false,
is_play_allowed: true,
is_seek_allowed: true,
queue_items_remaining: 0,
queue_time_remaining: 0,
seek_position: 0,
settings: {},
now_playing: {},
};
transport.subscribe_zones((cmd, data) => {
if(!['Changed', 'Subscribed'].includes(cmd)) return;
switch(Object.keys(data)[0]) {
case 'zones_removed':
Discord.Self().user?.clearActivity();
return;
case 'zones':
case 'zones_changed':
case 'zones_added':
const zones_to_check = Settings.roonZones.split(',');
const available_zones = data.zones || data.zones_changed || data.zones_added;;
const zones = available_zones.filter((zone_data) => zones_to_check.includes(zone_data.display_name));
const priority_zone = zones.sort((a, b) => zones_to_check.indexOf(a.display_name) - zones_to_check.indexOf(b.display_name));
if(priority_zone.length < 1) return;
zone_info = { ...zone_info, ...priority_zone[0] };
break;
case 'zones_seek_changed':
const correct_zone = data.zones_seek_changed.find(el => el.zone_id === zone_info.zone_id);
if(!correct_zone) return;
zone_info.now_playing.seek_position = correct_zone.seek_position;
zone_info = { ...zone_info, ...correct_zone };
break;
}
if(Discord.Self() === undefined) return;
SongChanged(core, zone_info);
});
}
/**
* Unpaired event callback for Roon.
* @param {object} core The Roon core.
*/
function Unpaired(core) {
if(Discord.Self() === undefined) return;
Discord.Self().user?.clearActivity();
}
let PreviousAlbumArt = {
imageKey: DEFAULT_IMAGE,
imageUrl: DEFAULT_IMAGE,
uploading: false,
};
/**
* Populate {@link PreviousAlbumArt} with the album art for a given Roon {@link image_key}, and update the current activity.
* @param {object} core The Roon core.
* @param {string} image_key The Roon image key tied to the given album.
* @param {string} artist The artist.
* @param {string} album The album title.
* @param {string} track The track title.
*/
async function SetAlbumArt(core, image_key, artist, album, track) {
if(!image_key || image_key === PreviousAlbumArt.imageKey || PreviousAlbumArt.uploading) return;
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = DEFAULT_IMAGE;
if(Settings.imgurEnable) {
PreviousAlbumArt.uploading = true;
Imgur.GetAlbumArt(image_key, GetImage(new RoonApiImage(core))).then((art) => {
PreviousAlbumArt.imageUrl = art;
PreviousAlbumArt.uploading = false;
Discord.Self().user?.setActivity({ largeImageKey: art });
}).catch((err) => Warn(`Imgur fetch failed: ${err}`))
.finally(() => PreviousAlbumArt.uploading = false);
} else if(Settings.discogsEnable) {
Discogs.Search(artist, album, track).then((result) => {
if(result.cover_image) {
PreviousAlbumArt.imageUrl = result.cover_image;
Discord.Self().user?.setActivity({ largeImageKey: result.cover_image});
} else {
Info(`No album art found for '${track} - ${artist}' in Discogs`);
}
}).catch((err) => Warn(`Discogs search failed: ${err}`));
}
}
/**
* Formats a string for embedding in an activity.
* @param {string} line The line to format.
* @returns {string | undefined} The formatted line, or `undefined` if {@link line} is empty.
*/
function formatSongLine(line) {
switch (line.length) {
case 0: return undefined;
case 1: line += " "; break;
}
return line.substring(0, 128);
}
/**
* Song changed event which will update the Discord user activity.
* @function SongChanged
* @async
* @param {object} core The Roon core.
* @param {object} data The data provided by Roon zones.
*/
async function SongChanged(core, data) {
switch(data.state) {
case 'playing': break;
case 'paused': Discord.Self().user?.clearActivity();
default: return;
}
const {
image_key,
length,
seek_position,
three_line,
} = data.now_playing;
const {
line1: track,
line2: artist,
line3: album,
} = three_line;
const startTimestamp = Date.now() - (seek_position ?? 0) * 1000;
const endTimestamp = startTimestamp + length * 1000;
const activity = {
type: ActivityType.Listening,
details: formatSongLine(track), // Track title
state: formatSongLine(artist), // Track artist
startTimestamp,
endTimestamp,
instance: false,
smallImageKey: DEFAULT_IMAGE,
smallImageText: `Listening at: ${data.display_name}`,
largeImageKey: PreviousAlbumArt.imageUrl,
largeImageText: (album)? album : `Listening at: ${data.display_name}`,
};
Discord.Self().user?.setActivity(activity);
if(
!image_key
|| image_key === PreviousAlbumArt.imageKey
|| PreviousAlbumArt.uploading
) return;
if(Settings.imgurEnable) {
PreviousAlbumArt.uploading = true;
Imgur.GetAlbumArt(image_key, GetImage(new RoonApiImage(core))).then((art) => {
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = art;
PreviousAlbumArt.uploading = false;
Discord.Self().user?.setActivity({ largeImageKey: art });
}).catch(() => {});
}
if(Settings.discogsEnable && !Settings.imgurEnable) {
Discogs.Search(artist, album, track).then((result) => {
if(result.cover_image) {
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = result.cover_image;
Discord.Self().user?.setActivity({ largeImageKey: result.cover_image });
} else {
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = DEFAULT_IMAGE;
Discord.Self().user?.setActivity({ largeImageKey: DEFAULT_IMAGE });
}
}).catch(() => {});
}
}
/**
* Get buffer of image from Roon's API.
* @function GetImage
* @param {RoonApiImage} api Instance of RoonApiImage (node-roon-api-image)
* @return {GetBuffer}
*/
function GetImage(api) {
/**
* Inner function for GetImage. Uses the RoonAPIImage instance to get the file buffer of the album image.
* @function
* @param {string} image_key Key of the album image.
* @return {Promise<Buffer|string>} File buffer of the album image.
*/
const GetBuffer = function(image_key) {
return new Promise((resolve, reject) => {
api.get_image(image_key, (error, content_type, image) => {
if(error) reject(error);
resolve(Buffer.from(image));
});
});
}
return GetBuffer;
}
if(require.main === module) {
Initiate();
}
module.exports = {
Initiate,
InitiateIntegrations,
}