liamks / libpytunes

Python Itunes Library parser
https://github.com/liamks/pyitunes
MIT License
220 stars 88 forks source link

track_id not persistent #61

Open tcarroll2 opened 5 years ago

tcarroll2 commented 5 years ago

iTunes track_id does not remain persistent. Any changes to your iTunes library, such and changing any metadata may very well cause the track_id to change. I had this happen to me. Only the persistent_id field remains unchanged no matter what you do to the library. This can result in errant behavior if one is relying on the track_id to never change for a particular track.

Is there a specific reason the persistent_id is not being used to build the dictionaries/lists?

tcarroll2 commented 5 years ago

After further investigating this issue, it appears whenever iTunes detects any changes that have been made to a 'Track' (i.e. Year, track number, album name, composer) metadata the entire iTunes Library.xml file is written back to disk and that previous 'track_id' is different for that track. Only the 'persistent_id' is consistent across any changes or deletions. Therefore, for anyone using this library who needs to be able to consistently refer to the same track, regardless of any updates made within the iTunes application, you will need to use the 'peristent_id' field.

I will see what I can do to make the 'Tracks' dictionary use the 'persistent_id' field as the key rather than the 'track_id' field. I discovered this after printing out hundreds of pages with track names and then find out the names no longer match up to a track_id after making a few changes within the iTunes library. Expensive mistake!

tcarroll2 commented 5 years ago

I created a new variable right below 'self.songs' named 'self.byPID' and then at the end of the getSongs() function I added just below 'self.songs[int(trackid)] = s', 'self.byPID[s.persistent_id] = s' this will now create an additional dictionary 'byPID' that can be used to manage tracks by their 'persistent_id' rather than 'track_id', which can change at anytime.