Kevin Nelson Marshall
Other entries:
« Somethings just seem to stick.

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

Subscribe »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »

Blog Details »

This blog now includes 286 wonderfully exciting posts from 1 unique and very special writer!


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk me on »

Twitter (@falicon) »
Delicious »
Digg »
Disqus »
Facebook »
Flickr »
FriendFeed »
Last.fm »
LinkedIn »
StumbleUpon »

Archives by Category »

(24) Code »
(5) ColdFusion »
(11) Database »
(7) Factor »
(286) General »
(9) JavaScript »
(15) Perl »
(13) PHP »
(17) Ruby »

Archives by Month »

(1) February 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0

Fantasy focused domains »

draftwizard.com
fantasy-football-draft.com
fantasyfootballkit.com
fantasyfootballquiz.com
hockeynotes.com
pegg.it
rosterhelp.com
sportsxml.com
statsfeed.com
supermug.com

Tech. focused domains »

factorcode.com
perlquiz.com
simpledb.info

Social Tool focused domains »

conversationlist.com
friendstat.us
fuzzypop.com
gawk.it
grou.pe
halfbite.com
jivegas.com
pu.ly
tagli.st
timelylinks.com
tym.ly
wow.ly

Utility focused domains »

fubnub.com

Other domains »

betaread.com
botfu.com
falicon.com
storyrank.com

Not yet live domains »

bar.ackoba.ma
basketballnotes.com
buddydirt.com
budrank.com
cakntoba.com
coachwizard.com
cointhief.com
ezbcs.com
falconsrule.com
fantasydeke.com
fantasyfootballrank.com
ffkit.com
footballnotes.com
footballpublishing.com
giggletweet.com
greentile.com
herobrawl.com
kacode.com
kickasscode.com
knowabout.it
leaguewizard.com
nfldraftnews.com
pa.ly
rorbe.com
slidepitch.com
startfail.com
survivorhub.com
tagli.st
thedfl.com
thescoutsreport.com
toptenify.com
tripacation.com
tweetwiki.com
umock.com

* Yes I realize I have a bit of an addiction to domain names, but I really do have specific ideas for each of the above.



This blog is powered by KickAssCode.