I think I'm stuck in a bit of a generation gap. My parents generation was (and more or less still is) all about collecting physical items. My kids generation seems to be all about collecting digital items. But my wife and I, the generation in the middle if you will, seems to be a mix of both. I find myself still wanting the physical items (ie. actual books, actual CDs, etc.) but also wanting access to all of them via digital formats.
I find this to be especially true for my music right now. I still like owning the CD (and I hate buying music from iTunes or the like in pure digital format). First, when I own the CD I get the option of listening to it in my car (with the built in CD player as opposed to my iPod adapter). I can listen to it in one of various radios around the house (again with built in CD players). I can listen to it directly in my computer (via the CD player). Or I can rip it through iTunes (or whatever) and get a digital version of the CD any time I want as well -- thereby making it available for all my digital needs as well. Best of all, I don't need an 'authorized' computer to get MY music onto MY mp3 player (try that with a downloaded iTunes song).
So anyway, yes, I still like owning CDs. So much so that I have a bit of a chaos situation going on with my collection (especially since the move last fall). I've tried to add all of my CDs to my iTunes library over time, but I'm pretty sure that there are a few here or there that I've missed...
So what I wanted to do was come up with a way to 1. get my physical CD collection organized, and 2. tell me which CDs I own that I have not yet ripped to mp3 format (so I can do that and make them available for my iPod).
With that in mind, here's how I tackled the problem:
1. iTunes let's me export an XML file containing all the details of my iTunes music library. The file it gives me contains all the songs, album, and track information it has for the things I've added to it. So once I export that, all I really need to do is trim that file down to the unique albums. (you can export your own iTunes library information from the 'file' area of the iTunes program).
2. Since I'm getting back into Perl anyway, I figured I would go ahead and tackle this 'triming' of the XML process with Perl. Here's the dead simple script I threw together for that today (run it with a command like perl music.pl Music.xml at the command prompt):
# parsing/chopping my iTunes XML file
# I just want to get a sorted list of the unique albums I own. The iTunes library lists
# all the songs I have in my collection, and includes the album name associated to each song.
# This program assumes that artist information occurs before album information within the file
# (which is the case in my exported Music.xml file from iTunes)
# this program also asigns the first artist of an album to the entire album (which
# isn't really correct for things like soundtracks and/or compilations, but is good enough for
# what I want right now)
die "please supply the name of your iTunes exported XML file." unless $#ARGV == 0;
open(MUSICXML, $ARGV[0]) or die "Can't open file $ARGV[0]";
my $artist = "";
while ( my $line = <MUSICXML> ) {
# get the lines with artists in them (and their actual name)
if ($line =~ m/<key>Artist</key><string>(.+)</string>/) {
# if they aren't in our artist hash already, let's add them
if (!exists $artist{$1}) {
$artist{$1} = $1;
}
$artist = $1; # set the artist we will associate with this album (if it's the first song from this album we've encountered).
}
# get the lines with ablums in them (and the ablum name)
if ($line =~ m/<key>Album</key><string>(.+)</string>/) {
# if they aren't in our album hash already, let's add them
if (!exists $album{$1}) {
$album{$1} = $artist;
$artist = "";
}
}
}
# now we have all the information we want
# let's just sort our lists and get the size of each
@artist = sort keys %artist;
$artistsize = @artist;
@album = sort keys %album;
$albumsize = @album;
# display the information we gathered about each album
$count = 0;
foreach $name (@album) {
$count++;
print "$count. $name by $album{$name}n";
}
# give some summary information about what we found out
print "$artistsize unique artists in your libraryn";
print "$albumsize unique albums in your libraryn";
Basically the script does just what it says in the comments. It reads my exported XML file line by line and builds out a couple of Hash tables (I use Hash tables because it seems to be more efficient to search for existing keys this way than to loop through an array every time -- otherwise, I pretty much treat each of the Hash values as array items [ie. I ignore the actual value because I really only care about the keys right now]).
Once I have the data I want stored in some Hash tables, I just sort the keys of those tables and then dump out the details...
Since I had it, I also go ahead and dump out the artist information for each album. However, this part of the data is not 100% correct because the script will associate the first artist it finds for an album with the entire album...that's fine most of the time, but things like Soundtracks and compilations will not really be correct. For what I wanted right now though, it doesn't really matter so I didn't spend a lot of time worrying about that minor detail (but yes I did know I was causing it, I just didn't care).
The script really just dumps out the information to the screen (ie. your command window)...what I will probably do next is have it write the information out to a file, probably in CSV format. Then I can import that CSV into Excel, format it nice for printing, and then actually print it out.
Once I have the printed list (in sorted order of course), I can use it to physically organize my CDs. The ones that end up in a pile outside of the sorted stacks, will obviously be the ones missing from my iTunes library...and so then I can digitize them, and rinse and repeat this whole process.
In the end, it should give me everything I want (plus a nice little printed sheet I'll tape to the side of my CD rack for reference). Plus it was a good, simple, project to get my Perl feet wet again. Nice.
posted by Kevin Marshall on 2008-04-06 00:00:00+00