-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
95 lines (87 loc) · 2.61 KB
/
index.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
const http = require('http');
const querystring = require('querystring');
const url = require('url');
const { OAuth2Client } = require('google-auth-library');
require('dotenv').config();
const { getPlaylists, setAuth, shufflePlaylist } = require('./shuffler');
const LAYOUT_START = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>`;
const LAYOUT_FOOTER = `
</body>
</html>`;
function applyLayout(bodyContent) {
return LAYOUT_START + bodyContent + LAYOUT_FOOTER;
}
async function shuffleRoute(req, res) {
const parsedUrl = url.parse(req.url);
const playlistId = parsedUrl.pathname.split('/')[2];
shufflePlaylist(playlistId);
res.end(
`${LAYOUT_START}
Shuffling playlist ${playlistId} at ${new Date()}!
${LAYOUT_FOOTER}`,
);
}
async function authMiddleware(req, res) {
const qs = querystring.parse(url.parse(req.url).query);
const oAuth2Client = new OAuth2Client(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
[process.env.REDIRECT_URI],
);
if (/token/.test(req.headers.cookie)) {
const tokens = JSON.parse(req.headers.cookie.split('=')[1]);
oAuth2Client.setCredentials(tokens);
} else if (qs.code) {
const tokensResp = await oAuth2Client.getToken(qs.code);
oAuth2Client.setCredentials(tokensResp.tokens);
res.setHeader(
'Set-Cookie',
`token=${JSON.stringify(
tokensResp.tokens,
)}; Max-Age=2147483647; HttpOnly`,
);
} else {
const authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/youtube.force-ssl',
prompt: 'consent',
});
res.setHeader('Location', authorizeUrl);
res.statusCode = 302;
return res.end();
}
setAuth(oAuth2Client);
}
http
.createServer(async (req, res) => {
try {
const parsedUrl = url.parse(req.url);
await authMiddleware(req, res);
if (parsedUrl.pathname === '/' && !res.writableEnded) {
const playlists = await getPlaylists();
const playlistsHTML = playlists
.map((pl) => {
return `<a href="/playlist/${pl.id}"><h2>${pl.snippet.title}</h2></a>`;
})
.join('<br />');
res.end(applyLayout(playlistsHTML));
} else if (parsedUrl.pathname.startsWith('/playlist/')) {
await shuffleRoute(req, res);
} else {
res.statusCode = 404;
res.end();
}
} catch (err) {
res.statusCode = 500;
res.end(err.stack);
}
})
.listen(process.env.PORT, () => {
console.log('Listening on port', process.env.PORT);
});