-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
334 lines (290 loc) · 11.6 KB
/
server.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
//LINK BETWEEN DATABASE AND WEBAPP
var mysql = require('mysql')
var express = require('express');
var app = express();
//connect to the jukebox database
var connection = mysql.createConnection({
host : 'junkeboxinstance.c4jsultqkn4z.us-east-2.rds.amazonaws.com',
user : 'EuraShin',
password : 'mypassword',
database : 'jukend'
});
connection.connect();
var request = require('request');
var cors = require('cors');
var querystring = require('querystring');
var cookieParser = require('cookie-parser');
var SpotifyWebApi = require('spotify-web-api-node');
var client_id = '690951f82905419d8342d9f33e3e6227'; // Your client id
var client_secret = 'bd8f410b044a4f16a17151c7d1c57601'; // Your secret
var redirect_uri = 'http://localhost:8080/callback'; // Your redirect uri
/**
* Generates a random string containing numbers and letters
* @param {number} length The length of the string
* @return {string} The generated string
*/
var generateRandomString = function(length) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var stateKey = 'spotify_auth_state';
/************ START OUR APP ************/
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8080;
/************ SPOTIFY WEBAPI WRAPPER SETUP ************/
var spotify_auth = "";
var access_token = "";
var spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret,
redirectUri: redirect_uri
});
// set the view engine to ejs
app.set('view engine', 'ejs');
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public')).use(cors()).use(cookieParser());
// set the home page route
app.get('/', function(req, res) {
res.render('index');
});
app.get('/loggedin', function(req, res) {
// set access token
spotifyApi.setAccessToken(access_token);
// get user and pass relevant information
spotifyApi.getMe()
.then(function(data) { // get user
var name = data.body.display_name;
var user_uri = data.body.uri;
var user_id = data.body.id;
connection.query("INSERT IGNORE INTO user(user_uri, user_name, user_id) VALUES ('" + user_uri + "','"
+ name + "','" + user_id + "')");
spotifyApi.getMyTopTracks({limit: 50})
.then(function(data) { // get track
for(var x in data.body.items) {
connection.query("INSERT IGNORE INTO song(uri, title, album, artist) VALUES ('" + data.body.items[x].uri + "','" + mysqlEscape(data.body.items[x].name) + "','" + mysqlEscape(data.body.items[x].album.name) + "','" + mysqlEscape(data.body.items[x].artists[0].name) + "')");
connection.query("INSERT IGNORE INTO stores(user_uri, s_uri) VALUE ('" + user_uri + "','" + data.body.items[x].uri + "')");
}
});
// render page
res.render("realFrontPage", {name: name, user_uri: user_uri, user_id: user_id});
}).catch(function(err) {
console.error(err.message);
});
});
/********** SESSION FUNCTIONS *************/
//called when "start a session" button is pressed
//input: useruri
app.get('/create', function(req,res) {
//generate random string
var randString = generateRandomString(5);
var link = 'http://localhost:8080/join/' + randString;
//start a session in the database
connection.query("INSERT IGNORE INTO jam(uniqueLink, host) VALUES ('" + link + "','"+ req.headers.useruri + "')"); //make session
connection.query("SELECT user_name, user_id FROM user WHERE user_uri = '" + req.headers.useruri + "'", function(err, rows, fields) { //
if (err) throw err;
//handle rendering the temp page by ID
console.log(req.headers.useruri);
var users = [].concat([rows[0].user_name]);
res.render("session_page", {link: link, users:users, host_id: rows[0].user_id, host_uri: req.headers.useruri}); //makes the webpage
});
});
//input
app.get('/session_start', function(req,res) {
spotifyApi.createPlaylist(req.headers.userid, 'jukebox', { 'public' : true })
.then(function(data) {
// dependent upon radio button selection for method of constructing playlist
var playlist = data.body.id;
// select the users in this session
connection.query("SELECT user_id FROM joins, user WHERE host_uri = '" + req.headers.useruri + "' "
+ "AND joins.user_uri = user.user_uri",
function(err, rows, fields) {
//make list of users, append host user
var users = [];
for(var x in rows) {
console.log(rows[x].user_id);
users.push(rows[x].user_id);
}
users.push(req.headers.userid);
users.forEach(function(user) {
//select the songs associated with this user
connection.query("SELECT s_uri FROM stores WHERE user_uri = 'spotify:user:" + user + "'",
function(err, rows, fields) {
var songs = [];
for(var z in rows) {
songs.push(rows[z].s_uri);
}
spotifyApi.addTracksToPlaylist(playlist, songs)
.then(function(data) {
spotifyApi.play({context_uri: "spotify:playlist:" + playlist});
spotifyApi.unfollowPlaylist(playlist);
}).catch(function(err) {
console.error(err);
});
});
});
});
});
});
//called when "join a session" button is pressed
app.get('/join/:uniqueLink', function(req, res) {
//find hosturi
var link = 'http://localhost:8080/join/' + req.params.uniqueLink;
console.log(link);
connection.query("SELECT host FROM jam WHERE jam.uniqueLink = '" + link + "'", function(err, rows, fields ) {
hosturi = rows[0].host;
console.log(hosturi);
//add user to session
connection.query("INSERT IGNORE INTO joins(host_uri, user_uri) VALUES ('" + hosturi + "','" + req.headers.useruri + "')");
//handle rendering the temp page by ID
//select all the users in session
connection.query("SELECT DISTINCT user_name FROM user,joins,jam WHERE jam.host ='" + hosturi +
"' AND user.user_uri = joins.user_uri", function(err, rows, fields) {
if (err) throw err;
var other_users = [];
for(var i=0; i<rows.length; i++) {
other_users.push(rows[i].user_name);
}
other_users = [].concat(other_users);
//select the host user
connection.query("SELECT user_name, user_id FROM user WHERE user_uri = '" + hosturi + "'", function(err, rows, fields) { //
if (err) throw err;
var host = rows[0].user_name;
var hostid = rows[0].user_id;
var users = other_users.concat(host);
console.log(users);
res.render('session_page', {link: link, users:users, host_uri:hosturi, host_id:hostid});
});
});
});
});
app.get('/refresh', function(req,res) {
//find hosturi
var link = 'http://localhost:8080/join/' + req.params.uniqueLink;
console.log(link);
connection.query("SELECT host FROM jam WHERE jam.uniqueLink = '" + link + "'", function(err, rows, fields ) {
hosturi = rows[0].host;
console.log(hosturi);
//handle rendering the temp page by ID
//select all the users in session
connection.query("SELECT DISTINCT user_name FROM user,joins,jam WHERE jam.host ='" + hosturi +
"' AND user.user_uri = joins.user_uri", function(err, rows, fields) {
if (err) throw err;
var other_users = [];
for(var i=0; i<rows.length; i++) {
other_users.push(rows[i].user_name);
}
other_users = [].concat(other_users);
//select the host user
connection.query("SELECT user_name, user_id FROM user WHERE user_uri = '" + hosturi + "'", function(err, rows, fields) { //
if (err) throw err;
var host = rows[0].user_name;
var hostid = rows[0].user_id;
var users = other_users.concat(host);
console.log(users);
res.render('session_page', {link: link, users:users, host_uri:hosturi, host_id:hostid});
});
});
});
});
app.get('/destroy', function(req,res){
// pause spotify playback
spotifyApi.pause();
//delete songs associated with users
hosturi = req.headers.useruri;
connection.query("DELETE FROM stores");
//delete host users session
connection.query("DELETE FROM joins WHERE host_uri='" +hosturi+"'");
connection.query("DELETE FROM jam WHERE host='" +hosturi+"'");
//redirect to homepage
res.render('index');
});
// spotify authentication verification
// upon clicking login button redirect to prompt user for authorization
app.get('/login', function(req, res) {
var state = generateRandomString(16);
res.cookie(stateKey, state);
// your application requests authorization
var scope = 'user-read-private user-read-email user-top-read user-read-playback-state'
+ ' user-modify-playback-state playlist-modify-private playlist-read-private playlist-modify-public';
res.redirect('http://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
}));
});
// receive the response and establish authentication code global variable
app.get('/callback', function(req, res) {
// your application requests refresh and access tokens
// after checking the state parameter
spotify_auth = req.query.code || null;
console.log(spotify_auth);
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;
if (state === null || state !== storedState) {
res.redirect('/#' +
querystring.stringify({
error: 'state_mismatch'
}));
} else {
res.clearCookie(stateKey);
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: spotify_auth,
redirect_uri: redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
access_token = body.access_token;
var refresh_token = body.refresh_token;
var 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, function(error, response, body) {
console.log(body);
});
// we can also pass the token to the browser to make requests from there
res.redirect('/loggedin#' +
querystring.stringify({
access_token: access_token,
refresh_token: refresh_token,
}));
} else {
res.redirect('/#' +
querystring.stringify({
error: 'invalid_token'
}));
}
});
}
});
function mysqlEscape(stringToEscape){
return stringToEscape
.replace("\\", "\\\\")
.replace("\'", "\\\'")
.replace("\"", "\\\"")
.replace("\n", "\\\n")
.replace("\r", "\\\r")
.replace("\x00", "\\\x00")
.replace("\x1a", "\\\x1a");
}
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});