-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
358 lines (301 loc) · 16.4 KB
/
functions.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
require('dotenv').config();
const { removeBackground } = require("@imgly/background-removal-node");
const Vibrant = require('node-vibrant');
const SpotifyWebApi = require('spotify-web-api-node');
const imgbbUploader = require("imgbb-uploader");
const { CLIENT_ID, CLIENT_SECRET } = process.env;
const spotifyApi = new SpotifyWebApi({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
function isWhiteColor(vibrantColor) {
const red = parseInt(vibrantColor.substring(1, 3), 16);
const green = parseInt(vibrantColor.substring(3, 5), 16);
const blue = parseInt(vibrantColor.substring(5, 7), 16);
return red === 255 && green === 255 && blue === 255;
}
async function getVibrantColor(imageUrl) {
return new Promise((resolve, reject) => {
Vibrant.from(imageUrl).getPalette((err, palette) => {
if (err) {
reject(err);
} else {
const vibrantColor = palette.Vibrant ? palette.Vibrant.getHex() : null;
resolve(vibrantColor);
}
});
});
}
function getArtistsImage() {
return new Promise((resolve, reject) => {
spotifyApi.clientCredentialsGrant().then(
function (data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
spotifyApi.setAccessToken(data.body['access_token']);
let ids = ["53XhwfbYqKCa1cC15pYq2q", "1Xyo4u8uXC1ZmMpatF05PJ", "06HL4z0CvFAxyc27GXpf02", "4q3ewBCX7sLwd24euuV69X", "246dkjvS1zLTtiykXe5h60", "7dGJo4pcD2V6oG8kP0tJRR", "3TVXtAsR1Inumwj472S9r4", "1uNFoZAHBGtllmzznpCI3s", "1bAftSH8umNcGZ0uyV7LMg", "2LRoIwlKmHjgvigdNGBHNo", "716NhGYqD1jl2wI1Qkgq36", "52iwsT98xCoGgiGntTiR7K", "1vCWHaC5f2uS3yhpwWbIA6", "224rbIjYbXaTI7lnP2ZMNJ", "1DxLCyH42yaHKGK3cl5bvG", "0jeYkqwckGJoHQhhXwgzk3", "0Y5tJX1MQlPlqiwlOH1tJY", "7uXKIO6VDeOCo6ImWZpZJn", "0DjGDEVSQsodFbL1bMVPRs", "5Uox3n7m4W2CoM9MmHPJwQ", "2UZIAOlrnyZmyzt1nuXr9y", "3vQ0GE3mI0dAaxIMYe5g7z", "0Q8NcsJwoCbZOHHW63su5S", "2O8vbr4RYPpk6MRA4fio7u"]
spotifyApi.getArtists(ids)
.then(function (data) {
const artistsData = data.body.artists.map(artist => {
return {
name: artist.name,
image: artist.images[0].url
};
});
// Dividir los artistas en dos conjuntos
const first17Artists = artistsData.slice(0, 17);
const restOfArtists = artistsData.slice(17);
// Devolver ambos conjuntos de datos
resolve({ first17Artists, restOfArtists });
})
.catch(reject);
})
.catch(reject);
});
}
function generateMatrix(rows, cols, artistsData) {
let cont = 0;
const matriz = [];
for (let i = 0; i < rows; i++) {
const fila = [];
for (let j = 0; j < cols; j++) {
if (i % 2 === 0) {
fila.push(j % 2 === 0 ? 'vacio' : artistsData[cont++]);
} else {
fila.push(j % 2 === 0 ? artistsData[cont++] : 'vacio');
}
}
matriz.push(fila);
}
return matriz;
}
// async function performBackgroundRemoval(imagePath) {
// try {
// const resultBlob = await removeBackground(imagePath);
// const resultBuffer = Buffer.from(await resultBlob.arrayBuffer());
// const base64String = resultBuffer.toString('base64');
// const options = {
// apiKey: process.env.IMGBB_API_KEY, // MANDATORY
// expiration: 300,
// base64string: base64String, // OPTIONAL: pass an URL to imgBB (max 32Mb)
// };
// return imgbbUploader(options)
// .then((response) => {
// return response.display_url;
// })
// .catch((error) => {
// console.error(error);
// return imagePath;
// });
// } catch (error) {
// console.error("Error al intentar eliminar el fondo:", error);
// throw error;
// }
// }
async function performBackgroundRemoval(imagePath) {
try {
const resultBlob = await removeBackground(imagePath);
const resultBuffer = Buffer.from(await resultBlob.arrayBuffer());
const dataUri = `data:image/png;base64,${resultBuffer.toString('base64')}`;
return dataUri;
} catch (error) {
console.error("Error al intentar eliminar el fondo:", error);
throw error;
}
}
function getPlaylist(url) {
const tracksInfo = []
return new Promise((resolve, reject) => {
function getDuration(milisegundos) {
const horas = Math.floor(milisegundos / 3600000);
const minutos = Math.floor((milisegundos % 3600000) / 60000);
const segundos = Math.floor((milisegundos % 60000) / 1000);
return {
horas,
minutos,
segundos
};
}
function getPlaylistId(url) {
const expresionRegular = /playlist\/([a-zA-Z0-9]+)/;
const coincidencia = url.match(expresionRegular);
if (coincidencia && coincidencia[1]) {
return coincidencia[1];
} else {
return null;
}
}
spotifyApi.clientCredentialsGrant().then(
function (data) {
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.getPlaylist(getPlaylistId(url))
.then(async function (data) {
// Obtén el color vibrante de la imagen de la playlist
const vibrantColor = await getVibrantColor(data.body.images[0].url);
const isWhite = isWhiteColor(vibrantColor);
let textColor = ""
if (isWhite) {
textColor = "#000000";
} else {
textColor = "FFFFFF";
}
playlistInfo = {
name: data.body.name,
description: data.body.description,
url: data.body.external_urls.spotify,
image: data.body.images[0].url,
imagePng: "",
color: vibrantColor,
text: textColor,
owner: {
name: data.body.owner.display_name,
url: data.body.owner.external_urls.spotify
},
followers: data.body.followers.total,
totalSongs: data.body.tracks.total,
totalDuration: 0
};
}, function (err) {
reject('Something went wrong while getting playlist information: ' + err);
});
let offset = 0;
let totalDuration = 0;
function getPlaylistTracksRecursive(offset) {
spotifyApi.getPlaylistTracks(getPlaylistId(url), {
offset: offset,
limit: 100
}).then(
async function (data) {
let tracks = data.body.items;
let cont = offset;
if (tracks.length > 0) {
tracks.forEach(async (element, index) => {
if (element.track && element.track.name) {
const artistInfoArray = [];
let artists = element.track.artists;
artists.forEach(artist => {
artistInfoArray.push({
id: artist.id,
name: artist.name
});
});
const imageUrl = element.track.album.images[0] && element.track.album.images[0].url ? element.track.album.images[0].url : 'images/album.jpg';
let trackInfo = {
name: element.track.name,
duration_ms: element.track.duration_ms,
url: element.track.external_urls.spotify,
album: {
name: element.track.album.name,
url: element.track.album.external_urls.spotify,
image: imageUrl
},
preview_url: element.track.preview_url,
artists: artistInfoArray
};
tracksInfo.push(trackInfo);
totalDuration += element.track.duration_ms;
cont++;
}
});
getPlaylistTracksRecursive(offset + 100);
} else {
const uniqueArtists = new Set();
tracksInfo.forEach(track => {
track.artists.forEach(artist => {
if (artist && artist.id) {
uniqueArtists.add(artist);
}
});
});
const uniqueArtistsArray = Array.from(uniqueArtists);
const fullArtists = [];
function getArtistsInfoInChunks(startIndex) {
const chunkSize = 50;
const endIndex = Math.min(startIndex + chunkSize, uniqueArtistsArray.length);
const chunkOfArtists = uniqueArtistsArray.slice(startIndex, endIndex);
return spotifyApi.getArtists(chunkOfArtists.map(artist => artist.id))
.then(function (data) {
data.body.artists.forEach(artist => {
fullArtists.push(artist);
});
if (endIndex < uniqueArtistsArray.length) {
return getArtistsInfoInChunks(endIndex);
}
})
.catch(function (err) {
console.error('Error getting artists information:', err);
reject('Error getting artists information: ' + err);
});
}
getArtistsInfoInChunks(0)
.then(async () => {
const totalSongs = tracksInfo.length;
const detailedArtistInfo = {};
fullArtists.forEach(artist => {
const artistSongs = tracksInfo.filter(track => track.artists.some(a => a.id === artist.id));
const percentage = (artistSongs.length / totalSongs) * 100;
// Check if the artist has an image and the 'url' property is defined
const imageUrl = artist.images[0] && artist.images[0].url ? artist.images[0].url : 'images/avatar.webp';
detailedArtistInfo[artist.id] = {
artist: {
name: artist.name,
url: artist.external_urls.spotify,
image: imageUrl,
followers: artist.followers.total
},
songs: artistSongs,
percentage: percentage.toFixed(2)
};
});
const sortedDetailedArtistInfo = Object.entries(detailedArtistInfo)
.sort(([, a], [, b]) => b.percentage - a.percentage)
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
// Perform background removal on the artist's image for the first artist
const firstArtistId = Object.keys(sortedDetailedArtistInfo)[0];
const firstArtist = sortedDetailedArtistInfo[firstArtistId];
if (firstArtist && firstArtist.artist && firstArtist.artist.image) {
try {
const removalResult = await performBackgroundRemoval(firstArtist.artist.image);
// Convert the background-removed image to a data URI
// Replace the artist's image with the data URI
firstArtist.artist.imagePng = removalResult;
} catch (error) {
console.error('Error performing background removal on artist image:', error);
}
}
playlistInfo.totalDuration = getDuration(totalDuration)
resolve({
playlistInfo: playlistInfo,
detailedArtistInfo: sortedDetailedArtistInfo
});
})
.catch(function (err) {
console.error('Error getting artists information:', err);
reject('Error getting artists information: ' + err);
});
}
},
function (err) {
console.error('Something went wrong:', err);
reject('Error getting playlist tracks: ' + err);
}
);
}
getPlaylistTracksRecursive(offset);
},
function (err) {
console.error('Something went wrong when retrieving an access token', err);
reject('Error retrieving access token: ' + err);
}
);
});
}
// Exportar funciones para ser utilizadas en otros archivos
module.exports = {
getArtistsImage,
generateMatrix,
getPlaylist
};