# Song versions in iTunes

During lunch today, I found myself looking at my iTunes library sorted by song name and noticed that I had five versions of “Baby Please Don’t Go.”1 It got me wondering which songs in my library were repeated the most, so I wrote a short program to find out.

Since I hate programming in AppleScript and I knew that Python’s dictionaries (known as hashes or associative arrays in other languages) were the perfect way to collect the song counts, I decided to use the appscript library. This gave me AppleScript-like access to iTunes from within Python. Here’s what I came up with.

 1:  #!/usr/bin/python
2:
3:  import appscript
4:
5:  tracks = appscript.app('iTunes').playlists['Music'].tracks.get()
6:
7:  counts = {}
8:  for t in tracks:
9:      n = t.name.get().title()
10:      if counts.has_key(n):
11:          counts[n] += 1
12:      else:
13:          counts[n] = 1
14:
15:  repeaters = [(v,k) for (k,v) in counts.items() if v >= 5]
16:
17:  repeaters.sort()
18:  repeaters.reverse()
19:
20:  for i in repeaters:
21:      print '%2d copies of %s' % i


It’s a pretty simple script, and it wouldn’t surprise me to find that there are ways to make it shorter and/or more efficient, but it was quick to write. Line 5 grabs all the tracks in the Music Library. Lines 7—13 create a dictionary where the keys are the song titles and the values are the number of times the song title appear. To insure that differences in capitalization aren’t counted as differences in song titles, Line 9 converts all the titles to titlecase (all words start with a capital letter). Line 15 creates a list of tuples from that dictionary in (count, title) order where the count is 5 or more. Putting the tuples in (count, title) order allows Line 17 to sort the list by count. Line 18 reverses the list to put the most repeated song at the top. Lines 20 and 21 print out the results.

Here’s what came out of my library:

8 copies of Silent Night
8 copies of All Along The Watchtower
7 copies of White Christmas
7 copies of Knockin' On Heaven'S Door
6 copies of Jingle Bells
6 copies of Hold On
5 copies of The Times They Are A-Changin'
5 copies of The Christmas Song
5 copies of Steppin' Out
5 copies of Respect
5 copies of Mean Old World
5 copies of Love Me Do
5 copies of Let It Be
5 copies of I'M A Man
5 copies of Have You Ever Loved A Woman
5 copies of Gloria
5 copies of Born Under A Bad Sign
5 copies of Baby Please Don'T Go
5 copies of Across The Universe


The results show two things:

1. I have a lot of Christmas music.
2. Python’s title() method doesn’t know anything about apostrophes.

In many cases, the copies are not actually different versions of the song, they’re the same version by the same artist but coming from different albums. For example, of the eight copies of “All Along The Watchtower,” three are the standard Dylan version from different albums. Still, I’m surprised I have six unique versions.

Despite the naive rankings given by the script, “White Christmas” beat out “Silent Night” by a score of 8—7½. A quick scroll through the library showed an extra “White Christmas” under the title “White Christmas (Clyde McPhatter and The Drifters).” Thank you, GraceNote. One of the “Silent Night” copies was a repeat (by Dinah Washington), and the ½ came from a “Silent Night/Away In A Manger” medley by Raffi. (Stop laughing—Raffi kicks ass.)

A fun little lunchtime project with oddly interesting results. If I had any juice in the blogosphere, I could see this becoming a meme.

1. By the Amboy Dukes, Big Joe Williams, John Lennon, Muddy Waters, and Van Morrison. The Lennon really shouldn’t be counted because it’s not the same song. Muddy’s, of course, is the best.