From acda732cc57ead57c203c1cde1b040026c4ccaa2 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 15 Jul 2017 18:20:13 -0400 Subject: Update iTunes crawler to use Playlist 2.0 format; now shows convenient error message if iTunes Library XML sharing is not enabled --- src/crawl-itunes.js | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'src/crawl-itunes.js') diff --git a/src/crawl-itunes.js b/src/crawl-itunes.js index 1c678dd..8e07165 100755 --- a/src/crawl-itunes.js +++ b/src/crawl-itunes.js @@ -26,6 +26,10 @@ function getDictValue(dict, key) { return null } +function findChild(grouplike, name) { + return grouplike.items.find(x => x.name === name) +} + async function crawl(libraryXML) { const document = new xmldoc.XmlDocument(libraryXML) @@ -35,7 +39,7 @@ async function crawl(libraryXML) { const trackDicts = tracksDict.children.filter(child => child.name === 'dict') - const result = [] + const resultGroup = {items: []} for (let trackDict of trackDicts) { let kind = getDictValue(trackDict, 'Kind') @@ -68,26 +72,24 @@ async function crawl(libraryXML) { // console.log(`${artist} - ${name} (${album})`) - const group = (arr, title) => arr.find(g => g[0] === title) - - let artistGroup = group(result, artist) + let artistGroup = findChild(resultGroup, artist) if (!artistGroup) { - artistGroup = [artist, []] - result.push(artistGroup) + artistGroup = {name: artist, items: []} + resultGroup.items.push(artistGroup) } - let albumGroup = group(artistGroup[1], album) + let albumGroup = findChild(artistGroup, album) if (!albumGroup) { - albumGroup = [album, []] - artistGroup[1].push(albumGroup) + albumGroup = {name: album, items: []} + artistGroup.items.push(albumGroup) } - albumGroup[1].push([name, location]) + albumGroup.items.push({name, downloaderArg: location}) } - return result + return resultGroup } async function main() { @@ -95,7 +97,32 @@ async function main() { `${process.env.HOME}/Music/iTunes/iTunes Music Library.xml` ) - const library = await readFile(libraryPath) + let library + + try { + library = await readFile(libraryPath) + } catch(err) { + if (err.code === 'ENOENT') { + console.error( + "It looks like you aren't sharing the iTunes Library XML file." + ) + console.error( + "To do that, just open up iTunes, select iTunes > Preferences from " + + "the menu bar, select the Advanced section, enable the " + + "\"Share iTunes Library XML with other applications\" checkbox, and " + + "click on OK." + ) + console.error("Then run the crawl-itunes command again.") + console.error( + "(Or, if you're certain it *is* being shared, you could try " + + "entering the path to the file as an argument to crawl-itunes.)" + ) + process.exit(1) + return + } else { + throw err + } + } const playlist = await crawl(library) -- cgit 1.3.0-6-gf8a5