-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremoveDuplicatesFromPlaylist.js
51 lines (44 loc) · 1.59 KB
/
removeDuplicatesFromPlaylist.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
const { auth } = require('./lib/auth');
const {
wait,
debug,
isEffectiveDuplicate
} = require('./lib/util');
const { buildTrackList } = require('./lib/common');
const { playlistId, removeTracksDryRun, waitDurations } = require('./config');
async function run() {
const spotifyApi = await auth();
const tracks = await buildTrackList(spotifyApi);
const tracksToRemove = [];
for (let trackIndex = 0; trackIndex < tracks.length; trackIndex++) {
if (isEffectiveDuplicate(tracks[trackIndex], tracks.slice(trackIndex + 1))) {
tracksToRemove.push(tracks[trackIndex]);
}
}
for (const track of tracksToRemove) {
debug(`Duplicate track "${ track.name }" by ${ track.artists[0].name }`);
}
debug(`${ tracksToRemove.length } duplicates found.`);
if (!removeTracksDryRun) {
const trackBatches = []
for (let i = 0; i < tracksToRemove.length / 10; i++) {
trackBatches.push(tracksToRemove.slice(i * 10, (i + 1) * 10));
}
for (const batch of trackBatches) {
for (const track of batch) {
debug(`Removing track "${ track.name }" by ${ track.artists[0].name }`);
}
const playlistSnapshotId = (await spotifyApi.getPlaylist(playlistId)).body.snapshot_id
await wait(waitDurations.getPlaylist);
const batchUris = batch.map((track) => ({ uri: `spotify:track:${ track.id }` }))
await spotifyApi.removeTracksFromPlaylist(playlistId, batchUris, {
snapshot_id: playlistSnapshotId
});
await wait(waitDurations.removeTracksFromPlaylist);
}
}
}
run().catch((err) => {
console.error(err);
console.trace(err);
});