-
Notifications
You must be signed in to change notification settings - Fork 0
/
itunes.html
285 lines (251 loc) · 13.1 KB
/
itunes.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html>
<head>
<title>iTunes Library Playlists</title>
<style>
body {
margin: 0;
font-family: helvetica,arial,sans-serif;
font-size: small;
line-height: 125%;
}
#welcome {
text-align: center;
}
input[type="file"] {
padding: 1ex;
background: #eee;
}
table {
text-align: left;
width: 100%;
border-collapse: collapse;
}
caption {
text-align: left;
padding: 1ex;
}
tbody tr:nth-child(odd) {
background-color: #f4f4f4;
}
th, td {
padding-left: 1ex;
}
.num {
text-align: right;
padding-right: 1ex;
}
@media print {
select, button {
display: none;
}
#playlistTracks caption::before {
content: attr(title);
font-weight: bold;
display: block;
font-size: 125%;
}
}
</style>
<script>
function parsePropertyList(plist) {
function parseArray(array) {
var parsed = [];
var node = array.firstChild;
while (node !== null) {
if (node.nodeType === Node.ELEMENT_NODE) {
parsed.push(parseNode(node));
}
node = node.nextSibling
}
return parsed;
}
function parseDict(dict) {
var parsed = {};
var key = "";
var node = dict.firstChild;
while (node !== null) {
if (node.nodeName === "key") {
key = node.textContent;
node = node.nextSibling;
while (node.nodeType !== Node.ELEMENT_NODE) {
node = node.nextSibling
}
parsed[key] = parseNode(node);
}
node = node.nextSibling;
}
return parsed;
}
function parseNode(node) {
switch(node && node.nodeName) {
case "string":
return node.textContent;
case "real":
return parseFloat(node.textContent);
case "integer":
return parseInt(node.textContent, 10);
case "true":
return true;
case "false":
return false;
case "date":
return new Date(node.textContent);
case "data":
return node.textContent.replace(/\s/g,'');
case "array":
return parseArray(node);
case "dict":
return parseDict(node);
default:
return null;
}
}
if (typeof plist === "string") {
plist = new DOMParser().parseFromString(plist, "text/xml");
}
var topDict = plist.querySelector("dict");
return parseNode(topDict);
}
var library = {};
function humanReadableTrackTime(time) {
var humanReadable = "";
var hours = Math.floor(time/3600000);
if (hours > 0) {
humanReadable += hours + ":";
}
var minutes = Math.floor(time/60000%60);
if (hours > 0 && minutes < 10) {
humanReadable += "0" + minutes + ":";
} else {
humanReadable += minutes + ":";
}
var seconds = Math.round(time/1000%60);
if (seconds < 10) {
humanReadable += "0" + seconds;
} else {
humanReadable += seconds;
}
return humanReadable;
}
function humanReadablePlaylistTime(time) {
var humanReadable = "";
var hours = Math.floor(time/3600000);
if (hours === 1) {
humanReadable += "1 hour ";
} else if (hours > 1) {
humanReadable += hours + " hours ";
}
var minutes = Math.round(time/60000%60);
if (minutes === 1) {
humanReadable += "1 minute";
} else if (minutes > 1) {
humanReadable += minutes + " minutes";
}
return humanReadable;
}
function playlistHTML(playlistIndex) {
var playlist = library.Playlists[playlistIndex];
var playlistItems = playlist["Playlist Items"] || [];
var playlistTotalTime = 0;
var tbody = '<tbody>';
playlistItems.forEach(function(item, index) {
var track = library.Tracks[item["Track ID"]];
playlistTotalTime += track["Total Time"];
tbody += '<tr><td class="num">' + (index+1) + '</td><td>' + (track["Name"]||"") + '</td><td class="num">' +
humanReadableTrackTime(track["Total Time"]) + '</td><td>' + (track["Artist"]||"") + '</td><td>' +
(track["Album"]||"") + '</td></tr>';
});
return '<table>' +
'<caption title="' + playlist.Name + '">' + playlistItems.length + " songs, " + humanReadablePlaylistTime(playlistTotalTime) + '</caption>' +
'<thead><tr><th class="num"></th><th>Name</th><th class="num">Time</th><th>Artist</th><th>Album</th></tr></thead>' +
tbody + '</table>';
}
function showPlaylist(playlistIndex) {
var playlistContainer = document.getElementById("playlistTracks");
if (playlistContainer == null) {
playlistContainer = document.createElement("div");
playlistContainer.id = "playlistTracks";
document.body.appendChild(playlistContainer);
}
playlistContainer.innerHTML = playlistHTML(playlistIndex);
document.title = document.querySelector("caption").title;
}
function buildPlaylistSelector() {
select = document.createElement("select");
select.id = "playlist-selector";
library.Playlists.forEach(function(playlist, index) {
var option = document.createElement("option");
option.value = index;
var numItems = playlist["Playlist Items"] ? playlist["Playlist Items"].length : 0;
option.textContent = playlist["Name"] + " (" + numItems + ")";
select.appendChild(option);
});
select.onchange = function playlistSelectChange(event) {
showPlaylist(this.value);
};
document.body.appendChild(select);
var exportButton = document.createElement("button");
exportButton.textContent = "Export Playlist";
exportButton.onclick = function() {
var playlistIndex = document.getElementById("playlist-selector").value;
exportPlaylist(playlistIndex);
}
document.body.appendChild(exportButton);
}
function exportPlaylist(playlistIndex) {
var newLine = "\n";
var playlist = library.Playlists[playlistIndex];
var exportString = "#EXTM3U" + newLine;
playlist["Playlist Items"].forEach(function(item) {
var track = library.Tracks[item["Track ID"]];
var trackTitle = track.Name;
if (track.Artist) {
trackTitle += " - " + track.Artist;
} else if (track["Album Artist"]) {
trackTitle += " - " + track["Album Artist"];
}
exportString += "#EXTINF:" + Math.round(track["Total Time"]/1000) + "," + trackTitle + newLine;
exportString += track.Location + newLine;
});
downloadFile("application/x-mpegurl", exportString, playlist.Name+".m3u");
}
function readFile(file) {
var reader = new FileReader();
reader.onload = function(e) {
library = parsePropertyList(e.target.result);
if (library === null) {
alert("Unable to find an iTunes Music Library in " + file.name);
} else {
document.getElementById("welcome").remove();
buildPlaylistSelector();
showPlaylist(0);
}
};
reader.readAsText(file);
}
function downloadFile(type, data, filename) {
var downloadLink = document.createElement('a');
var downloadAttributeSupported = 'download' in downloadLink;
downloadLink.href = 'data:' + type + ',' + encodeURIComponent( data );
downloadLink.download = filename;
downloadLink.textContent = filename;
document.body.insertBefore(downloadLink, document.getElementById('playlistTracks'));
if (downloadAttributeSupported) {
downloadLink.click();
downloadLink.parentNode.removeChild(downloadLink);
} else {
downloadLink.textContent += ' (right click and save as)';
}
}
</script>
</head>
<body>
<section id="welcome">
<h1>iTunes Library Playlists</h1>
<p>View and export the playlists contained in an <a href="https://web.archive.org/web/20191231145830/http://support.apple.com/en-us/HT201610">iTunes library XML file</a>.</p>
<input type="file" accept="application/xml" onchange="readFile(this.files[0]);">
<a href="https://github.com/bhoule/itunes-library-playlists"><img alt="Fork me on GitHub" style="position:absolute; top:0; right:0; border:0;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJUAAACVCAQAAAD7q3DEAAAOK0lEQVR42u2dyXNU1xWHj3BsbIJdJi4TFyEGPMVOpZJlFqmU8z/YMpJa6rlbaqklDJoFLTSAhdomkYVmqTWrQcJDzJQqA8ZZxamsYsjG0CLLyFt6/2Xxbr9+PUo4qlT11btvI22/Ovf0GX7nXJHsUyZl8oT8SHbLM7K3NtrHCsM0bsvXzwrjNH736m/lNTkkB2S//ESekz2yW56UJ2SXlEmZlNBJo9ojz8o+//kO1pijc1tgtTDKIHXfHfmdvCFH5KD8tJRhZaGS/YGPo3xBbJssq5tlpmh+cPj38qa8Ij8vZVhlUia75EfylDwje2Wf7JeDngvNLHCBE9sEa4oPqbt/6G15S16Vn8tLpQzLQPW07JXn5UU5IIddo+f4Cyu0bgusVqa5QHPi0Nvyy9KGVSa75Al5UnbLHnlOXpCX5GV5vWYiwiJD24KqiX4WuUDd/UN/kF/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