Is it fair to say iTunes' Library.xml situation is a hot mess, representing all the reasons why the early 2000's embrace of xml got replaced by an affection for json? I'll let you be the judge...
I've grown to distrust iTunes, sadly. A month or two ago I noticed one album that I remembered having (Mario Bros the Movie soundtrack... don't judge.) was missing. The other week I realized a King Missile album wasn't there, and today a difficult-to-replace album from a college A Capella group I worked with once (again, don't judge).
Some of these files I can recover from old file backups. Plus I had a copy of iTunes' "Library.xml" (What you get when you goto "File | Library | Export Library") from when I switch my collection to my mac so I should be able to figure out what has flown the coop.
Except look at this xml file excerpt (again, not with the judging of the music choice, ok?)
<dict>
<key>Track ID</key><integer>3853</integer>
<key>Name</key><string>Diggin' Your Scene</string>
<key>Artist</key><string>Smash Mouth</string>
<key>Album Artist</key><string>Smash Mouth</string>
<key>Composer</key><string>Gregory Camp</string>
<key>Album</key><string>Astro Lounge</string>
<key>Genre</key><string>Alternative Pop</string>
<key>Kind</key><string>MPEG audio file</string>
<key>Size</key><integer>3803563</integer>
<key>Total Time</key><integer>190066</integer>
[...]
</dict>
I think the # one thing I don't like about XML is when it relies on node proximity for semantic values, especially since it has a rather rich ability to label metadata as attributes of the like. Having what is roughly a flat list of key tag, value tag (of some kind), key, value tag (of some kind). Wouldn't something like
<dict>
<item key="Track ID" type="integer">3853</item>
</dict>
make more sense? Or if that was too difficult to validate (and heaven knows XML engineers loved their validations) making key an attribute
<dict>
<integer key="Track ID">3853</integer>
</dict>
at least nesting these things into a wrapper, so it's an iterable set?
<dict>
<item><key>Track ID</key><integer>3853</integer></item>
</dict>
Do I just not "get" the magic of XML in this case?
Anyway, I decided to get this into JSON, where I'm most comfortable manipulating things. My first attempt was using sparkbuzz's generic
jQuery-xml2json but it choked pretty hard on Apple's mess, giving me separate piles of keys, integers, and string values. I know you can use JQuery's selectors on xml, so I decided to go that route. Here is my in-progress embedded script (using the old
python miniwebserver trick to serve up the files) - it doesn't yet do the comparison of the old Library contents with the new, but that should be pretty easy with this JSON format:
{
"Track ID": "3853",
"Name": "Diggin' Your Scene",
"Artist": "Smash Mouth",
"Album Artist": "Smash Mouth",
"Composer": "Gregory Camp",
"Album": "Astro Lounge",
"Genre": "Alternative Pop",
"Kind": "MPEG audio file",
"Size": "3803563",
"Total Time": "190066",
[...]
}
Like a breath of fresh air, ain't it?
Anyway, here's the relevant bit of script: (small.xml was my test file, and when I'm ready to gear up to the real files, I'll add multiple calls in the "$.when()" so that files are loaded in parallel and work doesn't begin until all are loaded.
var library = {};
$.when(
$.ajax({
url: 'small.xml',
dataType: 'xml',
success: function(response) {
xmlToLibraryJson("small",response);
}
})
).then(function() {
//just a place holder to show my results
$("#guts").append(JSON.stringify(library,null," "));
});
function xmlToLibraryJson(file,response){
$xml = $(response);//$.parseXML( response );
var lib = [];
$xml.find("key:contains('Tracks')+dict > dict").each(function(i,elem){
var $dict = $(elem);
var dict = {};
$dict.find("key").each(function(j,thiskey){
dict[$(thiskey).text()] = $(thiskey).next().text();
});
lib.push(dict);
});
library[file] = lib;
}
So, that's it - it was a little harder to assemble than it looks, I haven't had to use $.next() and fancy proximity CSS-ish selectors that much before. I still have to accept the fact that if I add in old songs to iTunes, there's virtually no way to get iTunes to not treat them as new songs, and that will absolutely screw up my rolling "new music" Smart Playlists. But it's better than not having my old music at all.