-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (123 loc) · 4.97 KB
/
index.js
File metadata and controls
143 lines (123 loc) · 4.97 KB
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
const { response } = require('express');
const express = require('./node_modules/express');
const request = require('./node_modules/request');
const app = express();
const path = require('path');
const cookie = process.env.COOKIE;
// Serve static files from the 'public' folder
app.use(express.static(path.join(__dirname, 'public')));
// New endpoint for serving documentation
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/key', (req, res) => {
return res.send("success")
});
app.get('/getLyrics/:trackId', (req, res) => {
request.get({
url: process.env.TOKEN_URL,
headers: {
"Cookie": cookie,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
}, (error, response, body) => {
if (error) {
console.log(error);
console.log(respone);
return res.status(500).send(error);
}
let json = JSON.parse(body);
let clientId = json.clientId;
let accessToken = json.accessToken;
console.log(accessToken);
request.get({
url: process.env.LYRICS_BASE_URL + `${req.params.trackId}?format=json&vocalRemoval=false&market=from_token`,
headers: {
"app-platform": "WebPlayer",
"Authorization": `Bearer ${accessToken}`
}
}, (error, response, body) => {
if (error) {
return res.status(500).send
}
console.log(response.body);
res.header("Access-Control-Allow-Origin", "*");
res.send(JSON.stringify(JSON.parse(response.body), null, 2));
});
});
});
app.get('/getLyricsByName/:musician/:track', (req, res) => {
// Your client ID and secret
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
// Encode the client ID and secret
const encoded = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
// Make a POST request to the Spotify token endpoint to get an access token
request.post({
url: process.env.SEARCH_TOKEN,
headers: {
'Authorization': `Basic ${encoded}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
grant_type: 'client_credentials'
}
}, (error, response, body) => {
if (error) {
return res.status(500).send(error);
}
// Parse the response body into JSON
let json = JSON.parse(body);
// Get the access token from the response
let accessToken = json.access_token;
// Build the Spotify API search URL with the musician and track name, and set the limit to 5
const searchUrl = process.env.SEARCH_URL + `${encodeURIComponent(req.params.musician)}%20track:${encodeURIComponent(req.params.track)}&type=track&limit=10`;
// Make a GET request to the Spotify API search URL
request.get({
url: searchUrl,
headers: {
'Authorization': `Bearer ${accessToken}`
}
}, (error, response, body) => {
if (error) {
return res.status(500).send(error);
}
// Parse the response body into JSON
let json = JSON.parse(body);
if (!json.tracks.items.length) {
return res.status(404).send("No remix lyrics was found");
}
let realTrack;
if (req.query.remix === 'true') {
json.tracks.items =
json.tracks.items = json.tracks.items
.filter(track => track.name.toLowerCase().includes("remix"))
.sort((a, b) => b.popularity - a.popularity);
if (json.tracks.items.length == 0) {
json.tracks.items = json.tracks.items
.filter(track => !track.name.toLowerCase().includes("remix"))
.sort((a, b) => b.popularity - a.popularity);
}
} else {
json.tracks.items = json.tracks.items
.filter(track => !track.name.toLowerCase().includes("remix"))
.sort((a, b) => b.popularity - a.popularity);
}
realTrack = json.tracks.items.shift();
if (realTrack) {
console.log(realTrack.id);
let trackId = realTrack.id;
// Use the track ID to make a request to the initial route
res.header("Access-Control-Allow-Origin", "*");
res.redirect(`/getLyrics/${trackId}`);
} else {
res.header("Access-Control-Allow-Origin", "*");
res.status(404).send("No Remix lyrics was found");
}
});
});
});
module.exports = app;
app.listen(3000, () => {
console.log('Server started on port 3000');
});