Creating iTunes playlists with Python 


A Python program to create iTunes playlists; probably not of much use to you without some hacking on it 

I've written a smallish Python program to create iTunes playlists according to particular rules that suit me. You're welcome to the program; you can distribute it under the GPL. But since the particular rules that suit me probably won't suit you exactly, it may not be of much use to you unless you want to hack on it a bit yourself.

There are surely bugs in it and legitimate sorts of data in the music library file that it reads that I haven't anticipated. When bugs have caused it to create a bad playlist file, iTunes has rejected the file. But maybe I've just been lucky and a bad playlist file could do something Very Bad. As with programs in general, use it at your own risk. It cheerfully assumes that your terminal wants text output in UTF-8. That may not matter if you don't have any non-ASCII data in your music library. Bug reports and patches are naturally very welcome.

The only non-standard module it requires is Fredrik Lundh's excellent ElementTree module for manipulating XML.

I wrote the program after going down to my local Apple store and having a look at the iPod nano. It's hard to pick one up and not want one. (Though it was immediately clear to me that the thing would scratch easily and therefore really needs a case.) But practicality stopped me from buying one then. I have a reasonably big music collection: one reason that I like my 60GB iPod so much is that it lets me store my 300-some CDs in the basement. Would an iPod that holds only 4GB be of any use?

I imagined that 4GB ought to hold most or all my current favorites, so I created a playlist with my current favorites on it. It fit comfortably in 4GB, but it turned out to be pretty tedious to listen to. Because they were my favorites, I'd listened to most of them recently. I wanted something I hadn't heard recently pretty often.

It would be possible to create a playlist by hand that contained a bunch of favorites and also some albums I hadn't listened to recently, but it would quickly become a nuisance to maintain. So I wondered if it was possible to have a program create a playlist automatically. And it is.

It turns out that iTunes saves an XML description of your music library in:

    ~/Music/iTunes/iTunes Music Library.xml

That's not iTunes's main data file. As far as I can tell, it's a version that's generated for other programs to read. Changing it doesn't seem to accomplish anything. But iTunes will import playlists that are written in XML. So a program can read the XML version of your music library and generate a playlist based applying whatever rules it likes. And you can then import the playlist.

As far as I'm aware, neither of those file formats is documented publicly. So what the program expects to read and decides to write are based on inspection and experiment. I've very likely missed a few cases. As I mentioned above, bug reports and patches are very welcome.

The program actually creates several playlists, all in one file that's saved on the desktop. It starts with all the music that has been added within the last 60 days and then adds everything that hasn't ever been played. It puts them in a playlist called "AG-Recently Added". It then gets the music that has been played most and puts that in a playlist called "AG-Most Played". Assuming that it hasn't run out of space already, it adds albums from configurable genres chosen quasi-randomly, with a bias toward albums that haven't been played recently. Those playlists are named "AG-" and the genre name.

I still haven't gotten a nano. I'll have to see how I like the playlists that get generated and maybe tweak the rules a bit. 

Posted: Sun - October 2, 2005 at 08:30   Main   Category: 


©