-
Notifications
You must be signed in to change notification settings - Fork 1
/
itunesLoader.py
91 lines (73 loc) · 2.49 KB
/
itunesLoader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import re
import sys
import cPickle
def unescape(s):
s = s.replace(" ", " ")
s = s.replace("!", "!")
s = s.replace(""", "\"")
s = s.replace("#", "#")
s = s.replace("$", "$")
s = s.replace("%", "%")
s = s.replace("&", "&")
s = s.replace("'", "\'")
s = s.replace("(", "(")
s = s.replace(")", ")")
s = s.replace("*", "*")
s = s.replace("+", "+")
s = s.replace(",", ",")
s = s.replace("-", "-")
s = s.replace(".", ".")
s = s.replace("/", "/")
s = s.replace(":", ":")
s = s.replace(";", ";")
s = s.replace("<", "<")
s = s.replace("=", "=")
s = s.replace(">", ">")
s = s.replace("?", "?")
s = s.replace("@", "@")
return s
def load(x):
dom = open(x, 'r')
input = dom.read()
lines = input.split("\n")
dom.close()
theList = []
lastLineType = 1
theDictionary = {}
alert = 'ARTIST_DNE_ALERT'
for line in lines:
if re.search('<key>Artist</key>',line):
if lastLineType != 1:
theList.append('0')
artist = line.replace('<key>Artist</key><string>','')
artist = artist.replace('</string>','')
artist = artist.strip('\t')
theList.append(unicode(unescape(artist),'utf-8'))
lastLineType = 0
if re.search('<key>Play Count</key>',line):
if lastLineType != 0:
theList.append(alert)
pcount = line.replace('<key>Play Count</key><integer>','')
pcount = pcount.replace('</integer>','')
pcount = pcount.strip('\t')
theList.append(pcount)
lastLineType = 1
for i in range(0,len(theList)-1,2):
if theList[i] in theDictionary:
theDictionary[theList[i]] += int(theList[i+1])
else:
theDictionary[theList[i]] = int(theList[i+1])
if alert in theDictionary:
del theDictionary[alert]
theDictionary = sorted(theDictionary.iteritems(),key=lambda (k,v):(v,k), reverse=True)
for item in theDictionary:
if item[1] > 0:
print item[0] + ', ' + str(item[1])
f_dict = open('mycharts','w')
print 'writing to disk...'
print theDictionary
cPickle.dump(theDictionary, f_dict)
if len(sys.argv) < 2:
print 'pleaese specify the library xml, e.g.: "python itunesLoader.py Library.xml"'
else:
load(sys.argv[1])