-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
247 lines (218 loc) · 6.07 KB
/
server.js
File metadata and controls
247 lines (218 loc) · 6.07 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const express = require("express");
const axios = require("axios");
const savetube = require("ytdl-xl");
const path = require("path");
const cors = require("cors");
const os = require("os");
const fs = require("fs");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const TOKEN_URL = "https://restapi-v2.vercel.app/api/other/spotify-token";
const getAccessToken = async () => {
try {
const response = await axios.get(TOKEN_URL);
return response.data.response.access_token;
} catch (error) {
console.error("Error fetching token:", error.response?.data || error.message);
return null;
}
};
const extractSpotifyID = (url) => {
const match = url.match(/\/track\/([a-zA-Z0-9]+)/);
return match ? match[1] : null;
};
const getMetadata = async (trackId) => {
const token = await getAccessToken();
const response = await axios.get(`https://api.spotify.com/v1/tracks/${trackId}`, {
headers: { Authorization: `Bearer ${token}` }
});
const track = response.data;
return {
title: track.name,
artist: track.artists.map(artist => artist.name).join(", "),
album: track.album.name,
duration_ms: track.duration_ms,
cover: track.album.images[0]?.url || null,
spotify_url: track.external_urls.spotify
};
};
app.get("/api/spotify/metadata", async (req, res) => {
const url = req.query.url;
if (!url) return res.status(400).json({
status: false,
code: 400,
error: "Masukkan parameter ?url=spotify_url"
});
const trackId = extractSpotifyID(url);
if (!trackId) return res.status(400).json({
status: false,
code: 400,
error: "URL tidak valid"
});
try {
const metadata = await getMetadata(trackId);
res.json({
status: true,
code: 200,
result: metadata
});
} catch (error) {
res.status(500).json({
status: false,
code: 500,
error: error.message
});
}
});
app.get("/api/spotify/download", async (req, res) => {
const url = req.query.url;
if (!url) return res.status(400).json({
status: false,
code: 400,
error: "Masukkan parameter ?url=spotify_url"
});
const trackId = extractSpotifyID(url);
if (!trackId) return res.status(400).json({
status: false,
code: 400,
error: "URL tidak valid"
});
try {
const metadata = await getMetadata(trackId);
const query = metadata.title;
const search = await savetube.search(query);
const data = search.result[1];
const download = await savetube.download(data.url, "mp3");
const result = download.result.download;
res.json({
status: true,
code: 200,
result: {
metadata: metadata,
download: result
}
})
} catch (error) {
res.status(500).json({
status: false,
code: 500,
error: error.message
});
}
});
app.get("/api/spotify/play", async (req, res) => {
const { q } = req.query;
if (!q) return res.status(400).json({
status: false,
code: 400,
error: "Masukkan parameter ?q=sweater wheater"
});
const token = await getAccessToken();
if (!token) {
return res.status(500).json({
status: false,
code: 500,
error: "Gagal mendapatkan Access Token"
});
}
try {
const keywords = encodeURIComponent(q);
const response = await axios.get(`https://api.spotify.com/v1/search`, {
params: { q: keywords, type: "track", limit: 20 },
headers: { Authorization: `Bearer ${token}` },
});
if (!response.data.tracks.items.length) {
return res.status(404).json({
status: false,
code: 404,
error: "Tidak ada hasil yang ditemukan untuk query tersebut."
});
}
const tracks = response.data.tracks.items.map((track) => ({
artist: track.artists.map((artist) => artist.name).join(", "),
title: track.name,
url: track.external_urls.spotify,
}));
const match = tracks[0];
const trackId = extractSpotifyID(match.url);
if (!trackId) return res.status(400).json({
status: false,
code: 400,
error: "URL tidak valid"
});
const metadata = await getMetadata(trackId);
const search = await savetube.search(`${metadata.artist} - ${metadata.title}`);
const data = search.result[0];
const download = await savetube.download(data.url, "mp3");
const result = download.result.download;
res.json({
status: true,
code: 200,
result: {
metadata: metadata,
download: result
}
});
} catch (error) {
res.status(500).json({
status: false,
code: 500,
error: error.message
});
}
});
app.get("/api/spotify/search", async (req, res) => {
const { q } = req.query;
if (!q) return res.status(400).json({
status: false,
code: 400,
error: "Masukkan parameter ?q=sweater wheater"
});
const token = await getAccessToken();
if (!token) {
return res.status(500).json({
status: false,
code: 500,
error: "Gagal mendapatkan Access Token"
});
}
try {
const keywords = encodeURIComponent(q);
const response = await axios.get(`https://api.spotify.com/v1/search`, {
params: { q: keywords, type: "track", limit: 10 },
headers: { Authorization: `Bearer ${token}` },
});
if (!response.data.tracks.items.length) {
return res.status(404).json({
status: false,
code: 404,
error: "Tidak ada hasil yang ditemukan untuk query tersebut."
});
}
const tracks = response.data.tracks.items.map((track) => ({
id: track.id,
artist: track.artists.map((artist) => artist.name).join(", "),
title: track.name,
release_date: track.album.release_date,
images: track.album.images[0].url,
url: track.external_urls.spotify,
}));
res.json({
status: true,
code: 200,
result: tracks
})
} catch (error) {
res.status(500).json({
status: false,
code: 500,
error: error.message
});
}
})
const PORT = 1204;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});