-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
spotify.js
273 lines (243 loc) · 9.66 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
'use strict'
// 3rdparty libraries
const _ = require('lodash')
const chalk = require('chalk')
const debug = require('debug')
const SpotifyWebApi = require('spotify-web-api-node')
const crypto = require('crypto')
const urljoin = require('url-join')
/*
* How to integrate:
* 1. Create app in https://beta.developer.spotify.com/dashboard/applications
* 1a. Set redirect URI as http://whereYouAccessDashboard.com/oauth/spotify
* 2. Update your clientId, clientSecret, redirectURI in Integrations UI
* 3. Authorize your user through UI
*/
class Spotify {
constructor () {
if (require('cluster').isWorker) return
this.collection = 'integrations.spotify'
this.scopes = [ 'user-read-currently-playing', 'user-read-private', 'user-read-email' ]
this.client = null
global.panel.addMenu({category: 'main', name: 'integrations', id: 'integrations'})
this.status()
this.sockets()
setTimeout(() => this.IRefreshToken(), 60000)
setTimeout(() => this.ICurrentSong(), 10000)
}
get currentSong () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne('cache', { key: 'integration_spotify_currentSong' }), 'value', {})))
}
set currentSong (v) {
global.db.engine.update('cache', { key: 'integration_spotify_currentSong' }, { value: v })
}
async getMe () {
try {
const enabled = await this.status({ log: false })
if (enabled && !_.isNil(this.client)) {
let data = await this.client.getMe()
debug('spotify:getMe')('Authorized user: %j', data.body)
return data.body.display_name ? data.body.display_name : data.body.id
} else return null
} catch (e) {
if (e.message !== 'Unauthorized') {
global.log.error('Spotify user get failed')
global.log.error(e.message)
}
return null
}
}
async ICurrentSong () {
try {
let data = await this.client.getMyCurrentPlayingTrack()
let song = {
song: data.body.item.name,
artist: data.body.item.artists[0].name,
is_playing: data.body.is_playing,
is_enabled: await this.status({ log: false })
}
this.currentSong = song
} catch (e) {
this.currentSong = {}
}
setTimeout(() => this.ICurrentSong(), 10000)
}
async IRefreshToken () {
try {
if (!_.isNil(this.client)) {
let data = await this.client.refreshAccessToken()
debug('spotify:IRefreshToken')('New access token: ' + data.body['access_token'])
this.accessToken = data.body['access_token']
}
} catch (e) {
global.log.error('Spotify refresh token failed')
global.log.error(e)
}
setTimeout(() => this.IRefreshToken(), 60000)
}
get enabled () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'enabled' }), 'value', false)))
}
set enabled (v) {
(async () => {
v = !!v // force boolean
await global.db.engine.update(this.collection, { key: 'enabled' }, { value: v })
if (v === false) this.currentSong = {}
this.status()
})()
}
get clientId () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'clientId' }), 'value', null)))
}
set clientId (v) {
this.accessToken = null
this.refreshToken = null
this.client = null;
(async () => {
await global.db.engine.update(this.collection, { key: 'clientId' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v })
await this.status({ log: false })
})()
}
get clientSecret () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'clientSecret' }), 'value', null)))
}
set clientSecret (v) {
this.accessToken = null
this.refreshToken = null
this.client = null;
(async () => {
await global.db.engine.update(this.collection, { key: 'clientSecret' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v })
await this.status({ log: false })
})()
}
get redirectURI () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'redirectURI' }), 'value', null)))
}
set redirectURI (v) {
this.accessToken = null
this.refreshToken = null
this.client = null;
(async () => {
await global.db.engine.update(this.collection, { key: 'redirectURI' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v })
await this.status({ log: false })
})()
}
get code () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'code' }), 'value', null)))
}
set code (v) { global.db.engine.update(this.collection, { key: 'code' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v }) }
get refreshToken () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'refreshToken' }), 'value', null)))
}
set refreshToken (v) {
if (!_.isNil(this.client)) this.client.setRefreshToken(v)
global.db.engine.update(this.collection, { key: 'refreshToken' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v })
}
get accessToken () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'accessToken' }), 'value', null)))
}
set accessToken (v) {
if (!_.isNil(this.client)) this.client.setAccessToken(v)
global.db.engine.update(this.collection, { key: 'accessToken' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v })
}
get state () {
return new Promise(async (resolve, reject) => resolve(_.get(await global.db.engine.findOne(this.collection, { key: 'state' }), 'value', null)))
}
set state (v) { global.db.engine.update(this.collection, { key: 'state' }, { value: _.isNil(v) || v.trim().length === 0 ? null : v }) }
sockets () {
const d = debug('spotify:sockets')
const io = global.panel.io.of('/integrations/spotify')
io.on('connection', (socket) => {
d('Socket /integrations/spotify connected, registering sockets')
socket.on('state', async (callback) => {
callback(null, await this.state)
})
socket.on('code', async (token, callback) => {
this.code = token
setTimeout(() => this.status(), 5000)
callback(null, true)
})
socket.on('getMe', async (callback) => {
callback(null, await this.getMe())
})
socket.on('getCurrentSong', async (callback) => {
callback(null, await this.getCurrentSong())
})
socket.on('settings', async (callback) => {
callback(null, {
clientId: await this.clientId,
clientSecret: await this.clientSecret,
redirectURI: await this.redirectURI,
enabled: await this.status({ log: false })
})
})
socket.on('toggle.enabled', async (cb) => {
let enabled = await this.enabled
this.enabled = !enabled
cb(null, !enabled)
})
socket.on('set.variable', async (data, cb) => {
this[data.key] = data.value
cb(null, data.value)
})
socket.on('authorize', async (cb) => {
cb(null, await this.authorizeURI())
})
socket.on('revoke', async (cb) => {
debug('spotify:revoke')('User access have been revoked')
this.accessToken = null
this.refreshToken = null
this.currentSong = null
cb(null, null)
})
})
}
async status (options) {
options = _.defaults(options, { log: true })
const d = debug('spotify:status')
let [enabled, clientSecret, clientId, redirectURI, code, accessToken, refreshToken] = await Promise.all([this.enabled, this.clientSecret, this.clientId, this.redirectURI, this.code, this.accessToken, this.refreshToken])
d(enabled, clientSecret, clientId, redirectURI)
enabled = !(_.isNil(clientSecret) || _.isNil(clientId) || _.isNil(redirectURI)) && enabled
let color = enabled ? chalk.green : chalk.red
if (options.log) global.log.info(`${color(enabled ? 'ENABLED' : 'DISABLED')}: Spotify Integration`)
if (_.isNil(this.client) && enabled) {
let url = urljoin(redirectURI, 'oauth', 'spotify')
this.client = new SpotifyWebApi({
clientId: clientId,
clientSecret: clientSecret,
redirectUri: url
})
} else if (!enabled) {
this.client = null
}
try {
if (!_.isNil(code) && !_.isNil(this.client) && enabled) {
this.client.authorizationCodeGrant(code)
.then((data) => {
debug('spotify:authorizationCodeGrant')('The token expires in ' + data.body['expires_in'])
debug('spotify:authorizationCodeGrant')('The access token is ' + data.body['access_token'])
debug('spotify:authorizationCodeGrant')('The refresh token is ' + data.body['refresh_token'])
this.accessToken = data.body['access_token']
this.refreshToken = data.body['refresh_token']
})
this.code = null
}
if (!_.isNil(this.client) && enabled && !_.isNil(accessToken)) {
debug('spotify:enabled')('The access token is ' + accessToken)
debug('spotify:enabled')('The refresh token is ' + refreshToken)
this.client.setAccessToken(accessToken)
this.client.setRefreshToken(refreshToken)
}
} catch (e) {
console.error(e)
}
return enabled
}
authorizeURI () {
if (_.isNil(this.client)) return null
let state = crypto.createHash('md5').update(Math.random().toString()).digest('hex')
this.state = state
return this.client.createAuthorizeURL(this.scopes, state)
}
}
module.exports = new Spotify()