forked from jagvirdehal/spotify-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
339 lines (283 loc) · 10.2 KB
/
app.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
// Libraries
const fs = require('fs');
const express = require('express'); // Express web server framework
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 8888;
// Express addons
const cors = require('cors');
const querystring = require('querystring');
const cookieParser = require('cookie-parser');
// Used for generating room URLs
const randomWords = require('random-words');
// Get lobby and usermap from lobby.js
const [lobby, userMap] = require('./lobby.js');
// Module for making POST and GET requests to Spotify
const request = require('request'); // "Request" library
// Client id and secret
let client_id, client_secret;
try {
client_id = fs.readFileSync('client_id.txt', { encoding: "utf-8", flag: "r" }).trim(); // Your client id
client_secret = fs.readFileSync('client_secret.txt', { encoding: "utf-8", flag: "r" }).trim(); // Your secret
} catch (err) {
client_id = process.env.CLIENT_ID;
client_secret = process.env.CLIENT_SECRET;
}
const redirect_uri = 'https://4195235527ee.ngrok.io/callback'; // Your redirect uri
// Generate string function
const generateRandomString = (length) => {
let text = '';
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
// Error response function
const errorRes = (res, error) => {
res.redirect('/login#' +
querystring.stringify({
error: error
})
);
}
// Cookie keys
const loggedInKey = 'logged_in';
const accessTokenKey = 'access_token';
const refreshTokenKey = 'refresh_token';
const expiresInKey = 'expires_in';
const issueTimeKey = 'issue_time';
const stateKey = 'spotify_auth_state';
// Express App settings
app.use(express.static(__dirname + '/public'))
.use(cors())
.use(cookieParser());
let TOTAL_SOCKETS = new Set();
const cleanAllRooms = () => {
for (let room of lobby.rooms.keys())
lobby.clean(room);
};
// Connection and broadcasting logic
io.on('connection', (socket) => {
TOTAL_SOCKETS.add(socket.id);
socket.on('create-room', (user) => {
let room;
let id = user.id;
// Generate roomnames until unique room is found
do {
room = randomWords({exactly: 4, join: '-', maxLength: 6});
} while (room in lobby.rooms);
// Add/update user in userMap, and create lobby room
userMap.add(user);
lobby.create(room, id);
// Leave all rooms
socket.rooms.forEach(roomname => {
socket.leave(roomname);
});
// Attach socket to room and user
socket.join(room);
socket.join(id);
// Redirect
socket.emit('redirect', {path: `/${room}`});
});
socket.on('join-room', (user) => {
let room = user.room;
let userId = user.id;
let host, hostId;
let users, uids;
// Join user to the room
if (lobby.join(room, userId)) { // True if room does not exist
socket.emit('force-create');
return;
}
// Update user
userMap.add(user);
// Leave all rooms
socket.rooms.forEach(roomname => {
socket.leave(roomname);
});
// Leave all lobby rooms
let sockets = userMap.getSockets(userId);
if (sockets) {
for (let socketId of sockets) {
io.sockets.sockets.get(socketId).rooms.forEach(roomname => {
if (userMap.getRoom(userId) !== roomname)
lobby.leave(roomname, userId);
});
}
}
// Redirect all user sockets to the latest room
io.to(userId).emit('redirect', {path: `/${user.room}`});
// Attach socket to room and user
socket.join(room);
socket.join(userId);
userMap.addSocket(userId, socket.id);
// Get users
uids = lobby.getListeners(room);
users = uids.map((user, index) => {
return userMap.get(user);
});
hostId = lobby.getHost(room);
host = userMap.get(hostId);
// If there are too many lobbies, clean
if (lobby.rooms.size > userMap.users.size + 5)
cleanAllRooms();
io.to(room).emit('message', `${user.name} has entered the room ${room}`);
io.to(room).emit('message', `${uids} are in the room ${room}`);
io.to(room).emit('get-users', users);
io.to(room).emit('get-host', host);
io.emit('message', `${JSON.stringify(Object.keys(lobby))}`);
});
socket.on('update-music', (user, songData) => {
if (user.id !== lobby.getHost(user.room) || lobby.rooms.get(user.room) === undefined)
return;
for (let member of lobby.rooms.get(user.room)) {
if (member === user.id)
continue;
if (userMap.getSockets(member) !== undefined) {
for (let socketId of userMap.getSockets(member)) {
io.sockets.sockets.get(socketId).emit('update-music', songData);
break;
}
}
}
});
socket.on('disconnecting', () => {
TOTAL_SOCKETS.delete(socket.id);
socket.rooms.forEach(roomname => {
let userId = roomname;
let socketList = userMap.getSockets(userId);
if (!socketList) {
return;
}
for (let socketId of socketList) {
if (socketId === socket.id || !TOTAL_SOCKETS.has(socketId)) {
userMap.removeSocket(userId, socketId);
}
}
socketList = userMap.getSockets(userId);
if (socketList.size === 0) {
// Remove user from server
userMap.remove(userId);
// Remove user from all rooms
for (let room of lobby.rooms.keys()) {
lobby.leave(room, userId)
}
}
});
});
});
// setInterval(() => {
// console.log("Update:");
// console.log(TOTAL_SOCKETS);
// console.log(userMap.sockets);
// console.log(lobby.rooms);
// }, 1000);
// Login page render
app.get('/login', (req, res) => {
res.sendFile(__dirname + '/public/login.html');
});
// Login method call
app.get('/loginCall', (req, res) => {
const state = generateRandomString(16);
res.cookie(stateKey, state);
// your application requests authorization
const scope = 'user-read-private' +
' user-read-email' +
' user-read-recently-played' +
' user-read-playback-state' +
' app-remote-control' +
' user-modify-playback-state' +
' user-read-currently-playing' +
' user-read-playback-position';
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
})
);
});
// Callback from the login request
app.get('/callback', (req, res) => {
const code = req.query.code || null;
const state = req.query.state || null;
const storedState = req.cookies ? req.cookies[stateKey] : null;
if (state === null || state !== storedState) {
errorRes(res, 'state_mismatch');
} else {
res.clearCookie(stateKey);
const authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
json: true
};
request.post(authOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
let access_token = body.access_token,
refresh_token = body.refresh_token;
expires_in = body.expires_in;
res.cookie(loggedInKey, true);
res.cookie(accessTokenKey, access_token);
res.cookie(refreshTokenKey, refresh_token);
res.cookie(expiresInKey, expires_in);
res.cookie(issueTimeKey, Math.floor(new Date().getTime() / 1000));
const options = {
url: 'https://api.spotify.com/v1/me',
headers: { 'Authorization': 'Bearer ' + access_token },
json: true
};
// use the access token to access the Spotify Web API
request.get(options, (error, response, body) => {
console.log(body);
});
// we can also pass the token to the browser to make requests from there
res.redirect('/');
} else {
errorRes(res, invalid_token);
}
});
}
});
app.get('/refresh_token', (req, res) => {
// requesting access token from refresh token
let refresh_token = req.query.refresh_token;
let authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) },
form: {
grant_type: 'refresh_token',
refresh_token: refresh_token
},
json: true
};
request.post(authOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
let access_token = body.access_token,
expires_in = body.expires_in;
res.cookie(accessTokenKey, access_token);
res.cookie(expiresInKey, expires_in);
res.redirect('/');
// res.send({
// 'access_token': access_token
// });
}
});
});
// Catch-all route
app.all('*', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
})
http.listen(PORT, () => {
console.log(`Listening on ${PORT}`);
});