-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_helper.js
354 lines (340 loc) · 13.4 KB
/
node_helper.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
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
/*
* MMM-OnSpotify
* MIT license
*
* By Fabrizio <3 (Fabrizz) | https://github.com/Fabrizz/MMM-OnSpotify
*/
"use strict";
const NodeHelper = require("node_helper");
const SpotifyFetcher = require("./utils/SpotifyFetcher");
module.exports = NodeHelper.create({
start: function () {
console.log(
"[\x1b[35mMMM-OnSpotify\x1b[0m] by Fabrizz >> Node helper loaded.",
);
this.fetcher = undefined;
// Helps keping track of when the player becomes empty
this.lastPlayerStatus = undefined;
// Helps keping track of when the song actually changes
this.lastMediaUri = undefined;
// Helps keping track of when the current device target is changed
this.lastDeviceName = undefined;
// The times that an available (but empty) player has been returned by the api
this.isPlayerInTransit = 0;
// Configuration sent to the helper
this.preferences = undefined;
// Use a identifier to filter socket-io retries
this.appendableId = undefined;
this.serversideId = Date.now().toString(16);
// Canvas url and to which item it corresponds to
this.savedCanvasUrl = false;
this.savedCanvasUri = "";
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "SET_CREDENTIALS_REFRESH":
// Blocks re-requests caused by socket.io implementation
if (payload.backendExpectId === this.appendableId) break;
this.fetcher = new SpotifyFetcher(payload);
this.fetchSpotifyData("PLAYER");
this.preferences = payload.preferences;
this.appendableId = payload.backendExpectId;
if (this.appendableId !== "ABC")
console.log(
"[\x1b[35mMMM-OnSpotify\x1b[0m] Session identifier: >> \x1b[44m\x1b[37m %s \x1b[0m",
`${this.appendableId}`,
);
break;
case "REFRESH_PLAYER":
if (this.isCorrectIdOrRefresh(payload)) this.fetchSpotifyData("PLAYER");
break;
case "REFRESH_USER":
if (this.isCorrectIdOrRefresh(payload)) this.fetchSpotifyData("USER");
break;
case "REFRESH_AFFINITY":
if (this.isCorrectIdOrRefresh(payload))
this.fetchSpotifyData("AFFINITY");
break;
/* WIP | Non implemented */
case "REFRESH_QUEUE":
this.fetchSpotifyData("QUEUE");
break;
case "REFRESH_RECENT":
this.fetchSpotifyData("RECENT");
break;
}
},
fetchSpotifyData: async function (type) {
try {
let data = await this.fetcher.getData(type);
if (data instanceof Error)
return this.sendSocketNotification(
"CONNECTION_ERRONED",
JSON.stringify(data),
);
switch (type) {
case "PLAYER":
// CASE S1 The data is OK and the target is a filtered device
if (
data &&
data.device &&
data.device.name &&
!this.isAllowedDevice(data.device.name)
) {
this.sendSocketNotification("PLAYER_DATA", {
statusIsPlayerEmpty: true,
statusIsNewSong: false,
statusIsChangeToEmptyPlayer: this.lastPlayerStatus,
statusIsChangeToMediaPlayer: false,
statusPlayerUpdating: false,
statusIsDeviceChange: false,
notAllowedDevice: data.device.name,
});
this.lastMediaUri = "empty";
this.lastPlayerStatus = false;
this.lastPlayerStatusCount = 0;
this.lastDeviceName = "unknown";
break;
}
// CASE S2 The data is OK and the target is in a private session
if (data && data.device && data.device.is_private_session) {
let payload = {
/* Player data */
playerIsPlaying: true,
/* Device data (Some are not used yet) */
deviceName: data.device.name,
deviceType: data.device.type,
deviceVolume: data.device.volume_percent,
deviceIsPrivate: data.device.is_private_session,
deviceId: data.device.id,
/* Special status sync */
statusIsPlayerEmpty: false,
statusIsNewSong:
this.lastMediaUri !== "privatesession" ? true : false,
statusIsChangeToEmptyPlayer: false,
statusIsChangeToMediaPlayer: this.lastPlayerStatus ? false : true,
statusPlayerUpdating: false,
statusIsDeviceChange:
this.lastDeviceName !== data.device.name ? true : false,
notAllowedDevice: false,
};
this.sendSocketNotification("PLAYER_DATA", payload);
this.lastMediaUri = "privatesession";
this.lastDeviceName = data.device.name;
this.lastPlayerStatus = true;
this.lastPlayerStatusCount = 0;
break;
}
if (data && data.item) {
// CASE 1 The data is OK and there is an ITEM in the player
const isTrack =
data.currently_playing_type === "track" ? true : false;
const itemImages =
this.processImages(
(isTrack ? data.item.album.images : data.item.show.images) || []);
// Add a canvas object if enabled
if (isTrack && this.preferences.useCanvas && (this.lastMediaUri !== data.item.uri))
this.statelessGetCanvas(data.item.uri, itemImages)
let payload = {
/* Player data */
playerIsPlaying: data.is_playing,
playerProgress: data.progress_ms,
playerMediaType: data.currently_playing_type,
playerShuffleState: data.shuffle_state,
playerRepeatState: data.repeat_state,
/* Item generics */
itemId: data.item.id,
itemUri: data.item.uri,
itemName: data.item.name,
itemDuration: data.item.duration_ms,
itemImages,
/* Item specifics (Some are not used yet) */
itemAlbum: isTrack ? data.item.album.name : null,
itemPublisher: isTrack ? null : data.item.show.publisher,
itemShowName: isTrack ? null : data.item.show.name,
itemShowDescription: isTrack ? null : data.item.show.description,
itemArtist: isTrack ? data.item.artists[0].name : null,
itemArtists: this.processArtists(
(isTrack ? data.item.artists : null) || [],
),
/* Device data (Some are not used yet) */
deviceName: data.device.name,
deviceType: data.device.type,
deviceVolume: data.device.volume_percent,
deviceIsPrivate: data.device.is_private_session,
deviceId: data.device.id,
/* Special canvas sync */
canvas: data.item.uri === this.savedCanvasUri ? this.savedCanvasUrl : false,
/* Special status sync */
statusIsPlayerEmpty: false,
statusIsNewSong:
this.lastMediaUri !== data.item.uri ? true : false,
statusIsChangeToEmptyPlayer: false,
statusIsChangeToMediaPlayer: this.lastPlayerStatus ? false : true,
statusPlayerUpdating: false,
statusIsDeviceChange:
this.lastDeviceName !== data.device.name ? true : false,
notAllowedDevice: false,
};
this.sendSocketNotification("PLAYER_DATA", payload);
this.lastMediaUri = data.item.uri;
this.lastPlayerStatus = true;
this.lastPlayerStatusCount = 0;
this.lastDeviceName = data.device.name;
} else if (data && !data.item) {
// CASE 2 The player in in transit (data OK, no ITEM)
if (this.lastPlayerStatusCount <= 3) {
// CASE 2A The player is available but there are no items in it. Wait 3 calls
// This prevents an empty module when the player is loading or the playlist updating
this.sendSocketNotification("PLAYER_DATA", {
statusIsPlayerEmpty: true,
statusIsNewSong: false,
statusIsChangeToEmptyPlayer: this.lastPlayerStatus,
statusIsChangeToMediaPlayer: false,
statusPlayerUpdating: true,
statusIsDeviceChange: false,
notAllowedDevice: false,
});
this.lastPlayerStatusCount = this.lastPlayerStatusCount + 1;
this.lastPlayerStatus = true;
} else {
this.sendSocketNotification("PLAYER_DATA", {
// CASE 2B The player is empty but still available for more than expected. Mark it as empty
statusIsPlayerEmpty: true,
statusIsNewSong: false,
statusIsChangeToEmptyPlayer: this.lastPlayerStatus,
statusIsChangeToMediaPlayer: false,
statusPlayerUpdating: false,
statusIsDeviceChange: false,
notAllowedDevice: false,
});
this.lastMediaUri = "empty";
this.lastPlayerStatus = false;
this.lastDeviceName = "unknown";
}
// CASE 3 There is nothing playing and there is not a player available
} else {
this.sendSocketNotification("PLAYER_DATA", {
statusIsPlayerEmpty: true,
statusIsNewSong: false,
statusIsChangeToEmptyPlayer: this.lastPlayerStatus,
statusIsChangeToMediaPlayer: false,
statusPlayerUpdating: false,
statusIsDeviceChange: false,
notAllowedDevice: false,
});
this.lastMediaUri = "empty";
this.lastPlayerStatus = false;
this.lastPlayerStatusCount = 0;
this.lastDeviceName = "unknown";
}
break;
case "USER":
if (data) {
let payload = {
country: data.country,
name: data.display_name,
id: data.id,
image: data.images
? data.images[0]
? data.images[0].url
: null
: null,
product: data.product,
filterEnabled: data.explicit_content.filter_enabled,
filterLocked: data.explicit_content.filter_locked,
type: data.type,
};
this.sendSocketNotification("USER_DATA", payload);
}
break;
case "AFFINITY":
if (data) {
let payload = [];
data.items.forEach((item) =>
payload.push({
name: item.name,
image: (item.type === "track" ? item.album.images : item.images)
// Here we use medium sized images || Maybe later even the icon ones, as its just a background
.filter((image) =>
image.width >= 240 && image.width <= 360
? image.width >= 240 && image.width <= 360
: // Fallback to any of the available image sizes.
image,
)[0].url,
}),
);
this.sendSocketNotification("AFFINITY_DATA", payload);
}
break;
// Future update
case "QUEUE":
break;
}
} catch (error) {
console.warn(
"[\x1b[35mMMM-OnSpotify\x1b[0m] >> \x1b[41m\x1b[37m %s \x1b[0m ",
"Error fetching data (OOB)",
error,
);
}
},
async statelessGetCanvas(uri, itemImages) {
try {
// use then to prevent context issue
this.fetcher.getCanvas(uri).then(canvas => {
// console.log("[CANVAS DATA]", JSON.stringify(canvas))
if (canvas.canvasesList.length == 1 && canvas.canvasesList[0].canvasUrl.endsWith('.mp4')) {
const item = canvas.canvasesList[0];
this.savedCanvasUrl = item.canvasUrl;
this.savedCanvasUri = item.trackUri;
this.sendSocketNotification("UPDATE_CANVAS", {
itemUri: item.trackUri,
url: item.canvasUrl,
itemImages: itemImages
});
}
});
} catch (error) {
console.warn(
"[\x1b[35mMMM-OnSpotify\x1b[0m] >> \x1b[41m\x1b[37m %s \x1b[0m ",
"Error fetching cover data (OOB)",
error,
);
}
},
isCorrectIdOrRefresh(rcvd) {
if (rcvd !== this.appendableId) {
if (typeof this.appendableId === "undefined") {
// Means that the backend was restarted and the frontend was maintained
this.sendSocketNotification("REQUEST_REAUTH", this.serversideId);
}
return false;
}
return true;
},
isAllowedDevice: function (currentDevice) {
if (
!this.preferences.deviceFilter ||
this.preferences.deviceFilter.length < 1
)
return true;
return this.preferences.deviceFilterExclude
? !this.preferences.deviceFilter.includes(currentDevice)
: this.preferences.deviceFilter.includes(currentDevice);
},
processArtists: (artists) => artists.map((artist) => artist.name).join(", "),
processImages: (images) => {
return {
large: images.filter(
(image) => image.width >= 580 && image.width <= 720,
)[0].url,
medium: images.filter(
(image) => image.width >= 240 && image.width <= 360,
)[0].url,
thumb: images.filter(
(image) => image.width >= 24 && image.width <= 200,
)[0].url,
};
},
});