-
Notifications
You must be signed in to change notification settings - Fork 15
/
spotify.js
223 lines (202 loc) · 6.84 KB
/
spotify.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
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
process.on('uncaughtException', function (error) {
console.log(error);
if (error.code == 'ECONNRESET') {
spotifyPortOffset++;
console.log('connection failed; trying new port...');
}
});
const https = require('https');
const exec = require('child_process').exec;
const request = require('request');
const SERVER_PORT = 5000;
const UPDATE_INTERVAL = 1000;
const DEFAULT_RETURN_ON = ['login', 'logout', 'play', 'pause', 'error', 'ap'];
let spotifyPortOffset = 0;
const DEFAULT_HTTPS_CONFIG = {
host: '',
port: 4380,
path: '',
headers: {
'Origin': 'https://open.spotify.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
};
let config;
let version;
version = {};
version.running = false;
let csrf;
let oauth;
let albumId;
let coverUrl;
let mainWindow;
let mod;
let trackUri;
function copyConfig() {
let configCopy = JSON.parse(JSON.stringify(DEFAULT_HTTPS_CONFIG));
configCopy.port += (spotifyPortOffset % 10);
return configCopy;
}
module.exports = {};
module.exports.generateLocalHostname = function() {
/*return randomstring.generate({
length: 10,
charset: 'abcdefghijklmnopqrstuvwxyz'
}) + '.spotilocal.com';*/
return '127.0.0.1';
};
module.exports.generateUrl = function(host, port, path) {
return `http${port == 443 ? 's' : ''}://${host}:${port}${path}`;
}
module.exports.getUrl = function(path) {
mod.generateLocalHostname() + '/' + path;
};
module.exports.getJson = function(config, callback) {
var options = {
url: mod.generateUrl(config.host, config.port, config.path),
rejectUnauthorized: false,
headers: config.headers
}
request(options, function (error, response, body) {
//console.log('error:', error);
//console.log('statusCode:', response && response.statusCode);
//console.log('body:', body);
callback(JSON.parse(body));
});
};
module.exports.getStatus = function() {
config = copyConfig();
config.host = mod.generateLocalHostname();
config.path = '/remote/status.json?oauth=' + oauth + '&csrf=' + csrf + '&returnafter=1&returnon=' + DEFAULT_RETURN_ON.join();
mod.getJson(config, function(data) { console.log(data); });
};
module.exports.getCurrentAlbumId = function() {
config = copyConfig();
config.host = mod.generateLocalHostname();
config.path = '/remote/status.json?oauth=' + oauth + '&csrf=' + csrf + '&returnafter=1&returnon=' + DEFAULT_RETURN_ON.join();
mod.getJson(config, function(data) {
//console.log(data);
if (typeof mainWindow !== 'undefined') {
mainWindow.webContents.send('running', data.running);
}
try {
if (data.track.album_resource.uri !== albumId) {
console.log('album updated');
albumId = data.track.album_resource.uri;
track = data.track;
console.log(albumId);
mod.getAlbumCover(albumId);
}
else {
if (typeof mainWindow !== 'undefined') {
mainWindow.webContents.send('coverUrl', coverUrl);
}
}
if (typeof mainWindow !== 'undefined') {
mainWindow.webContents.send('position', (data.playing_position / data.track.length) * 100);
mainWindow.webContents.send('length', data.track.length);
mainWindow.webContents.send('playing', data.playing);
mainWindow.webContents.send('shuffle', data.shuffle);
mainWindow.webContents.send('repeat', data.repeat);
mainWindow.webContents.send('next_enabled', data.next_enabled);
mainWindow.webContents.send('prev_enabled', data.prev_enabled);
mainWindow.webContents.send('track', data.track.track_resource.name);
mainWindow.webContents.send('album', data.track.album_resource.name);
mainWindow.webContents.send('artist', data.track.artist_resource.name);
}
}
catch(ex) {
console.log(ex);
}
});
};
module.exports.seek = function(percent) {
var time = (percent / 100) * track.length;
exec('osascript -e \'tell application "Spotify" to set player position to ' + time + '\'');
};
module.exports.pause = function(pause) {
exec('osascript -e \'tell application "Spotify" to ' + pause ? 'pause' : 'play' + '\'');
};
module.exports.playpause = function() {
exec('osascript -e \'tell application "Spotify" to playpause\'');
};
module.exports.skip = function(forward) {
exec('osascript -e \'tell application "Spotify" to ' + (forward ? 'next' : 'previous') + ' track\'');
};
module.exports.repeat = function(repeating) {
exec('osascript -e \'tell application "Spotify" to set repeating to ' + repeating + '\'');
};
module.exports.shuffle = function(shuffle) {
exec('osascript -e \'tell application "Spotify" to set shuffling to ' + shuffle + '\'');
};
module.exports.getAlbumCover = function(id) {
mod = this;
config = copyConfig();
config.host = 'open.spotify.com';
config.path = '/oembed?url=' + id;
config.port = 443;
mod.getJson(config, function(data) {
console.log(data.thumbnail_url);
coverUrl = data.thumbnail_url;
if (typeof mainWindow !== 'undefined') {
mainWindow.webContents.send('coverUrl', coverUrl);
}
});
};
module.exports.grabTokens = function() {
if (typeof mainWindow !== 'undefined') {
mainWindow.webContents.send('loadingText', 'Connecting to Spotify...');
}
config = copyConfig();
config.host = mod.generateLocalHostname();
config.path = '/simplecsrf/token.json';
mod.getJson(config, function(data) { csrf = data.token; });
config.host = 'open.spotify.com';
config.path = '/token';
config.port = 443;
mod.getJson(config, function(data) { oauth = data.t; });
let updateTrackCover;
let waitForRequest = setInterval(function() {
if (typeof version !== 'undefined' && typeof csrf !== 'undefined' && typeof oauth !== 'undefined') {
clearInterval(waitForRequest);
console.log('done.');
console.log(version);
console.log(csrf);
console.log(oauth);
updateTrackCover = setInterval(mod.getCurrentAlbumId, UPDATE_INTERVAL);
}
else {
console.log('waiting for authentication...');
}
}, 500);
};
module.exports.setWindow = function(window) {
mainWindow = window;
//console.log(mainWindow);
};
module.exports.init = function() {
mod = this;
let waitForSpotify = setInterval(function() {
if (typeof version !== 'undefined' && version.running) {
clearInterval(waitForSpotify);
mod.grabTokens();
}
else {
config = copyConfig();
config.host = mod.generateLocalHostname();
config.path = '/service/version.json?service=remote';
mod.getJson(config, function(data, port) {
if (!('running' in data)) {
data.running = true;
}
version = data;
console.log(version);
console.log('port: ' + port);
config.port = port;
});
console.log('waiting for spotify...');
config.port++;
}
}, 500);
}