#!/usr/bin/python import sys, os, re, struct DEBUGGING = True CURRENT = {'db': '', 'title': '', 'location': '', 'album': '', 'artist': ''} def main(itunesdb): db = readall(itunesdb) CURRENT['db'] = itunesdb i = 0 while i < len(db): tag = db[i:i + 4] taglength = struct.unpack(' 40: strlen = struct.unpack(' 40: strlen = struct.unpack(' 40: strlen = struct.unpack(' 40: strlen = struct.unpack('>sys.stderr, 'reached end of songs, found playlist' sys.exit(0) else: debug('skipping tag %s' % tag) i += taglength def debug(message): if DEBUGGING: print >>sys.stderr, message def readall(filename): input = open(filename, 'rb') data = input.read() input.close() return data def translate(path): debug('translating %s into real path' % path) basedir = os.path.dirname(os.path.dirname(os.path.abspath(CURRENT['db']))) subdir = path.replace(':', os.sep) basedir, check = os.path.split(basedir) if check != subdir.split(os.sep)[1]: raise Exception, 'directory mismatch' else: debug('joining %s to %s' % (basedir, subdir)) dirname = os.path.join(basedir, subdir.lstrip(os.sep)) debug(('dirname', dirname)) return dirname def symlink(path, title): debug('linking %s to %s' % (path, title)) outfile = os.path.join(CURRENT['artist'], CURRENT['album'], CURRENT['title']) outdir = '/tmp/ipod' if not os.path.isdir(outdir): os.mkdir(outdir) outpath = os.path.join(outdir, outfile) + os.path.splitext(path)[1] if not os.path.isdir(os.path.dirname(outpath)): os.makedirs(os.path.dirname(outpath)) debug('output to %s' % outpath) try: os.symlink(path, outpath) except OSError: debug('ignoring duplicate') pass if __name__ == '__main__': main((sys.argv + [os.path.join( os.getenv('HOME', '/var/mobile'), 'Media/iTunes_Control/iTunes/iTunesDB')])[1])