-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
220 lines (192 loc) · 5.87 KB
/
server.js
File metadata and controls
220 lines (192 loc) · 5.87 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
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const querystring = require('querystring');
const app = express();
const session = require('express-session');
const cookieParser = require('cookie-parser');
const axios = require('axios');
require('dotenv').config();
//const redirect_uri = 'http://localhost:3000/callback';
const redirect_uri = 'https://playlist-gen.herokuapp.com/callback';
const client_id = process.env.SPOTIFY_CLIENT_ID;
const client_secret = process.env.SPOTIFY_CLIENT_SECRET;
const port = process.env.PORT || 3000;
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
cookie: {}
}))
app.set('view engine', 'ejs')
app.get('/', function (req, res) {
res.render('pages/index');
})
app.get('/home', function (req, res) {
res.clearCookie("URIs");
res.clearCookie("artistInfo");
res.render('pages/home', { artistInfo: [] });
})
app.post('/addToList', async function(req, res){
let access_token = req.cookies.access_token
let artistName = req.body.artistName
let artistInfo = []
if(req.cookies.artistInfo){
artistInfo = req.cookies.artistInfo
}
await search(artistName, access_token).then(function(result) {
let artist = result.data.artists.items[0]
let obj = {
img: artist.images[0].url,
name: artist.name,
artistId: artist.id
}
artistInfo.push(obj)
res.cookie('artistInfo', artistInfo)
res.render('pages/home', { artistInfo: artistInfo });
})
})
app.post('/getRecommendations', async function (req, res) {
let playlistLength = req.body.playlistLength
let artistQuery = ""
let access_token = req.cookies.access_token
let artistInfo = req.cookies.artistInfo
for(i=0; i < artistInfo.length; i++){
artistQuery += artistInfo[i].artistId + ","
}
artistQuery = artistQuery.slice(0, -1);
await getRecommendations(artistQuery, playlistLength, access_token).then(function(result) {
let tracks = []
let trackURIs = []
for(i = 0; i < result.data.tracks.length; i++){
let track = result.data.tracks[i]
let obj = {
number: i+1,
name: track.name,
artist: track.artists[0].name,
duration: toMins(track.duration_ms)
}
tracks.push(obj)
trackURIs.push(track.uri)
res.cookie('URIs', trackURIs)
}
res.render('pages/playlist', { tracks: tracks });
})
})
app.post('/makePlaylist', async function (req, res) {
let playlistName = req.body.playlistName
let userId = ""
let playlistId = ""
let access_token = req.cookies.access_token
let trackURIs = req.cookies.URIs
await getUser(access_token).then(function(result) {
userId = result.data.id
})
await makePlaylist(userId, playlistName, access_token).then(function(result) {
playlistId = result.data.id
})
await addToPlaylist(userId, playlistId, trackURIs, access_token).then(function(result) {
res.clearCookie("URIs");
res.clearCookie("artistInfo");
res.render('pages/done')
})
})
// AUTHENTICATION
// Standard Oauth 2 code + token
app.get('/login', function(req, res) {
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: 'user-read-private user-read-email playlist-modify-public',
redirect_uri
}))
})
app.get('/callback', function(req, res) {
let code = req.query.code
let authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
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) {
res.cookie('access_token', body.access_token)
//let uri = 'http://localhost:3000/home'
let uri = 'https://playlist-gen.herokuapp.com/home'
res.redirect(uri)
})
})
//FUNCTIONS
async function getRecommendations(artistQuery, playlistLength, access_token) {
return await axios({
url: `https://api.spotify.com/v1/recommendations?seed_artists=${artistQuery}&limit=${playlistLength}`,
method: 'get',
headers: {
'Authorization': 'Bearer ' + access_token
}
});
}
async function getUser(access_token) {
return await axios({
url: `https://api.spotify.com/v1/me`,
method: 'get',
headers: {
'Authorization': 'Bearer ' + access_token
}
});
}
async function makePlaylist(userId, playlistName, access_token) {
return await axios({
url: `https://api.spotify.com/v1/users/${userId}/playlists`,
method: 'post',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
data: {
'name': playlistName
}
});
}
async function addToPlaylist(userId, playlistId, trackURIs, access_token) {
return await axios({
url: `https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`,
method: 'post',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
data: {
uris: trackURIs
}
});
}
async function search(artistName, access_token) {
return await axios({
url: `https://api.spotify.com/v1/search?q=${artistName}&type=artist&limit=1`,
method: 'get',
headers: {
'Authorization': 'Bearer ' + access_token
}
});
}
function toMins(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return (seconds == 60 ? (minutes+1) + ":00" : minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
}
app.listen(port, function() {
console.log('Our app is running on port ' + port);
});