60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
const fs = require('fs/promises');
|
|
const path = require('path');
|
|
const { setupSpotify, spotifyApi } = require(path.join(__dirname, 'bits', 'spotify'));
|
|
|
|
const selectionFile = process.argv[4]
|
|
? path.resolve(process.argv[4])
|
|
: path.join(__dirname, 'track-selection.json');
|
|
|
|
const operation = process.argv[2];
|
|
if (!operation || !['create', 'add'].includes(operation)) {
|
|
console.error('Invalid operation! Operations: create, add');
|
|
process.exit(1);
|
|
}
|
|
|
|
const playlist = process.argv[3];
|
|
if (!playlist) {
|
|
console.error('No playlist ID or name provided');
|
|
process.exit(1);
|
|
}
|
|
|
|
async function readSelection() {
|
|
let spotifyUris = [];
|
|
let chunkedUris = [];
|
|
let discardFiles = [];
|
|
let total = 0;
|
|
|
|
const content = JSON.parse(await fs.readFile(selectionFile));
|
|
content.forEach((item) => {
|
|
spotifyUris.push(item[1]);
|
|
discardFiles.push(item[0]);
|
|
total++;
|
|
});
|
|
|
|
if (spotifyUris.length >= 100) {
|
|
while (spotifyUris.length >= 100) {
|
|
chunkedUris.push(spotifyUris.splice(0, 100));
|
|
}
|
|
}
|
|
|
|
if (spotifyUris.length) {
|
|
chunkedUris.push(spotifyUris);
|
|
}
|
|
|
|
await setupSpotify();
|
|
let playlistId = playlist;
|
|
|
|
if (operation === 'create') {
|
|
const newPlaylist = await spotifyApi.createPlaylist(playlist, { public: false });
|
|
playlistId = newPlaylist.body.id;
|
|
}
|
|
|
|
for (const uris of chunkedUris) {
|
|
await spotifyApi.addTracksToPlaylist(playlistId, uris);
|
|
}
|
|
|
|
console.log(operation === 'create' ? 'Created a playlist with' : 'Added', total, 'tracks');
|
|
}
|
|
|
|
readSelection().catch(console.error);
|