-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2.py
81 lines (73 loc) · 2.92 KB
/
v2.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
import os
import xml.etree.cElementTree as ET
import pandas as pd
itunes_folder = os.path.expanduser('~' + os.sep + 'Music' + os.sep + 'iTunes')
tree = ET.ElementTree(file=os.path.join(itunes_folder, 'iTunes Music Library.xml'))
root = tree.getroot()
for i , child in enumerate(root[0]):
if child.text == "Tracks":
tracks = root[0][i]
break
songdb = list()
for j in range(len(root[0][i+1])): # Loop for every song
if j % 2 == 1: # every two is a song
song_detail = root[0][i+1][j]
Name_i = int()
Artist_i = int()
Album_i = int()
PlayCount_i = int()
Rating_i = int()
for field_i, field in enumerate(song_detail):
if field_i % 2 == 0 and field.text == 'Name':
Name_i = field_i + 1
if field_i % 2 == 0 and field.text == 'Artist':
Artist_i = field_i + 1
if field_i % 2 == 0 and field.text == 'Album':
Album_i = field_i + 1
if field_i % 2 == 0 and field.text == 'Play Count':
PlayCount_i = field_i + 1
if field_i % 2 == 0 and field.text == 'Rating':
Rating_i = field_i + 1
song = dict()
normal = True
if PlayCount_i == 0:
continue
for field_i, field in enumerate(song_detail):
if field_i == Name_i:
tempsong = str()
tempsong = field.text
song['Name'] = tempsong.replace(' (Live)','').replace('(Live)','')
if field_i == Artist_i:
song['Artist'] = field.text
if field_i == Album_i:
song['Album'] = field.text
if field_i == Rating_i:
try:
song['Rating'] = int(field.text)
except ValueError:
normal = False
except TypeError:
pass
if field_i == PlayCount_i:
try:
testisint = int(field.text)
except ValueError:
normal = False
except TypeError:
pass
if field.text == None:
song['Play Count'] = 0
else:
song['Play Count'] = field.text
if normal == True and song.get('Play Count') is not None:
songdb.append(song)
df = pd.DataFrame(songdb)
df['Play Count'] = df ['Play Count'].astype(int)
grouped=df.groupby(['Name','Artist']).agg({'Play Count':sum})
res = grouped.sort_values('Play Count', ascending=False)
print(res.head(30))
artistgroup = df.groupby('Artist').agg({'Play Count':sum})
artistres = artistgroup.sort_values('Play Count', ascending=False)
print(artistres.head(30))
ratingcount = df.groupby('Rating').agg('count').sort_values('Rating', ascending=False)
print(ratingcount.head(5))