-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
executable file
·119 lines (102 loc) · 3.63 KB
/
node_helper.js
File metadata and controls
executable file
·119 lines (102 loc) · 3.63 KB
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
'use strict';
/* Magic Mirror
* Module: SpotifyConnectUI
*
* By Cato Antonsen
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var http = require('http');
module.exports = NodeHelper.create({
log: function(message) {
console.log("[" + this.name + "] " + message);
},
error: function(message) {
console.log("[" + this.name + "] " + message);
},
start: function () {
this.started = false;
this.config = null;
this.spotifyData = null;
},
socketNotificationReceived: function (notification, payload) {
var self = this;
self.log("Notification received: " + notification);
if (notification === 'CONFIG') {
this.config = payload;
this.started = true;
this.spotifyData = null;
self.startSpotifyPolling();
}
},
startSpotifyPolling: function() {
var self = this;
// Get name of Spotify Connect
self.getJson(self.config.Protocol + "://" + self.config.Host + ":" + self.config.Port + self.config.RemoteName)
.then(function(data) {
self.log("Spotify Connect broadcasted as " + data.remoteName);
setInterval(function() {
self.getSpotifyData(data.remoteName);
}, 2500);
})
.catch((err) => self.error(err));
},
getSpotifyData: function(remoteName) {
var self = this;
self.getJson(self.config.Protocol + "://" + self.config.Host + ":" + self.config.Port + self.config.StatusApi)
.then(function(statusData) {
if (statusData.active) {
self.getJson(self.config.Protocol + "://" + self.config.Host + ":" + self.config.Port + self.config.MetadataApi)
.then(function(metaData) {
var currentSpotifyData = { remoteName: remoteName, status: statusData, meta: metaData };
if (self.spotifyData == null ||
currentSpotifyData.status.active != self.spotifyData.status.active ||
currentSpotifyData.status.playing != self.spotifyData.status.playing ||
currentSpotifyData.meta.track_uri != self.spotifyData.meta.track_uri)
{
self.spotifyData = currentSpotifyData;
self.refreshSpotifyData(currentSpotifyData);
}
})
.catch((err) => self.error(err));
} else {
var currentSpotifyData = { remoteName: remoteName, status: statusData};
if (self.spotifyData == null ||
currentSpotifyData.status.active != self.spotifyData.status.active ||
currentSpotifyData.status.playing != self.spotifyData.status.playing)
{
self.spotifyData = currentSpotifyData;
self.refreshSpotifyData(currentSpotifyData);
}
}
})
.catch((err) => self.error(err));
},
refreshSpotifyData: function(data) {
var self = this;
if (data.meta != null) {
var cover_uri = self.config.ImageUrlApi + data.meta.cover_uri;
var options = {method: 'GET', host: self.config.Host, port: self.config.Port, path: cover_uri};
var req = http.get(options, function(res) {
data.meta.cover_uri = res.headers["location"];
self.sendSocketNotification("SpotifyConnectUI", data);
});
} else {
self.sendSocketNotification("SpotifyConnectUI", data);
}
},
// Borrowed parts from https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/
getJson: function(url) {
return new Promise((resolve, reject) => {
const request = http.get(url, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(JSON.parse(body.join(''))));
});
request.on('error', (err) => reject(err))
})
},
});