Re-creating multiple iTunes XML files from historical ITL files

Now that I have retrieved all the historical .itl files of interest, I need to convert the .itl files to XML so that I can parse them. The easiest way is to have iTunes do this for me. If I open iTunes

The first thing I need to do is create a second iTunes library directory to prevent my current library from being corrupted during this process. Close iTunes, then re-open it while holding down the ‘option’ key. You should see a window prompting you to create a new library. Do this and give it an easy name (I used ‘ITLConversion’).

Next, for each backup file, I need to copy the file to the right directory, open iTunes, let it parse the file, close iTunes, and retrieve the created .xml file.

The first time you open one of your historical files, iTunes will collect album artwork for all the other media on your computer… this will take a minute or two. Once this is done remaining files will only take a second or two each to load.

Note: I strongly recommend you turn off networking and disconnect any iPods or other slave devices to speed up the process. Having iTunes try to back up all your devices each time it starts is going to waste time unnecessarily.

The steps to follow for each file are:

  1. move the .itl file to overwrite ~/Music/ITLConversion/iTunes Library.itl
  2. start iTunes
  3. wait until the .xml file has been created
  4. quit iTunes
  5. move the .xml file to ~/Music/iTunes/Previous iTunes Libraries/iTunes Library .xml

As before, I start by including the necessary packages:

import getpass, os, shutil, signal, subprocess, time

Also, set up handy constants and strings:

start_itunes = [ "/Applications/iTunes.app/Contents/MacOS/iTunes" ]

username = getpass.getuser()

src_directory = "/Users/{}/Music/iTunes/Previous iTunes Libraries".format(username)
interim_filename = "/Users/{}/Music/ITLConversion/iTunes Library".format(username)
dest_filename_format = "iTunes Library {}.xml"

Retrieve a list of all the .itl files in the Previous iTunes Libraries folder:

backups = os.listdir(src_directory)
backups = [ x for x in backups if x.endswith('.itl') and not x.startswith('.') ]

Remove any existing .xml file in the conversion directory:

try:
    os.remove("{}.xml".format(interim_filename))
except:
    pass

Iterate over files performing the steps outlined above:

for f in backups:
    date_part = f[15:25]
    src_filename = "{}/{}".format(src_directory, f)
    interim_itl_filename = "{}.itl".format(interim_filename)
    print "copy from {} to {}".format(src_filename, interim_itl_filename)
    shutil.copyfile(src_filename, interim_itl_filename)
    interim_xml_filename = "{}.xml".format(interim_filename)
    itunes_proc = subprocess.Popen(start_itunes)
    while not os.path.exists(interim_xml_filename):
        time.sleep(1)
    time.sleep(5)
    os.kill(itunes_proc.pid, signal.SIGINT)
    dest_filename = "{}/{}".format(src_directory, dest_filename_format.format(date_part))
    print "move from  {} to {}".format(interim_xml_filename, dest_filename)
    shutil.move(interim_xml_filename, dest_filename)