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!

3 Responses to “iTunes, Perl, and You”


  1. 1 iTunes Music Blog

    I cannot say how right you are!!! These other people have failed to see your point. I am going to put a link on my blog back to you ok?

  2. 2 S Yanoff

    PERL is so awesome for this type of thing, especially it’s associative arrays. As I didn’t want to install PERL for gathering iTunes stats, I wrote one in VB Script (which uses the concept of a Dictionary object which is like PERL’s associate arrays).

    It basically spits out a bunch of numbers and lists to a date-stamped file and it takes about a minute to run for every 1,000 songs you have. For me, it takes just over 3 minutes to run on my aging home PC (with nearly 5,000 songs).

    You can save it anywhere on your PC and double-click on it and it should run. You may need to have VB.NET installed, so you can go to the Microsoft Updates site for that. I am VERY interested in feedback on this, and I’d welcome enhancements to the script as well.

    You can get information and download it from:
    http://yanoff.org/music/iTunesStats.shtml

    Cheers,
    -Scott

  3. 3 askedrelic

    Looks good Scott, although I happen to only run OSX, so no VBS script support for me. If you had to download and install a language anyways, why not use one that runs on multiple platforms!

Leave a Reply