-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
286 lines (265 loc) · 9.83 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
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
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const dotenv = require("dotenv")
const cookieParser = require('cookie-parser');
const request = require('request');
const { Liquid } = require('liquidjs');
const { generateRandomString, getEnv } = require("./helpers");
const { kmeans } = require("./kmeans");
const { exit } = require("process");
var session = require("express-session")
const { Client } = require('pg')
/* dotenv Setup */
const result = dotenv.config();
if (result.error) {
console.log("Error: dotenv not configured correctly.")
throw result.error;
}
const PORT = 8888;
const HOSTNAME = "localhost";
const CLIENT_ID = getEnv("SPOTIFY_CLIENT_ID");
const REDIRECT_URI = getEnv("SPOTIFY_REDIRECT_URI");
const CLIENT_SECRET = getEnv("SPOTIFY_CLIENT_SECRET");
const SESSION_SECRET = getEnv("SESSION_SECRET")
var stateKey = 'spotify_auth_state';
/* Express.js setup with socket.io and LiquidJS templating */
const app = express();
const engine = new Liquid();
app.engine("liquid", engine.express());
app.set("views", __dirname + "/views");
app.set("view engine", "liquid");
app.use(express.static(__dirname + "/static"))
app.use(cookieParser());
const sessionMiddleware = session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
// secure: true
}
})
app.use(sessionMiddleware)
const server = createServer(app);
const io = new Server(server);
io.engine.use(sessionMiddleware);
/* Connect to Postgres DB */
// connection information is provided with .env variables
const client = new Client();
client.connect()
.then(() => {
console.log("Connected to PostgreSQL database")
})
.catch((err) => {
console.error(err)
process.exit(1)
})
async function fetchColors(albumID, albumURL) {
var colors = ['#D9D9D9', '#D9D9D9', '#D9D9D9', '#D9D9D9', '#D9D9D9']
try {
const res = await client.query('SELECT * FROM colors WHERE albumID = $1 LIMIT 1;', [albumID])
if (res.rows.length > 0) {
colors = res.rows[0].colors
} else {
colors = await kmeans(albumURL)
await client.query('INSERT INTO colors (albumID, colors) VALUES ($1, $2)', [albumID, colors])
}
} catch (err) {
console.error(err)
}
return colors
}
function fetchCurrentlyPlaying(socket, access_token) {
var currently_playing_params = {
url: 'https://api.spotify.com/v1/me/player/currently-playing',
headers: { 'Authorization': 'Bearer ' + access_token },
json: true
};
request.get(currently_playing_params, async (error, response, body) => {
if (error) {
console.log(error)
socket.emit('error', 'Error: Invalid response from Spotify: ' + error)
} else if (response.statusCode == 200) {
// response OK
var albumURL = body.item.album.images[0].url;
var title = body.item.name;
var artists = body.item.artists.map((artist) => artist.name).join(', ')
var albumID = body.item.album.id
try {
var colors = await fetchColors(albumID, albumURL)
socket.emit("currentlyPlaying", title, artists, albumURL, colors)
} catch (error) {
socket.emit("error", "Error: Async function kmeans failed: " + error)
}
} else if (response.statusCode == 401) {
// bad or expired token
socket.emit('refreshToken')
} else if (response.statusCode == 429) {
// exceeded data rate
socket.emit('error', "Error: Spotipi exceeded Spotify API data rate.")
} else if (response.statusCode != 204) {
// invalid HTTP code received
socket.emit("error", "Error: invalid HTTP code received" + response.statusCode)
}
});
}
// home page
app.get("/", (req, res) => {
var error = req.query['error']
if (error) {
res.render('home', {
error: error
});
} else {
res.render('home');
}
});
// dashboard - login required
app.get("/dashboard", (req, res) => {
if (req.session.user) {
res.render('dashboard', {
user: req.session.user,
});
} else {
var params = new URLSearchParams({ "error": "Error: Login required." });
res.redirect('/?' + params.toString())
}
})
// request authorization from Spotify
app.get("/auth/login", (req, res) => {
var state = generateRandomString(16);
res.cookie(stateKey, state);
var params = new URLSearchParams({
'response_type': 'code',
'client_id': CLIENT_ID,
'scope': "user-read-currently-playing user-read-private user-read-email",
'redirect_uri': REDIRECT_URI,
'state': state,
});
if (req.query['show_dialog']) {
params.append('show_dialog', true)
}
res.redirect("https://accounts.spotify.com/authorize?" + params.toString());
});
// Spotify authorization callback
app.get("/auth/callback", (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) {
var params = new URLSearchParams({ 'error': 'Error: State mismatch on callback.' });
res.redirect('/?' + params.toString());
} else {
// generate token request
res.clearCookie(stateKey);
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: REDIRECT_URI,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')
},
json: true
};
// post token request
request.post(authOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
req.session.access_token = body.access_token
req.session.refresh_token = body.refresh_token
var userOptions = {
url: 'https://api.spotify.com/v1/me',
headers: { 'Authorization': 'Bearer ' + body.access_token },
json: true
};
request.get(userOptions, (error, response, body) => {
if (!error && response.statusCode == 200) {
req.session.user = body.display_name
res.redirect('/dashboard');
} else {
req.session.access_token = null
req.session.refresh_token = null
req.session.user = null
var params = new URLSearchParams({ "error": "Error: Failed to fetch username from Spotify." });
res.redirect('/?' + params.toString());
}
})
} else {
req.session.access_token = null
req.session.refresh_token = null
req.session.user = null
var params = new URLSearchParams({ "error": "Error: Invalid token in callback." });
res.redirect('/?' + params.toString());
}
});
}
});
// refresh access token
app.get("/auth/refresh_token", (req, res) => {
if (req.session.user) {
var refresh_token = req.session.refresh_token
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')
},
form: {
grant_type: 'refresh_token',
refresh_token: refresh_token
},
json: true
};
request.post(authOptions, (error, response, body) => {
if (!error && response.statusCode == 200) {
const cookieAttributes = {
httpOnly: true,
secure: true,
}
req.session.access_token = body.access_token
req.session.refresh_token = body.refresh_token
res.redirect(next)
} else {
var params = new URLSearchParams({ "error": "Error: Failed to fetch refresh token from Spotify" });
res.redirect('/?' + params.toString());
}
});
} else {
var params = new URLSearchParams({ "error": "Error: Login required." });
res.redirect('/?' + params.toString())
}
});
/* Cleanup PostgreSQL connection */
process.on('SIGINT', async () => {
console.log("Disconnecting from PostgreSQL database")
await client.end()
process.exit(0)
})
/* Websocket connection */
io.on("connection", (socket) => {
console.log("* socket.io client " + socket.id + " connected");
var session = socket.request.session
if (session) {
fetchCurrentlyPlaying(socket, session.access_token); // initial fetch
const intervalId = setInterval(() => {
fetchCurrentlyPlaying(socket, session.access_token);
}, 5000);
socket.on('disconnect', () => {
console.log('* socket.io client ' + socket.id + ' disconnected');
clearInterval(intervalId);
});
}
}).on("error", (err) => {
console.log("Error: Failed to connect to websocket, Raspberry Pi not turned on or connected to internet.")
console.log(err);
});
/* Express Server connection */
server.listen(PORT, HOSTNAME, () => {
console.log(`App running on ${HOSTNAME}:${PORT}`);
}).on("error", (err) => {
console.log("Error: Express server not configured correctly.")
console.log(err);
});