ITunes Library Persistent ID Editor

Beat_Slayer 0 Tallied Votes 1K Views Share

ITunes Library Persistent ID Editor.

Sample usage:

fxml = 'iTunes Music Library.xml'    # path to xml file
fitl = 'iTunes Library.itl'          # path to itl file

itk = ITunesLibKeys(fxml, fitl)

print itk.file_xml                   # prints path to xml file
print itk.file_itl                   # prints path to itl file
print itk.xml_key                    # prints key on xml
print itk.itl_key                    # prints key on itl
print itk.valid_key                  # prints True if keys match

itk.new_user_key()                   # ask for user input key

itk.xml_key_replace(itk.itl_key)     # replace key on xml file with the itl key

itk.itl_key_replace(itk.new_key)     # replace key on itl with user input key
fxml = 'iTunes Music Library.xml'       # path to xml file
fitl = 'iTunes Library.itl'             # path to itl file

itk = ITunesLibKeys(fxml, fitl)

if not itk.valid_key:                      # if keys don't match
    itk.xml_key_replace(itk.itl_key)       # replace xml key with the itl key
import binascii

class ITunesLibKeys():
    def __init__(self, xml_file, itl_file):
        self.file_xml = xml_file
        self.file_itl = itl_file
        self.xml_key = self.xml_key_find()
        self.itl_key = self.itl_key_find()
        self.valid_key = self.key_compare()
        self.new_key = ''

    def xml_key_find(self):
        f_x = open(self.file_xml)
        f_xml = f_x.readlines()
        f_x.close()    
        for item in f_xml:
            if item.find('<key>Library Persistent ID</key>') != -1:
                before_xml, tag_before, rest_xml = str(item).partition('<key>Library Persistent ID</key><string>')
                key_xml, tag_after, after_xml = rest_xml.partition('</string>')
        return key_xml

    def itl_key_find(self):
        f_i = open(self.file_itl, 'rb')
        f_itl_bin = f_i.read()
        f_i.close()
        f_itl_hex = str(binascii.hexlify(f_itl_bin)).upper()
        return f_itl_hex[104:120]

    def key_compare(self):
        if self.xml_key == self.itl_key:
            return True
        else:
            return False

    def new_user_key(self):
        while len(self.new_key) != 16:
            self.new_key = raw_input('Enter the new key:').upper()
        
    def xml_key_replace(self, sel_key):
        f_x = open(self.file_xml)
        f_xml = f_x.read()
        f_x.close()    
        f_xml = f_xml.replace(self.xml_key, sel_key)
        f_x = open(self.file_xml, 'w')
        f_x.write(f_xml)
        f_x.close()   

    def itl_key_replace(self, sel_key):
        f_i = open(self.file_itl, 'rb')
        f_itl_bin = f_i.read()
        f_i.close()
        f_itl_hex = str(binascii.hexlify(f_itl_bin)).upper()
        f_itl_hex = f_itl_hex.replace(self.xml_key, sel_key)
        f_i = open(self.file_itl, 'wb')
        f_itl_bin = binascii.unhexlify(f_itl_hex)
        f_i.write(f_itl_bin)
        f_i.close()