Not quite happy with any of the programs I found to identify iTunes duplicates, I spent an hour making my own (beats tracking down duplicates. :-) ) I thought I’d share it with you. It’s my first attempt at using OS X’ ScriptingBridge and written in Python. I have no clue if it runs out of the box, I suppose you should have Developer Tools installed. It’s not the fastest beast either, and Python and iTunes both use 50% CPU. But it gets the job done. :-) If you wonder why I give so many parameters to the track class I should say that I plan on reusing it to do some more iTunes housekeeping. The script will mark all the duplicates with one star. Then I can round them up and delete them afterwards.

import struct
from ScriptingBridge import *

class Track:
   def __init__(self, name, album, artist, size, length, track, path):
     self.name= self.str(name)
     self.album= self.str(album)
     self.artist= self.str(artist)
     self.size = size
     self.length= self.str(length)
     self.track = track
     self.path= self.str(path)

   def str(self, s):
     try:
       return s.encode('utf-8')
     except:
       return "N/A"

   def equals(self, t):
     test = t.name == self.name
     test = test and t.album == self.album
     test = test and t.artist == self.artist
     test = test and t.size == self.size
     test = test and t.length == self.length
     test = test and t.track == self.track
     return test

   def __eq__(self, t):
     return self.equals(t)

iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
lib = iTunes.sources()[0].playlists()[0]
tracks = lib.tracks()
filetracks = lib.elementArrayWithCode_(struct.unpack('>L', 'cFlT')[0])

Tracks = []
Duplicates = []
duplicates = []
for track in filetracks:
  t = Track(track.name(), track.album(), track.artist(), track.size(), track.time(), track.trackNumber(),track.location())
  if(Tracks.__contains__(t)):
    Duplicates.append(t)
    duplicates.append(track)
  else:
    Tracks.append(t)

print len(duplicates)

# Deal with duplicates
for track in duplicates:
   track.setRating_(20)


Good fun, I just found another blog titled Not_an_Artist


I’m a big fan of Amazon EC2 and S3. To be able to use the amount of computing power you need and the amount of storage you need and only pay for what you use is a good way of computing in my view. However, EC2 wasn’t built to run clustered databases like MySQL on, and I’ve had problems finding any service that will let me pay for the capacity I need (cpu time and disk space) and give me a guaranteed query time. Amazon’s SimpleDB seems to be a good step in that direction, and I’m really looking forward to toying with it and hopefully use it for many interesting projects.


Morten

70-200mm 2007-12-12

Morten


I’ve moved my blog and photoblog back home. A few of my other sites might follow. I’m hoping to have them better integrated so. At the moment people will come up to me and say ‘I saw your home page!’ and I’ll ask them which one. Not a good place to be. I’ve been planning an integrated solution for a year without really having had the time for it, but having it back home might actually do some good for those plans. :-)