Skip to content

Instantly share code, notes, and snippets.

@wcharczuk
Created November 18, 2011 19:08
Show Gist options
  • Save wcharczuk/1377437 to your computer and use it in GitHub Desktop.
Save wcharczuk/1377437 to your computer and use it in GitHub Desktop.
iTunes Media Directory Extra Files Cleaner
from pyItunes import *
import os
import sys
import urllib
extraFiles = []
libraryLocation = None
if len(sys.argv) > 1:
libraryLocation = sys.argv[1]
else:
libraryLocation = os.path.expanduser("~/Music/iTunes/iTunes Music Library.xml")
def itunesDecode(string):
return urllib.url2pathname(string).replace("file://localhost", "").replace("&", "&")
library = XMLLibraryParser(libraryLocation)
songs = Library(library.dictionary).songs
songHash = set([itunesDecode(k.location) for k in songs])
def strEndsWith(string, exts):
for ext in exts:
if string.endswith(ext):
return True
return False
def fileInLibrary(f):
return f in songHash
#next we need the files in a directory
extensions = ['.mp3', '.mp4', '.alac', '.wav']
root = None
if len(sys.argv) > 2:
root = sys.argv[2]
else:
root = os.path.expanduser("~/Music/iTunes/iTunes Media/Music")
def walkFolder(folder):
for rootDir, subfolders, files in os.walk(folder):
for f in [f2 for f2 in files if strEndsWith(f2, extensions)]:
fullPath = os.path.join(rootDir, f)
if not fileInLibrary(fullPath):
extraFiles.append(fullPath)
print fullPath.replace("\'", "\\\'").replace(" ", "\\ ")
if len(subfolders) > 0:
for f in subfolders:
walkFolder(f)
walkFolder(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment