Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Last active December 18, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenrobertson/5813608 to your computer and use it in GitHub Desktop.
Save glenrobertson/5813608 to your computer and use it in GitHub Desktop.
Get the path names for all iTunes albums that I have with a total track play count of zero, where the album has between 3 and 50 tracks. This is so I can remove albums I don't care about to free up disk space
#!/usr/bin/python
import os
from appscript import app
itunes = app('iTunes')
# get all your tracks, takes a while
tracks = itunes.file_tracks()
# index all tracks by album name
tracks_by_album = {}
for track in tracks:
album = track.album()
if album not in tracks_by_album:
tracks_by_album[album] = []
tracks_by_album[album].append(track)
# calculate total number of track play counts per album
album_playcounts = {}
for album, album_tracks in tracks_by_album.iteritems():
if album not in album_playcounts:
album_playcounts[album] = 0
for track in album_tracks:
album_playcounts[album] += track.played_count()
# sort albums by the play count descending
albums_sorted_by_playcount = sorted(album_playcounts.items(), key=lambda item: item[1], reverse=True)
# get album names that have total track play count of 0, and have between 3 and 50 tracks
albums_with_zero_play_count_and_reasonable_amount_of_tracks = [(album, playcount) for (album, playcount) in albums_sorted_by_playcount if len(tracks_by_album[album]) >= 3 and len(tracks_by_album[album]) <= 50 and playcount == 0]
# store the album directory paths to remove in a set
dirs_to_remove = set()
for album, playcount in albums_with_zero_play_count_and_reasonable_amount_of_tracks:
for track in tracks_by_album[album]:
try:
dirs_to_remove.add(os.path.dirname(track.location().path))
except AttributeError,e:
pass
for dir in dirs_to_remove:
print dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment