-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (103 loc) · 3.88 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
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
//////////////////////////////////////////////////////////////////////////////////////////////////
// MUSILINKS //
//////////////////////////////////////////////////////////////////////////////////////////////////
// By: Bernardo Luiz Knackfuss, 2020 //
//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
// REQUIRES
/////////////////////////////////////////////////
const express = require("express"),
http = require("http"),
path = require("path"),
bodyparser = require("body-parser"),
fs = require("fs");
/////////////////////////////////////////////////
// STATE
/////////////////////////////////////////////////
var spotifyToken = null;
var spotifyExpiration = null;
var globalDate = null;
/////////////////////////////////////////////////
// MAIN EXECUTION
/////////////////////////////////////////////////
var getToken = require("./authentication");
refreshToken();
main();
async function main() {
const app = express();
app.set("port", process.env.PORT || 3000);
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
var jsonParser = bodyparser.json();
app.use((err, _req, res, _next) => {
console.error(err.stack);
res.status(500).send("Error caught! Debug this bernardo...");
});
app.get("/", (_req, res) => {
res.render("index", {
analyticsTagManagerUrl: process.env.ANALYTICS_GTM_URL,
analyticsContainer: process.env.ANALYTICS_GTM_CONTAINER_ID,
analyticsUA: process.env.ANALYTICS_GA_UA
});
});
app.get("/token", async (_req, res) => {
res.json({ token: spotifyToken, expiration: spotifyExpiration })
});
app.post("/search", jsonParser, async (req, res) => {
let search = await searchMusicbrainz(req.body["search-text"]);
res.json(search);
});
let server = http.createServer(app);
server.on('error', (e) => console.log(e));
server.listen(app.get("port"), function () {
console.log("Server up and running on port " + app.get("port"));
});
}
function countdown(expiration) {
return new Promise(async (resolve) => {
spotifyExpiration = expiration;
let timer = setInterval(() => spotifyExpiration = spotifyExpiration - 1, 1000);
let loop = () => {
if (spotifyExpiration > 0) {
// log(`Test log, spotifyExpiration = ${spotifyExpiration}`)
setTimeout(loop, 1000);
}
else {
log(`Countdown promise resolving, spotifyExpiration = ${spotifyExpiration}
now awaiting token renewal. ${globalDate}`);
clearInterval(timer);
resolve('ok');
}
}
loop();
});
}
async function refreshToken() {
getToken()
.then((auth) => {
spotifyToken = auth.token;
log(`Token acquired, spotifyToken = ${spotifyToken} and expiration = ${auth.
expiration}.`);
countdown(auth.expiration)
.then(() => refreshToken());
})
.catch(exception => {
if (exception.error === "RESPONSE_NOT_EXPECTED") {
log(`\n RESPONSE_NOT_EXPECTED error. Here is response body: \n`);
}
else if (exception.error === "RESPONSE_ERROR") {
log("\n RESPONSE_ERROR error. Here is error object: \n");
}
else {
log("\n ERROR_UNKNOWN error. Here is details: \n");
}
for (const prop in exception.details) {
log(`${prop} : ${exception.details[prop]} \n`);
}
});
}
function log(message) {
globalDate = new Date();
console.log("\n" + message + " :: " + globalDate.toJSON());
}