Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
362 lines (321 loc) · 8.8 KB
/
Copy pathapp.js
File metadata and controls
362 lines (321 loc) · 8.8 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//Depedency variables
const express = require('express')
var cors = require('cors');
var querystring = require('querystring');
var cookieParser = require('cookie-parser');
var fs= require('fs');
var bodyParser = require("body-parser");
var SpotifyWebApi = require('spotify-web-api-node');
//Scope Definition for Spotify WebAPI calls
const scopes = [
'ugc-image-upload',
'user-read-playback-state',
'user-modify-playback-state',
'user-read-currently-playing',
'streaming',
'app-remote-control',
'user-read-email',
'user-read-private',
'playlist-read-collaborative',
'playlist-modify-public',
'playlist-read-private',
'playlist-modify-private',
'user-library-modify',
'user-library-read',
'user-top-read',
'user-read-playback-position',
'user-read-recently-played',
'user-follow-read',
'user-follow-modify'
];
const port = process.env.PORT || '5000';
//Initialising the SpotifyAPI node package
var spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: 'https://queue-player.herokuapp.com/callback'
});
var access_token;
//Initialising the express server
const app = express();
app.use(bodyParser.json());
const { ppid } = require('process');
app.use(cors())
.use(cookieParser());
//Authorization flow for the Spotify API
app.get('/', (req, res) => {
res.redirect(spotifyApi.createAuthorizeURL(scopes));
});
app.get('/callback', (req, res) => {
const error = req.query.error;
const code = req.query.code;
const state = req.query.state;
if (error) {
console.error('Callback Error:', error);
res.send(`Callback Error: ${error}`);
return;
}
spotifyApi
.authorizationCodeGrant(code)
.then(data => {
access_token = data.body['access_token'];
const refresh_token = data.body['refresh_token'];
const expires_in = data.body['expires_in'];
spotifyApi.setAccessToken(access_token);
spotifyApi.setRefreshToken(refresh_token);
// console.log('access_token:', access_token);
// console.log('refresh_token:', refresh_token);
setInterval(async () => {
const data = await spotifyApi.refreshAccessToken();
const access_token = data.body['access_token'];
console.log('The access token has been refreshed!');
console.log('access_token:', access_token);
spotifyApi.setAccessToken(access_token);
}, expires_in / 2 * 1000);
})
.catch(error => {
console.error('Error getting Tokens:', error);
res.send(`Error getting Tokens: ${error}`);
});
res.send("Connected with Spotify");
});
app.get('/testing', (req, res)=>{
res.send({msg:"Testing Successful"});
})
//Get the Track to play as requested by the client
app.post('/getTrackToPlay', (req, res) => {
var trackInfos = readDatabase();
var bpmData=getDatafromBPM(trackInfos, req.body.bpm);
var songAddition = processDatabase(bpmData, req.body.userID);
queue=songAddition;
var q=queue.shift();
var cr=getColorSequence(queue);
// userControl(req.body.userID);
res.send({"queue": queue, "song":q, "color": cr});
console.log(queue);
})
// Get the track into the queue
app.post('/getTrackToQueue',(req, res)=>{
if(!userCheck(req.body.userID))
{
var trackInfos = readDatabase();
var bpmData=getDatafromBPM(trackInfos, req.body.bpm);
var songAddition = processDatabase(bpmData, req.body.userID);
queue.splice(req.body.offset,queue.length-req.body.offset);
queue=queue.concat(songAddition);
var cr=getColorSequence(queue);
// userControl(req.body.userID);
res.send({"queue": queue, "color": cr});
}
else
{
res.send({"queue":queue, color:cr});
}
})
// Get the track from the queue to automatically continue playing
app.get('/continuePlaying', (req, res)=>{
user1Added=false;
user2Added=false;
user3Added=false;
user4Added=false;
var q=queue.shift();
var cr=getColorSequence(queue);
res.send({"queue": queue, "song":q, "color": cr});
})
//Play the song , finds the active spotify player if device id not specified
app.post('/playback',async (req, res) => {
console.log("Kya kr rha hai")
res.setHeader('Content-Type', 'application/json');
const play= await spotifyApi.play({
"uris": req.body.song,
"position_ms":0,
}).then(function() {
console.log('Playback started');
res.send();
}, function(err) {
//if the user making the request is non-premium, a 403 FORBIDDEN response code will be returned
console.log('Something went wrong!', err);
});
});
// Gets the state of the active player to check if song has ended or playing
app.get('/getState', async (req, res)=> {
const state=await spotifyApi.getMyCurrentPlaybackState()
.then(function(data) {
if(data.body.is_playing && data.body.item!=null)
{
var wot=0;
if(wot==0 && data.body.progress_ms+1000>data.body.item.duration_ms)
{
wot=1;
console.log('Finished Playing: ' + data.body.item.name);
res.send({song:data.body.item.name,state:"ended"});
}
else
{
res.send({song:data.body.item.name,state:"playing"});
}
}
else
{
res.send({song:null,state:"unknown"})
}
}, function(err) {
console.log('Something went wrong!', err);
});
})
//Gets the name of the song playing, just for the website
app.post('/getTrack', (req, res) => {
const track=spotifyApi.getTrack(req.body.id)
.then(function(song) {
console.log(song.body.name);
res.send({songName:song.body.name});
})
})
app.post('/makeActive',(req, res)=>{
console.log(req.body.user_id);
if(req.body.user_id==1)
{
user1Active=true;
}
else if(req.body.user_id==2)
{
user2Active=true;
}
else if(req.body.user_id==3)
{
user3Active=true;
}
else if(req.body.user_id==4)
{
user4Active=true;
}
res.send({activeUser:[user1Active,user2Active,user3Active,user4Active]})
})
app.listen(port, () =>
console.log(
'HTTP Server up. Now go to http://localhost:${port} in your browser.'
)
);
//////////// Server Helper Functions ///////////
var queue = [];
var colorArr = [];
var user1Active=false;
var user2Active=false;
var user3Active=false;
var user4Active=false;
var user1Added=false;
var user2Added=false;
var user3Added=false;
var user4Added=false;
// Reading the JSON file data
function readDatabase()
{
var qpDataset=require("./Final Database/Final Final/qp_multiuser.json");
return qpDataset;
}
function getDatafromBPM(qpData, bpm)
{
//Handling the case when the specified bpm is not present and then the next lowest bpm is selected
var qpBPMData=new Array();
while(qpBPMData.length == 0)
{
for(let i=0;i<qpData.length;i++)
{
if(qpData[i].tempo==bpm)
{
qpBPMData.push(qpData[i]);
}
}
bpm--;
}
return qpBPMData;
}
//Processing the JSON file data
function processDatabase(qpData,user)
{
//Include Song Selection Algorithm
//Sorting data according to danceability for now , until song selection algorithm
qpData.sort((first,second) => {
return first.danceability - second.danceability;
});
//Choosing the first song for the user interacted
let l=0;
while(l<qpData.length && !qpData[l].user_id.includes(user))
{
l++;
}
var temp=qpData.splice(0,l);
qpData=qpData.concat(temp);
return qpData;
}
function userControl(userPressed)
{
if(userPressed==1)
{
user1Added=true;
}
else if(userPressed==2)
{
user2Added=true;
}
else if(userPressed==3)
{
user3Added=true;
}
else if(userPressed==4)
{
user4Added=true;
}
}
function userCheck(userPressed)
{
if(userPressed==1)
{
return user1Added;
}
else if(userPressed==2)
{
return user2Added;
}
else if(userPressed==3)
{
return user3Added;
}
else if(userPressed==4)
{
return user4Added;
}
}
function getColorSequence(que)
{
colorArr = [];
let i=0;
while(i<que.length && i<4)
{
var temp=[];
let j=0;
while(j<que[i].user_id.length)
{
if(que[i].user_id[j]==1)
{
temp.push('#FF0000');
}
else if(que[i].user_id[j]==2)
{
temp.push('#0000FF');
}
else if(que[i].user_id[j]==3)
{
temp.push('#00FF00');
}
else if(que[i].user_id[j]==4)
{
temp.push('#FFFF00');
}
j++;
}
colorArr.push(temp);
i++;
}
return colorArr;
}