miércoles, 23 de febrero de 2011

Manipulating iTunes library with Python 3

I wanted to fix some songs in my iTunes library that I rated low but somehow the last.fm scrobbler managed to get them unusually high play counts, so I searched a bit on google for iTunes Python and found this script that was written to remove dead tracks. I worked on it to build the script I needed and with the help of the iTunes Windows COM interface SDK(downloaded from the apple’s developer site) I got a working script.

If you want the iTunes COM documentation and you don’t want to register here it is the help file.

And of course the code.

 

# this programs evaluates each song in an iTunes.xml file and reduces de number
# of play counts of the song if the song is rated with certain ammount of stars
# and the song has a determined ammount of play counts.

import win32com.client

STARS = 20,40 #stars the song has to be an offender, 1 star = 20(internally)
MAX_PLAYS = 55 #minimum number of playcounts the song has to have to be an offender
RED_PERCENT = 90 #the ammount the played count will be reduce, 90%

#First, we create an instance of the iTunes Application
itunes= win32com.client.Dispatch("iTunes.Application")

#please consult the hierarchy used here in the iTunes COM interface.chm
mainLibrary = itunes.LibraryPlaylist
NumTracks = mainLibrary.Tracks.Count

for track in range(1,NumTracks):
    currTrack = mainLibrary.Tracks.Item(track)
    if currTrack.Rating in STARS:
        if currTrack.PlayedCount >= MAX_PLAYS:
            print ("the song "+str(currTrack.Name)+" by "+str(currTrack.Artist)+" has been played "+str(currTrack.PlayedCount)+" times with a rating of "+str(currTrack.Rating/20))
            currTrack.PlayedCount = int(currTrack.PlayedCount*0.2)
            print ("new played count is "+str(currTrack.PlayedCount))

For this code to work you need to install the win32 module, get the latest build it from here.

I ran into some weird bug with Python 3.1.3, bug and fix here.

No hay comentarios:

Publicar un comentario