-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplist2csv.py
executable file
·60 lines (48 loc) · 1.79 KB
/
plist2csv.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
#!/usr/bin/env python3
import csv
import plistlib
import sys
if len(sys.argv) != 3:
print('usage: {} <input.xml> <output.csv>'.format(sys.argv[0]))
sys.exit(1)
with open(sys.argv[1], 'rb') as fp:
pl = plistlib.load(fp)
with open(sys.argv[2], 'w') as fc:
csv.register_dialect('UniversalScrobbler', delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_ALL)
writer = csv.writer(fc, dialect='UniversalScrobbler')
scrobbles = 0
for t in pl['Tracks']:
try:
title = pl['Tracks'][t]['Name']
except KeyError:
print('Record {} is missing mandatory title information, skipping!'.format(t), file=sys.stderr)
continue
try:
artist = pl['Tracks'][t]['Artist']
except KeyError:
print('Record {} is missing mandatory artist information, skipping!'.format(t), file=sys.stderr)
continue
try:
album = pl['Tracks'][t]['Album']
except KeyError:
album = ''
try:
album_artist = pl['Tracks'][t]['Album Artist']
except KeyError:
album_artist = ''
try:
length = int(pl['Tracks'][t]['Total Time']/1000)
except KeyError:
length = ''
try:
play_count = pl['Tracks'][t]['Play Count']
except KeyError:
print('Failed to get play count for record {}, skipping!'.format(t), file=sys.stderr)
continue
else:
if play_count == 0:
print('Zero plays for record {}!'.format(t), file=sys.stderr)
for _ in range(play_count):
writer.writerow([artist, title, album, '', album_artist, length])
scrobbles += 1
print('{} scrobbles written to {}'.format(scrobbles, sys.argv[2]))