Monthly Archive for September, 2008

iTunes, Perl, and You

I was updating my Facebook profile tonight and wanted to fill in the “Favorite Music” section with some real data from iTunes, considering I listen to 99% of my music through iTunes, my iPod, or iPhone, which means all of my stats get tracked through iTunes. Creating a Smart Playlist in iTunes of my most played music would be adequate, but I don’t know of a way to that data out of iTunes without copying and pasting, a boring and time waste experience, so I turned to Perl.

A friend of mine, Drew, maintains a set of Perl scripts on CPAN to parse and help analyze your iTunes library XML file, aptly called Mac-iTunes-Library.

I installed the package via CPAN:

install 'Mac::iTunes::Library'

Then coded up up a script to pull the top 50 artists based on the number of tracks. Assuming I listen to these tracks, it’s probably a good statistic that I like this artist.

#!/usr/bin/env perl                                                         
use strict;                                                                 
use warnings;                                                               
 
use Mac::iTunes::Library;                                                   
use Mac::iTunes::Library::XML;                                              
 
#full filesystem url of your itunes xml file
my $library = Mac::iTunes::Library::XML->parse('iTunes Music Library.xml');
#hash of <Artist, numTracks>
my %artists = $library->artist();
my @sortedArtists = ();
my $count = 0;
my $artist;
 
#sort function
sub numerically {
        $artists{$b} <=> $artists{$a};
}
 
#sort artists by the number of tracks, then grab the top 50
for my $artist (sort numerically (keys %artists)) {
    if( $count < 50) {
        push(@sortedArtists, $artist);
    }
    $count++;
}
 
#Now take the top 50 artists sorted by number of tracks,
#and sort alphabetically, and print
foreach $artist (sort(@sortedArtists)) {
    print "$artist, ";
}
 
print "\n";

Now, I’m a beginner with Perl, but from a quick parse of the results, there are still some case-sensitivity issues that I’m looking into; “Boards of Canada” and “Boards Of Canada” are seperate artists, but that’s also a problem with iTunes not caring much about case either when saving data. With some minor formatting to the output, you should be rewarded with a list something like this:

311, Aphex Twin, Armin Van Buuren, BT, Bear McCreary, Beck, Cake, DJ Miko & Mini Me, Daft Punk, Deep Forest, Depeche Mode, Dieselboy, Eels, Eminem, Foo Fighters, Frank Klepacki, Hybrid, Incubus, Infected Mushroom, Jack Johnson, Johnny Cash, Juno Reactor, Korn, Machinae Supremacy, Massive Attack, Metallica, Moby, Muse, Nine Inch Nails, Orbital, Pink Floyd, Placebo, Prodigy, Queen, Queens of the Stone Age, Radiohead, Rammstein, Scooter, Stabbing Westward, System of a Down, The Chemical Brothers, The Crystal Method, The Offspring, The Red Hot Chili Peppers, The Smashing Pumpkins, The Strokes, Tiesto, Weezer, Yo La Tengo, Yoko Kanno

Hurray data!

Back To School

Alot can change in a month and it hasn’t even been a full month since I last wrote! Lets recap.

SFO-NYC

Following WordCamp, I had my last week of internship. I said my goodbyes and got some great advice before leaving. I went to a wedding in Los Gatos and reunited with some friends college. What stuff I couldn’t carry with me on the plane, I packed into boxes and mailed out. I dropped off my bike at my bike shop and had them deal with mailing. Then I was off, a Sunday morning around 7am. Flying Virgin America across the country was one of the best experiences I’ve had flying in a long time.

NYC

I spent about 4 days wandering around NYC once again. It had been awhile and I missed the city. I stopped at the Met, Guggenheim, and MoMA for some cultural experiences, while literally just wandering the rest of the time. I ate all my favorite food again and loved it. I had some beer in the East Village. I read a book in Battery Park for a few hours and took in the sun. NYC is a lovely city.

Rochester

Rochester is not a lovely city. I try not to be too scathing in my review of this city, but the longer I spend here, the more miserable I get. My work ethic toward has improved greatly, mainly in part to not wanting to go out at all. I’ve eaten everywhere, seen all the hobos and landmarks, and driven the streets too many times. 6 months of 70 degree, sunny California weather makes me happy. 6 months of snow, rain, and freezing weather doesn’t.

It’s week 3 of classes currently and I’m chugging along. I’m doing some good programming in C, finishing up some personal work from the summer, and ready to move on. I have a full year left at RIT, I’m going to make the most of it, but I will be very glad when I’m done.