-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
368 lines (288 loc) · 9.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
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
363
364
365
366
367
368
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const unirest = require("unirest");
const _ = require("lodash");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const app = express();
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public"));
app.use(session({
secret: "Some Random Secret",
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect("mongodb://localhost:27017/songDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.set("useCreateIndex", true);
// Schema for the user
const userSchema = new mongoose.Schema({
googleId: String,
firstName: String,
lastName: String,
profilePhoto: String,
});
// Schema for the song
const songSchema = new mongoose.Schema({
id: String,
title: String,
preview: String,
cover_big: String,
cover_xl: String,
artist: String,
});
// Schema for playlist
const playlistSchema = new mongoose.Schema({
user: userSchema,
songs: [songSchema],
});
userSchema.plugin(findOrCreate);
const User = mongoose.model("User", userSchema);
const Playlist = mongoose.model("Playlist", playlistSchema);
const Song = mongoose.model("Song", songSchema);
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/musicbuzz"
},
function(accessToken, refreshToken, profile, cb) {
// console.log(profile);
User.findOrCreate(
{
googleId: profile.id,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
profilePhoto: profile.photos[0].value,
},
function (err, user) {
return cb(err, user);
}
);
}
));
// For searching based on the query String.
function search(query,callback){
var req = unirest("GET", "https://deezerdevs-deezer.p.rapidapi.com/search");
req.query({
"q": query
});
req.headers({
"x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
"x-rapidapi-key": process.env.API_KEY,
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
else{
// console.log(res.body);
callback(res.body);
}
});
}
// For getting a single track based on songId.
function getTrack(songId,callback){
var req = unirest("GET", "https://deezerdevs-deezer.p.rapidapi.com/track/" + songId);
req.headers({
"x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
"x-rapidapi-key": process.env.API_KEY,
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
else{
console.log(res.body);
callback(res.body);
}
});
}
// For home page
app.get("/",function(req,res){
let user=null;
if (req.isAuthenticated()) {
user = req.user;
}
res.render("home",{ showNavbar: true, user: user});
});
// For displaying the player.
app.get("/songs/:id",function(req,res){
let user = null;
if (req.isAuthenticated()) {
user = req.user;
}
const songId = req.params.id;
let trackResponse = getTrack(songId,function(response){
const songData = response;
const song = new Song({
id: songId,
title: songData.title,
preview: songData.preview,
cover_big: songData.album.cover_big,
cover_xl: songData.album.cover_xl,
artist: songData.artist.name,
});
// const song = {title: songData.title,preview: songData.preview, cover_big: songData.album.cover_big, cover_xl: songData.album.cover_xl, artist: songData.artist.name};
res.render("song",{song: song, user: user,showNavbar: false });
});
});
// for logging in the users
app.get("/login",
passport.authenticate("google", { scope: ["profile"] })
);
app.get('/auth/google/musicbuzz',
passport.authenticate('google', { failureRedirect: "/login" }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect("/");
console.log("Successfully logged in");
});
// logs out the users
app.get("/:userId/logout", function (req, res) {
if (req.isAuthenticated()){
req.logout();
res.redirect("/");
console.log("Successfully logged out");
} else {
res.redirect("/login")
}
});
// Posts the query to the search function of the API.
app.post("/search",function(req,res){
let query = req.body.searchQuery;
res.redirect("/search/" + query);
});
// Displays the search results based on the name parameter.
app.get("/search/:name",function(req,res){
let user = null;
if (req.isAuthenticated()) {
user = req.user;
}
let query = req.params.name;
let searchResponse = search(query,function(response){
const data = response.data;
let songList = [];
for(const song of data){
const newSong = new Song({
id: song.id,
title: song.title,
preview: song.preview,
cover_big: song.album.cover_big,
cover_xl: song.album.cover_xl,
artist: song.artist.name
});
songList.push(newSong);
}
res.render("songlist",{songs: songList,user: user,showNavbar: true});
});
});
// Redirects to the user's playlist.
app.get("/playlists", function (req, res) {
if (req.isAuthenticated()) {
res.redirect("/" + req.user._id + "/playlists");
} else {
res.send("You need to be logged in to view or create your playlist");
}
});
// Displays the user's playlist.
app.get("/:userId/playlists", function (req, res) {
User.findOne({ _id: req.user.id }, function (err, foundUser) {
if (err) {
console.log(err);
} else {
Playlist.findOne({ user: foundUser}, function (err, foundPlaylist) {
if (err) {
console.log(err);
} else {
if (!foundPlaylist) {
res.send("No Playlists have been created");
} else {
res.render("songlist",{songs: foundPlaylist.songs, user: foundPlaylist.user, showNavbar: true});
}
}
});
}
});
});
// Adding new songs to user playlist
app.post("/playlists", function (req, res) {
if (!req.isAuthenticated()) {
res.redirect("/login");
} else {
const currentUser = req.user;
const songInfo = JSON.parse(req.body.songInfo);
console.log(songInfo);
Playlist.findOne({ user: currentUser }, function (err, foundPlaylist) {
if (err) {
console.log(err);
} else {
const songToAdd = new Song({
id: songInfo.id,
title: songInfo.title,
preview: songInfo.preview,
cover_big: songInfo.cover_big,
cover_xl: songInfo.cover_xl,
artist: songInfo.artist
});
if (!foundPlaylist) {
// Create a new Playlist
songs = [songToAdd];
const list = new Playlist({
user: currentUser,
songs: songs,
});
list.save(function (err) {
if (!err) {
res.redirect("/");
console.log("Successfully added song to your favorites");
}
});
} else {
let flag = 1;
// Check if song already exists in the playlist
foundPlaylist.songs.forEach(song => {
if (song.id === songToAdd.id) {
flag = 0;
}
if (flag === 0) {
console.log("Song already exists in your favorites");
res.redirect("/");
} else {
// Add Song to existing Playlist
foundPlaylist.songs.push(songToAdd);
foundPlaylist.save(function (err) {
console.log("Successfully added song to your favorites");
res.redirect("/");
});
}
});
}
}
});
}
});
// let PORT = process.env.PORT;
// if (PORT == null || PORT == "") {
// PORT = 3000;
// }
// Starts the server at the desired PORT.
app.listen(3000,function(){
console.log("Server started on port 3000");
});