-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (69 loc) · 2.38 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
const SpotifyWebApi = require("spotify-web-api-node");
const express = require("express");
const cors = require('cors');
const cookieParser = require('cookie-parser');
const querystring = require('querystring');
const { client_id, client_secret, redirect_uri, state } = require("./config.json");
const tal = require("./tequilaAndLights.js");
const spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret,
redirectUri: redirect_uri
});
const app = express();
app.use(express.static(__dirname + '/public'))
.use(cors())
.use(cookieParser());
app.get('/login', function (req, res) {
const scopes = ["user-read-private", "user-read-email", "user-read-currently-playing", "user-read-playback-state", "user-modify-playback-state"];
const authorizeURL = spotifyApi.createAuthorizeURL(scopes, state, true);
res.redirect(authorizeURL);
});
function refreshToken() {
spotifyApi.refreshAccessToken().then(function(data) {
console.log('The access token has been refreshed!');
spotifyApi.setAccessToken(data.body['access_token']);
}).catch(function(err) {
console.log(err);
});
}
app.get('/callback', function (req, res) {
const code = req.query.code;
spotifyApi.authorizationCodeGrant(code).then(function(data) {
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.setRefreshToken(data.body['refresh_token']);
res.redirect('/#' +
querystring.stringify({
access_token: spotifyApi.getAccessToken(),
refresh_token: spotifyApi.getRefreshToken()
})
);
tal.run(spotifyApi);
//Automatically refresh access token after 30 mins
const refresh_time = 5 * 60 * 1000;
setInterval(function() {
refreshToken();
console.log("refreshing...");
}, refresh_time);
}).catch(function(err) {
console.log(err);
});
});
app.get('/refresh_token', function (req, res) {
refreshToken();
});
app.get("/logout", function (req, res) {
spotifyApi.resetAccessToken();
spotifyApi.resetRefreshToken();
console.log("Logging out...");
res.redirect('/');
});
app.get("/setIntervals", function(req, res) {
tal.setIntervalTimes(req.query.base, req.query.max);
});
app.get("/tequila", function(req, res) {
tal.testTequila(spotifyApi);
});
console.log('Listening on 8888');
app.listen(8888);