iTunes Library Checker 2.0

I’ve rewritten most of iTunes Library Checker to use some neater import code, it should also be a bit faster. Not quite complete yet, but close to the previous functionality.

 1     #! /usr/bin/env python
 2     
 3     import Foundation
 4     import os
 5     import struct
 6     
 7     library =  os.path.expanduser('~/Music/iTunes/iTunes Music Library.xml')
 8     db = Foundation.NSDictionary.dictionaryWithContentsOfFile_(library)
 9     libpath = Foundation.NSURL.URLWithString_(db[u'Music Folder']).path()
10     
11     tracks = db[u'Tracks'].itervalues()
12     
13     findstr = "find '"+libpath+"' -type f -not -name .aacgained -not -name ._* -not -name .DS_Store | sort"
14     treedata = os.popen(findstr).readlines()
15     
16     missing = []
17     other = []
18     missing_files = []
19     other_files = []
20     surplus = treedata[:]
21     
22     for track in tracks:
23         location = Foundation.NSURL.URLWithString_(track[u'Location']).path()
24         if not os.path.exists(location):
25             missing.append(track)
26             missing_files.append(str(location))
27         else:
28             try:
29                 surplus.remove(str(location)+'\n')
30             except ValueError:
31                 other.append(track)
32                 other_files.append(str(location))
33         
34     missing_files.sort()
35     other_files.sort()
36     surplus.sort()
37     
38     open('surplus.txt','w').writelines(surplus)
39     open('missing.txt','w').writelines(missing_files)
40     open('other.txt','w').writelines(other_files)