-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (86 loc) · 2.94 KB
/
server.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
const express = require("express");
const app = express(); // initialize an express instance called 'app'
const axios = require("axios");
const { getAccessToken } = require("./spotify/auth.js")
const { searchTracks, getRecommendations } = require("./spotify/actions.js")
if (!process.env.SPOTIFY_CLIENT_ID || !process.env.SPOTIFY_CLIENT_SECRET) {
console.error("ERROR: Missing one or more crucial spotify variables.");
}
app.use(express.json()); // set up the app to parse JSON request bodies
// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// return the public/index.html file when a GET request is made to the root path "/"
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
app.post("/recommendations", async (req, res) => {
if (!req.body) {
return res.status(400).send({ message: "Not working bruh, Retry" })
}
const { track, artist } = req.body;
let accessToken;
try {
accessToken = await getAccessToken();
console.log("authentication successful")
} catch (err) {
console.error(err.message);
return res
.status(500)
.send({ message: "Something went wrong when fetching access token" })
}
const http = axios.create({
headers: { Authorization: `Bearer ${accessToken}` },
});
if (!track && !artist) {
// Random recommendation logic
try {
const result = await getRecommendations(http, {});
const { tracks } = result;
if (!tracks || !tracks.length) {
return res.status(404).send({ message: "No recommendations found." })
}
return res.send({ tracks });
} catch (err) {
console.error(err.message);
console.log("iamnick random error")
return res
.status(500)
.send({ message: "Something went wrong when getting recommendations" })
}
}
let trackId;
try {
const result = await searchTracks(http, { track, artist });
const { tracks } = result;
if (!tracks || !tracks.items || !tracks.items.length) {
return res
.status(404)
.send({ message: `Song ${track} by ${artist} not found.` })
}
trackId = tracks.items[0].id;
} catch (err) {
console.error(err.message);
return res
.status(500)
.send({ message: "Something went wrong when searching tracks" })
}
try {
const result = await getRecommendations(http, { trackId })
const { tracks } = result
if (!tracks || !tracks.length) {
return res.status(404).send({ message: "No recommendations found." })
}
return res.send({ tracks });
} catch (err) {
console.error(err.message);
console.log("iamnick error")
return res
.status(500)
.send({ message: "Something went wrong when getting recommendations" })
}
});
// start listening on a port provided by Glitch
app.listen(process.env.PORT, () => {
console.log(`Example app listening at port ${process.env.PORT}`);
});