# -*- coding: UTF-8 -*-
import sys, os, re
if len(sys.argv) > 2:
inputPath = sys.argv[2]
else:
inputPath = os.path.expanduser("~/Music/iTunes/iTunes Music Library.xml")
print "Using the default iTunes Library path: %s" % inputPath
if len(sys.argv) > 1:
wantedKey = sys.argv[1]
XML = open(inputPath)
xmlPattern = re.compile(r'^\s*?(.*?)<.*?>(.*?)$')
items = []
currentArtist = None
currentAlbum = None
currentComp = False
ignoreCurrent = True
albums = {}
line = XML.readline()
while line:
result = xmlPattern.search(line)
if line.find("Compilation") > -1 and line.find("true") > -1:
currentComp = True
if line.find("Podcast") > -1 and line.find("true") > -1:
ignoreCurrent = True
if result:
pair = result.groups()
if pair[0] == wantedKey:
wantedValue = pair[1]
if pair[0] == 'Artist':
currentArtist = pair[1]
elif pair[0] == 'Album':
currentAlbum = pair[1]
elif line.find("") > -1:
if not ignoreCurrent:
if currentComp:
currentArtist = "[Compilation]"
albumArtist = "%s — %s" % (currentAlbum, currentArtist)
if not albumArtist in albums:
albums[albumArtist] = wantedValue
print "%s — %s" % (albumArtist, wantedValue)
wantedValue = None
albumArtist = None
currentAlbum = None
currentArtist = None
currentComp = False
ignoreCurrent = False
line = XML.readline()
stats = {}
for oneAlbum in albums.keys():
if albums[oneAlbum] in stats:
stats[albums[oneAlbum]] += 1
else:
stats[albums[oneAlbum]] = 1
orderedKeys = stats.keys()
orderedKeys.sort()
print "\n\n"
for oneKey in orderedKeys:
print oneKey,
print "\n\n"
for oneKey in orderedKeys:
print stats[oneKey],
else:
print "Please enter a key to search by."