-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
187 lines (157 loc) · 4.52 KB
/
app.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
var express = require('express');
var passport = require('passport');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var SpotifyStrategy = require('passport-spotify').Strategy;
var request = require('request');
var CLIENT_ID = process.env['SPOTIFY_CLIENT_ID'];
var CLIENT_SECRET = process.env['SPOTIFY_CLIENT_SECRET'];
var REDIRECT_URI = 'http://localhost:8888/callback';
var OAUTH_SCOPES = 'user-read-private user-read-email playlist-read-private playlist-modify-public playlist-modify-private'
var tokens = {};
var playlists = {};
var removeLists = {};
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(cookieParser());
app.use(bodyParser());
app.use(session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/static'));
passport.use(
new SpotifyStrategy({
clientID: CLIENT_ID,
clientSecret: CLIENT_SECRET,
callbackURL: "http://localhost:8888/callback"
},
function(accessToken, refreshToken, profile, done) {
tokens[profile.id] = accessToken;
return done(null, profile);
}
));
function root(req, res) {
res.render('login.html');
}
function refreshPlaylists(id, done) {
request({
url: "https://api.spotify.com/v1/users/" + id + "/playlists",
headers: {
Authorization: "Bearer " + tokens[id]
}
}, function (err, apiRes, body) {
var data = JSON.parse(body);
playlists[id] = data;
done(err, data);
});
}
function prompt(req, res) {
refreshPlaylists(req.user.id, function (err, data) {
if (err) {
console.log(err);
return res.send('error');
}
res.render('search.html', {user: req.user});
});
}
function remove(req, res) {
doRemove(req.user.id, req.body.album, done);
function done() {
res.send('done probably maybe');
}
}
function doRemove(id, album, done) {
console.log(' doRemove args', arguments);
var list = playlists[id].items;
var numDone = list.length;
for (var i = 0; i < numDone; i++) {
process(i);
}
function process(i) {
request({
url: list[i].href,
headers: {Authorization: "Bearer " + tokens[id]}
}, processPlaylist);
}
function processPlaylist(err, apiRes, body) {
if (err) {
return doneOne(err);
}
// loop over, send delete requests
var toDelete = {};
var data = JSON.parse(body);
var tracks = data.tracks.items;
var i;
for (i = 0; i < tracks.length; i++) {
if (tracks[i].track.album.name.indexOf(album) !== -1) {
toDelete[tracks[i].track.uri] = toDelete[tracks[i].track.uri] || [];
toDelete[tracks[i].track.uri].push(i);
}
}
var deleteIds = Object.keys(toDelete);
var deleteArray = [];
if (deleteIds.length === 0) {
return doneOne();
}
for (i = 0; i < deleteIds.length; i++) {
deleteArray.push({
uri: deleteIds[i],
positions: toDelete[deleteIds[i]]
});
}
request({
method: "DELETE",
url: data.href + "/tracks",
headers: {Authorization: "Bearer " + tokens[id]},
json: true,
body: {
tracks: deleteArray
}
}, doneOne);
}
function doneOne(err) {
numDone -= 1;
if (numDone < 1) {
done();
}
if (err) {
console.err(err);
}
}
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/auth/spotify');
}
app.get('/auth/spotify',
passport.authenticate('spotify', {scope: OAUTH_SCOPES, showDialog: true}),
function(req, res){
// The request will be redirected to spotify for authentication, so this
// function will not be called.
});
app.get('/callback',
passport.authenticate('spotify', { failureRedirect: '/auth/spotify', scope: OAUTH_SCOPES }),
function(req, res) {
res.redirect('/prompt');
});
app.get('/', root);
app.get(
'/prompt',
ensureAuthenticated,
prompt
);
app.post(
'/remove',
ensureAuthenticated,
remove
);
app.listen(8888);