I’m trying to maintain a 25-song rolling playlist in iTunes on Windows via win32com and the iTunes COM interface. I’ve got it working fine, except upon initial running it occasionally fails at adding a random song to the playlist with an error stating the song I’m trying to add may have been deleted. The song list it chooses from is simply the iTunes library list that it loads at the start of the script. It looks at the rating of each song as they’re being loaded and funnels them into different groups based on that rating.
all_songs = itunes.LibraryPlaylist.Tracks
grouped_songs = {}
for song in all_songs:
if song.Rating > 0 and not song.ExcludeFromShuffle:
if song.Rating not in grouped_songs:
grouped_songs[song.Rating] = []
grouped_songs[song.Rating].append(song)
Then later in the script when I need a new song to be added to the playlist I’m using this to do so:
while len(playlist.Tracks) < 25:
time.sleep(1)
# Select a random rating group
rating = random.choice(rating_weighting)
# Select a random song from that rating group
song = random.choice(grouped_songs[rating])
# Add the song to the playlist if it's not already there
if song not in playlist.Tracks:
try:
playlist.AddTrack(song)
except pywintypes.com_error as e:
# I no longer care to see the errors because I can't do anything about them anyway.
pass
Occasionally during the initial run it will throw an error from the playlist.AddTrack(song) line as if iTunes thinks I’m referencing a file that does not exist and the add attempt fails. After being annoyed at it exiting every time this happened I threw it in a “try” so that it would just make another attempt whenever it ran across a failure. I couldn’t quite figure out why it was failing in the first place despite trying to determine if it was tied to specific songs or if random.choice() was somehow trying to select from outside the data pool. It was tough because once one of the song entries was throwing an error I couldn’t print(“song.Name:”, song.Name) successfully because it would throw an error at that point, too, so I didn’t think it had something to do with iTunes itself once the print commands were also failing. It’ll happily print song.Name for any entries that are still good, but as soon as any of them are bad it’ll throw the error. I managed to track down a few “bad” songs by printing out the index before and index after the bad one and then looking at the entire list of songs to see which songs were in between the two good indexes at the supposedly bad index. Different run attempts would show it successfully adding the bad songs, so it wasn’t tied to some weird data attached to any one song, it seemed, since it would successfully add one of those songs sometimes. The bad songs just seemed to be entirely random. And the thing is, the longer I let this script run the more often it throws failures. I’ve currently got a print statement in there that prints the group number that it is going to select from next so that I can see if it is trying multiple times to add one song. The longer it runs the longer and longer the thing prints the attempts being made when it needs to add another song. And as this happens the successful attempts at adding a song are more and more frequently adding the same smaller group of songs. So it seems as though the data is being corrupted for some reason, and more and more indexes are going bad. The whole script is only about 150 lines if anyone wants to see the whole thing, and not just those snippets. I’m stumped, though I have very little experience as I’m new to python anyway. Any help would be appreciated. Thanks.