Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Last active July 2, 2019 18:20
Show Gist options
  • Save dnicolson/ef11c74a3ee800df5fd76d5c0f573a2e to your computer and use it in GitHub Desktop.
Save dnicolson/ef11c74a3ee800df5fd76d5c0f573a2e to your computer and use it in GitHub Desktop.
Takes a setlist.fm setlist ID and creates a playlist in iTunes based on the setlist
#!/usr/bin/env node
/* global Application */
/* eslint-disable no-console */
const fetch = require('node-fetch');
const osa = require('osa2');
require('array-flat-polyfill');
const API_KEY = 'd1be89b2-08b8-4b93-9ebd-eeca6544bdbd';
process.chdir(__dirname);
const getSongsFromSetlist = setlistId => {
const req = fetch(`https://api.setlist.fm/rest/1.0/setlist/${setlistId}`, {
headers: { Accept: 'application/json', 'x-api-key': API_KEY },
});
return req
.then(response => response.json())
.then(data => {
return [
data.artist.name,
data.sets.set
.flatMap(set => set.song)
.filter(song => !song.tape)
.map(song => song.name),
];
});
};
const addSongsToMusic = osa((artist, songs) => {
const app = Application('Music');
const destinationPlaylist = app.UserPlaylist().make();
destinationPlaylist.name = artist;
songs.forEach(name => {
const matchedNames = app.playlists.whose({ name: 'Library' })[0].tracks.whose({ name: { _equals: name } });
for (let i = 0; i < matchedNames.length; i++) {
if (matchedNames[i].artist().toLowerCase() === artist.toLowerCase()) {
matchedNames[i].duplicate({ to: destinationPlaylist });
songs = songs.filter(n => n !== name);
}
}
});
return songs;
});
getSongsFromSetlist(process.argv[2]).then(([artist, songs]) => {
addSongsToMusic(artist, songs)
.then(songs => {
if (songs.length) {
console.log('The following songs were not found:');
console.table(songs);
}
})
.catch(console.error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment