Sunday 7 March 2021

Cannot access Album Artist key using iTunes COM

I am trying to read the album artist of a track playing in iTunes (Windows) with Python over the iTunes COM interface.

I am able to access information about a track using the following code:

import win32com.client

iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
currentTrack = win32com.client.CastTo(iTunes.CurrentTrack,"IITFileOrCDTrack")

print(currentTrack.Name)
print(currentTrack.Artist)
print(currentTrack.Album)
print(currentTrack.AlbumArtist)

However when I run it while playing a song in iTunes (Zedd - Clarity for example but any song works), I can't access the AlbumArtist key despite the fact that is definitely exists.

enter image description here

$ python w.py
Clarity (feat. Foxes)
Zedd
Clarity
Traceback (most recent call last):
  File "w.py", line 9, in <module>
    print(currentTrack.AlbumArtist)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 474, in __getattr__
    return self._ApplyTypes_(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

I did some investigation in the pywin32 source code and found a private function that exposes all available properties of a COM interface and found the following:

>>> print(currentTrack._prop_map_get_.keys())
dict_keys(['Album', 'AlbumArtist', 'AlbumRating', 'AlbumRatingKind', 'Artist', 'Artwork', 'BPM', 'BitRate', 'BookmarkTime', 'Category', 'Comment', 'Compilation', 'Composer', 'DateAdded', 'Description', 'DiscCount', 'DiscNumber', 'Duration', 'EQ', 'Enabled', 'EpisodeID', 'EpisodeNumber', 'ExcludeFromShuffle', 'Finish', 'Genre', 'Grouping', 'Index', 'Kind', 'KindAsString', 'Location', 'LongDescription', 'Lyrics', 'ModificationDate', 'Name', 'PartOfGaplessAlbum', 'PlayOrderIndex', 'PlayedCount', 'PlayedDate', 'Playlist', 'Playlists', 'Podcast', 'Rating', 'ReleaseDate', 'RememberBookmark', 'SampleRate', 'SeasonNumber', 'Show', 'Size', 'Size64High', 'Size64Low', 'SkippedCount', 'SkippedDate', 'SortAlbum', 'SortAlbumArtist', 'SortArtist', 'SortComposer', 'SortName', 'SortShow', 'Start', 'Time', 'TrackCount', 'TrackDatabaseID', 'TrackNumber', 'Unplayed', 'VideoKind', 'VolumeAdjustment', 'Year', 'playlistID', 'ratingKind', 'sourceID', 'trackID'])

Clearly, the AlbumArtist key is there and should be accessible. Directly accessing the AlbumArtist key of the prop map yields the following:

>>> print(currentTrack._prop_map_get_['AlbumArtist'])
(1610874906, 2, (8, 0), (), 'AlbumArtist', None)

I don't know if there is any information I can derive from this, it returns the same value regardless of the song that's playing. Internally, the pywin32 library converts this sort of data to a string for the accessible keys (Artist and Name for example) but returns a Member not found for AlbumArtist.



from Cannot access Album Artist key using iTunes COM

No comments:

Post a Comment