-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
265 lines (227 loc) · 6.98 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
const express = require("express");
const request = require("request");
const querystring = require("querystring");
const cookieParser = require("cookie-parser");
const mustache = require("mustache");
const fs = require("fs");
const SpotifyWebApi = require("spotify-web-api-node");
const session = require("express-session");
const app = express();
const router = express.Router();
const port = process.env.PORT;
const path = __dirname + "/views/";
var client_id = process.env.client_id; // Application client
var client_secret = process.env.client_secret; // Application secret
var redirect_uri = process.env.redirect_uri; // Application redirect uri
var scopes = ["playlist-modify-public", "playlist-modify-private"];
var stateKey = "spotify_auth_state";
// Create the API object with the credentials
var spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret,
redirectUri: redirect_uri
});
app.use(express.static(__dirname)); // Serves static files
app.use(express.static(__dirname + "/views/")).use(cookieParser());
app.use(
session({
store: new session.MemoryStore(),
secret: "do-the-fandango",
resave: false,
saveUninitialized: false,
cookie: { maxAge: 86400000 }
})
);
app.use(function(req, res, next) {
if (!req.session.song_ids) {
req.session.song_ids = [];
}
next();
});
// Generates a random string containing numbers and letters
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;
};
router.get("/", function(req, res) {
res.sendFile(path + "index.html");
});
router.get("/about", function(req, res) {
res.sendFile(path + "about.html");
});
router.get("/login", function(req, res) {
var state = generateRandomString(16);
res.cookie(stateKey, state);
// Create the authorization URL
var authorizeURL = spotifyApi.createAuthorizeURL(scopes, state);
res.redirect(authorizeURL);
});
router.get("/error", function(req, res) {
res.status(400);
res.sendFile(path + "error_2.html");
});
router.get("/auth", function(req, res) {
var code = req.query.code || null;
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;
if (state === null || state !== storedState) {
res.redirect("/error");
} else {
res.clearCookie(stateKey);
// Retrieve an access token and a refresh token
spotifyApi.authorizationCodeGrant(code).then(
function(data) {
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body["access_token"]);
spotifyApi.setRefreshToken(data.body["refresh_token"]);
res.redirect("/");
},
function(err) {
res.redirect("/error");
}
);
}
});
// Retrieve recommended playlist based on artist
router.get("/artist/playlist", function(req, res) {
query = req.query;
var artist = query["input"];
spotifyApi
.searchArtists(artist)
.then(function(data) {
var artist_id = data.body.artists.items[0].id;
return artist_id;
})
.then(function(artist_id) {
return spotifyApi.getRecommendations({ seed_artists: [artist_id] });
})
.then(function(data) {
var playlist = [];
var tracks = data.body.tracks;
for (var i = 0; i < tracks.length; i++) {
var track = {
id: tracks[i].id,
song: tracks[i].name,
artist: tracks[i].artists[0].name,
album: tracks[i].album.name,
albumImageURL: tracks[i].album.images[0].url,
previewURL: tracks[i].preview_url
};
playlist.push(track);
}
return {
"playlist": playlist
};
})
.then(function(playlistObj) {
fs.readFile(path + "playlist.html", function(err, data) {
res.writeHead(200, {
"Content-Type": "text/html"
});
req.session.song_ids = [];
for (var i = 0; i < playlistObj.playlist.length; i++) {
req.session.song_ids.push(playlistObj.playlist[i].id);
}
res.write(mustache.render(data.toString(), playlistObj));
res.end();
});
})
.catch(function(err) {
res.redirect("/error");
});
});
// Retrieve recommended playlist based on song
router.get("/song/playlist", function(req, res) {
query = req.query;
var song = query["input"];
spotifyApi
.searchTracks(song)
.then(function(data) {
var track_id = data.body.tracks.items[0].id;
return track_id;
})
.then(function(track_id) {
return spotifyApi.getRecommendations({ seed_tracks: [track_id] });
})
.then(function(data) {
var playlist = [];
var tracks = data.body.tracks;
for (var i = 0; i < tracks.length; i++) {
var track = {
id: tracks[i].id,
song: tracks[i].name,
artist: tracks[i].artists[0].name,
album: tracks[i].album.name,
albumImageURL: tracks[i].album.images[0].url,
previewURL: tracks[i].preview_url
};
playlist.push(track);
}
return {
"playlist": playlist
};
})
.then(function(playlistObj) {
fs.readFile(path + "playlist.html", function(err, data) {
res.writeHead(200, {
"Content-Type": "text/html"
});
req.session.song_ids = [];
for (var i = 0; i < playlistObj.playlist.length; i++) {
req.session.song_ids.push(playlistObj.playlist[i].id);
}
res.write(mustache.render(data.toString(), playlistObj));
res.end();
});
})
.catch(function(err) {
res.redirect("/error");
});
});
// Save playlist to the user's Spotify account
router.get("/save_playlist", function(req, res) {
query = req.query;
var playlist_name = query["playlist_name"];
var settings = query["settings"];
// Get the authenticated user, create a playlist, and add tracks to playlist
spotifyApi
.getMe()
.then(function(data) {
var user_id = data.body.id;
return user_id;
})
.then(function(user_id) {
var user_choice;
if (settings === "private") {
user_choice = false;
} else {
user_choice = true;
}
return spotifyApi.createPlaylist(user_id, playlist_name, {
public: user_choice
});
})
.then(function(data) {
var playlist_id = data.body.id;
return playlist_id;
})
.then(function(playlist_id) {
var songs = [];
for (var i = 0; i < req.session.song_ids.length; i++) {
songs.push("spotify:track:" + req.session.song_ids[i]);
}
return spotifyApi.addTracksToPlaylist(playlist_id, songs);
})
.catch(function(err) {
res.redirect("/error");
});
res.sendFile(path + "saved_playlist.html");
});
app.use("/", router);
var server = app.listen(port);
console.log(`Listening on port ${server.address().port}...`);