iPod iTunesDB Parsing

I spent a couple hours today messing around with parsing the iPod iTunesDB.

Ostensibly, this is for the purpose of writing an Audioscrobbler plugin for syncing the songs played. (iScrobbler does this, but not if you don’t have autosyncing on – since my iPod is pruned from a much larger collection, I have manually manage my songs).

At first glance, there seem to be many options, but neither Mac::iPod::DB nor Mac::iPod::GNUpod support the ‘lastplay’ fields. libitunesdb and Sam Wood’s iPodDB.cpp are out of the running because my C/C++ skills have now faded to nothing.

Anyway, it turns out that GNUpod does support the lastplay field, and after you install the scripts, it also installs the GNUpod::iTunesDB package. Using the tunes2pod.pl as a base, it was pretty straightforward to pull the tracks from the “Recently Played” list and sort by lastplay date.

That’s about as far as I got, so I get output like this:

2005-03-05 18:55:14 - Bloc Party - Plans
2005-03-05 18:47:34 - Bloc Party - Banquet
2005-03-05 18:42:32 - Doves - Black And White Town
2005-03-05 16:29:53 - daft_punk - on_off
2005-03-05 16:22:26 - Bloc Party - Compliments
2005-03-05 16:18:05 - daft_punk - emotion
2005-03-04 21:54:48 - Four Tet - Tangle
...

I’m also calculating percent played based on the next played item to try to insure that it’s over 240s/50%, although I’m not sure exactly how skipped songs are counted, so I’ll have to experiment a bit w/ that.

What’s left to make this actually functional:

  • cron/daemon to check for songs when iPod is newly attached
  • md5sum + date check if iTunesDB should be read (reads are expensive, my 2400 song DB takes about 20s of full CPU usage)
  • queing submissions
  • submitting to Audioscrobbler (I’ll need to get a client id)

I’m probably not going to have time to get to finishing this anytime soon, but in case anyone is working on something similar, here’s what I hacked together today.

Some misc notes:

  • The iPodLinux site has a very comprehensive page on the iTunesDB format (see the discussion as well)
  • A pair of perl scripts that weren’t directly applicable to what I want to do, but are worth mentioning: tunes2html and tunesxml2html that convert iTunesDB and the iTunes Library XML to HTML listings
  • One of the peculiarities of the date format is that its stored in Mac epoch time, which starts at January 1, 1904 instead of January 1, 1970. I couldn’t find the second offset anywhere online (it’s 2082844800 seconds) and I ended up calculating it in JavaScript after not being able to figure out an easy way to do it with Date::Calc:
    var d1 = new Date("January 1, 1904")
    var d2 = new Date("January 1, 1970")
    diff = d2.getTime() - d1.getTime();
    document.write(diff/1000);