-
Notifications
You must be signed in to change notification settings - Fork 17
/
Spotify.js
329 lines (296 loc) · 10.7 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
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
const fs = require("fs")
const path = require("path")
const axios = require("axios")
const opn = require("open")
const express = require("express")
const moment = require("moment")
let _Debug = (...args) => { /* do nothing */ }
class Spotify {
constructor(config, debug = false, first = false) {
if (first) this.app = express()
this.version = require('./package.json').version
this.default = {
USERNAME: "",
CLIENT_ID: "",
CLIENT_SECRET: "",
AUTH_DOMAIN: "http://localhost",
AUTH_PATH: "/callback",
AUTH_PORT: "8888",
SCOPE: "user-read-private app-remote-control playlist-read-private streaming user-read-playback-state user-modify-playback-state",
TOKEN: "./token.json",
updateInterval: 1000,
idleInterval: 10000,
}
this.token = null
this.setup = first
this.config = Object.assign({}, this.default, config)
this.logMessage = "[SPOTIFY - " + this.config.USERNAME + "]"
if (debug) _Debug = (...args) => { console.debug(this.logMessage, ...args) }
this.authorizationSeed = 'Basic ' + (
Buffer.from(
this.config.CLIENT_ID + ':' + this.config.CLIENT_SECRET
).toString('base64')
)
this.initFromToken()
_Debug("Spotify version", this.version, " Initialized...")
}
updateSpotify(spotify) {
return new Promise((resolve, reject) => {
this.getCurrentPlayback((code, error, result) => {
if (result === "undefined" || code !== 200) {
reject()
} else {
resolve(result)
}
})
})
}
writeToken(output, cb = null) {
let token = Object.assign({}, output)
token.expires_at = Date.now() + ((token.expires_in - 60) * 1000)
this.token = token
let file = path.resolve(__dirname, this.config.TOKEN)
fs.writeFileSync(file, JSON.stringify(token))
_Debug("Token file was created")
_Debug("Token will expire at: ", moment(this.token.expires_at).format("LLLL"))
if (cb) cb(this.token.access_token)
}
initFromToken() {
let file = path.resolve(__dirname, this.config.TOKEN)
if (fs.existsSync(file)) {
this.token = JSON.parse(fs.readFileSync(file))
}
else {
if (!this.setup) console.log(this.logMessage, "Token file not found!", file)
}
}
isExpired() {
return (Date.now() >= this.token.expires_at);
}
handleRequestError(error) {
if (error.response) {
console.error(this.logMessage, "Invalid response")
console.error(this.logMessage, "Response error code:", error.response.status)
console.error(this.logMessage, "Response error text:", error.response.statusText)
_Debug("Response error data:", error.response.data)
_Debug("Response error headers:", error.response.headers)
} else if (error.request) {
console.error(this.logMessage, "Invalid request")
_Debug("Request:", error.request)
} else {
console.error(this.logMessage, error.message)
}
_Debug(error.toJSON())
_Debug(error.config)
}
refreshToken(cb = null) {
_Debug("Refreshing Token...")
let refresh_token = this.token.refresh_token
let requestData = new URLSearchParams({
grant_type: "refresh_token",
refresh_token: refresh_token
})
let requestConfig = {
url: "https://accounts.spotify.com/api/token",
method: "post",
data: requestData.toString(),
headers: {
Authorization: this.authorizationSeed
},
validateStatus: function (status) {
return status == 200
}
}
axios(requestConfig)
.then((response) => {
_Debug("API Response:", response.status)
response.data.refresh_token = refresh_token
this.writeToken(response.data, cb)
})
.catch((error) => {
console.error(this.logMessage, "Failed to refresh Token.")
this.handleRequestError(error)
})
}
accessToken() {
return (this.token.access_token) ? this.token.access_token : null
}
doRequest(api, type, queryParam, bodyParam, cb) {
if (!this.token) {
console.error(this.logMessage, "Token error!", this.config.TOKEN)
return
}
let requestConfig = {
baseURL: "https://api.spotify.com",
url: api,
method: type,
headers: {
Authorization: "Bearer " + this.token.access_token
}
}
if (typeof queryParam !== "undefined") requestConfig.params = queryParam
if (typeof bodyParam !== "undefined") requestConfig.data = bodyParam
let req = (newAccessToken) => {
if (typeof newAccessToken !== "undefined") requestConfig.headers.Authorization = "Bearer " + newAccessToken
axios(requestConfig)
.then((response) => {
if (api !== "/v1/me/player" && type !== "GET") _Debug("API Response:", response.status, "; Requested:", api)
if (cb) cb(response.status, null, response.data)
})
.catch((error) => {
console.error(this.logMessage, "Failed to request API:", api)
this.handleRequestError(error)
if (cb) {
let errorStatus = ((typeof error.response.status !== "undefined") ? error.response.status : "408")
let errorData = ((typeof error.response.data !== "undefined") ? error.response.data : {})
let retryTimerInSeconds = ((typeof error.response.headers["retry-after"] !== "undefined") ? error.response.headers["retry-after"] : 5)
console.log(this.logMessage, "Will retry in", retryTimerInSeconds, "seconds ...")
setTimeout(() => { cb(errorStatus, error, errorData) }, retryTimerInSeconds * 1000)
}
})
}
if (this.isExpired()) {
this.refreshToken(req)
} else {
req()
}
}
getCurrentPlayback(cb) {
this.doRequest("/v1/me/player", "GET", { additional_types: "episode,track" }, null, cb)
}
getDevices(cb) {
this.doRequest("/v1/me/player/devices", "GET", null, null, cb)
}
play(param, cb) {
this.doRequest("/v1/me/player/play", "PUT", null, param, cb)
}
pause(cb) {
this.doRequest("/v1/me/player/pause", "PUT", null, null, cb)
}
next(cb) {
this.doRequest("/v1/me/player/next", "POST", null, null, (code, error, body) => {
this.doRequest("/v1/me/player/seek", "PUT", { position_ms: 0 }, null, cb)
})
}
previous(cb) {
this.doRequest("/v1/me/player/seek", "PUT", { position_ms: 0 }, null, (code, error, body) => {
this.doRequest("/v1/me/player/previous", "POST", null, null, cb)
})
}
search(param, cb) {
param.limit = 50
this.doRequest("/v1/search", "GET", param, null, cb)
}
transfer(req, cb) {
if (req.device_ids.length > 1) req.device_ids = [req.device_ids[0]]
this.doRequest("/v1/me/player", "PUT", null, req, cb)
}
transferByName(device_name, cb) {
this.getDevices((code, error, result) => {
if (code == 200) {
let devices = result.devices
for (let i = 0; i < devices.length; i++) {
if (devices[i].name == device_name) {
this.transfer({ device_ids: [devices[i].id] }, cb)
return
}
}
} else {
cb(code, error, result)
}
})
}
volume(volume = 50, cb) {
this.doRequest("/v1/me/player/volume", "PUT", { volume_percent: volume }, null, cb)
}
repeat(state, cb) {
this.doRequest("/v1/me/player/repeat", "PUT", { state: state }, null, cb)
}
shuffle(state, cb) {
this.doRequest("/v1/me/player/shuffle", "PUT", { state: state }, null, cb)
}
replay(cb) {
this.doRequest("/v1/me/player/seek", "PUT", { position_ms: 0 }, null, cb)
}
async waitForFileExists(filePath, currentTime = 0, timeout = 0) {
if (fs.existsSync(filePath)) return this.logMessage + " Authentication successful"
if (currentTime >= timeout) throw new Error("Token file was not created (\"" + filePath + "\")")
await new Promise((resolve, reject) => setTimeout(() => resolve(true), 1000))
return this.waitForFileExists(filePath, currentTime + 1000, timeout)
}
async authFlow() {
let self = this
let redirect_uri = this.config.AUTH_DOMAIN + ":" + this.config.AUTH_PORT + this.config.AUTH_PATH
let logMsg = this.logMessage + " AUTH:"
let file = path.resolve(__dirname, this.config.TOKEN)
let waitForFileTimeout = 0
if (!this.config.CLIENT_ID) {
throw new Error(logMsg + " CLIENT_ID doesn't exist.")
}
if (this.token) {
return logMsg + " You already have a token - no need to authenticate."
}
let server = this.app.get(this.config.AUTH_PATH, (req, res) => {
let code = req.query.code || null
let requestData = new URLSearchParams({
code: code,
redirect_uri: redirect_uri,
grant_type: "authorization_code"
})
let requestConfig = {
url: "https://accounts.spotify.com/api/token",
method: "post",
data: requestData.toString(),
headers: {
Authorization: this.authorizationSeed
},
validateStatus: function (status) {
return status == 200
}
}
axios(requestConfig)
.then((response) => {
this.writeToken(response.data)
_Debug("AUTH: stopping express app now")
server.close()
res.send(this.config.TOKEN + " should now be created. Please close the browser to continue.")
})
.catch((error) => {
console.error(logMsg, "Error in request.")
this.handleRequestError(error)
// throw error
})
}).listen(this.config.AUTH_PORT, () => {
_Debug("AUTH: express app started and listening on port", this.config.AUTH_PORT)
})
let urlParams = new URLSearchParams({
response_type: 'code',
client_id: this.config.CLIENT_ID,
scope: this.config.SCOPE,
redirect_uri: redirect_uri,
state: Date.now(),
show_dialog: true
}).toString()
let url = "https://accounts.spotify.com/authorize?" + urlParams
if (this.config.AUTH_DOMAIN == this.default.AUTH_DOMAIN) {
console.log(logMsg, "Opening browser for authentication on Spotify...")
await opn(url, { wait: true })
.catch((error) => {
server.close()
console.error(logMsg, "Failed to open the URL in your default browser.")
console.error(logMsg, "If you are using an environment without UI (docker f.e.) have a look at:\n", "\n\thttps://github.com/skuethe/MMM-Spotify#custom-callback\n")
throw error
})
} else {
console.log(logMsg, "Using custom callback URL. Copy + paste the following URL into your browser:\n\n\t", url, "\n")
console.log(logMsg, "This process will timeout after 5 minutes!")
waitForFileTimeout = 60000 * 5
}
return await this.waitForFileExists(file, 0, waitForFileTimeout)
.catch((error) => {
server.close()
throw error
})
}
}
module.exports = Spotify