-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenplaylist.html
More file actions
246 lines (211 loc) · 8.15 KB
/
genplaylist.html
File metadata and controls
246 lines (211 loc) · 8.15 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Archive.org _files.xml ? JSON</title>
<style>
body { font-family: sans-serif; max-width: 900px; margin: 1rem auto; }
h1 { font-size: 1.4rem; }
label { font-weight: 600; margin-top: 0.75rem; display: block; }
input, button, textarea {
width: 100%;
box-sizing: border-box;
margin-top: 0.25rem;
margin-bottom: 0.5rem;
padding: 0.4rem 0.5rem;
font-size: 0.95rem;
}
button { cursor: pointer; background: #007cba; color: white; border: none; border-radius: 4px; }
button:hover { background: #005a87; }
button:disabled { background: #ccc; cursor: not-allowed; }
textarea { height: 320px; font-family: monospace; }
small { color: #555; }
.save-section { margin-top: 0.75rem; }
.stats { font-size: 0.85rem; color: #666; margin-top: 0.25rem; }
</style>
</head>
<body>
<h1>Archive.org _files.xml ? JSON</h1>
<label for="xmlUrl">_files.xml URL</label>
<input id="xmlUrl" type="text"
value="https://dn720308.ca.archive.org/0/items/fantasia-2000-ost-deluxe-edition/fantasia-2000-ost-deluxe-edition_files.xml">
<small>
Example: any Internet Archive <em>_files.xml</em> metadata URL. The script will:
- use only .mp3 file nodes,
- concatenate all <creator> tags,
- <strong>year = "0000" (DuckDuckGo unreliable)</strong>,
- use most frequent <genre> as default when empty.
</small>
<button id="convertBtn">Convert</button>
<label for="output">JSON output</label>
<textarea id="output" readonly></textarea>
<div class="stats" id="stats"></div>
<div class="save-section">
<button id="saveJsonBtn" disabled>Save JSON</button>
<button id="copyBtn" disabled>Copy JSON</button>
</div>
<script>
// Find most frequent genre from all <genre> elements
function getMostFrequentGenre(xmlDoc) {
const genreNodes = xmlDoc.getElementsByTagName("genre");
const genreCounts = {};
for (let genreNode of genreNodes) {
const genre = genreNode.textContent.trim();
if (genre) {
genreCounts[genre] = (genreCounts[genre] || 0) + 1;
}
}
if (Object.keys(genreCounts).length === 0) return "";
let maxCount = 0;
let mostFrequent = "";
for (let [genre, count] of Object.entries(genreCounts)) {
if (count > maxCount) {
maxCount = count;
mostFrequent = genre;
}
}
return mostFrequent;
}
// Derive Archive.org identifier from the *original* _files.xml URL
function getIdentifierFromUrl(inputUrl) {
let targetUrl = inputUrl;
try {
const u = new URL(inputUrl);
if (u.hostname === "corsproxy.io") {
const qs = u.search;
if (qs && qs.length > 1) {
targetUrl = decodeURIComponent(qs.substring(1));
}
}
} catch (e) {
targetUrl = inputUrl;
}
try {
const u2 = new URL(targetUrl);
const parts = u2.pathname.split("/").filter(Boolean);
const itemsIndex = parts.indexOf("items");
if (itemsIndex !== -1 && parts.length > itemsIndex + 1) {
return parts[itemsIndex + 1];
}
} catch (e) {}
return "";
}
// Update stats display
function updateStats(jsonString) {
const statsEl = document.getElementById("stats");
if (!jsonString.trim()) {
statsEl.textContent = "";
return;
}
const charCount = jsonString.length;
const objCount = JSON.parse(jsonString).length;
statsEl.textContent = `${charCount} chars, ${objCount} tracks`;
}
// Save JSON to file
function saveJson(jsonString, identifier) {
if (!jsonString.trim()) {
alert('No JSON to save. Generate a conversion first.');
return;
}
const nameBase = identifier.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase();
const filename = `${nameBase}.json`;
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Copy JSON to clipboard
async function copyJson(jsonString) {
try {
await navigator.clipboard.writeText(jsonString);
alert('JSON copied to clipboard!');
} catch (err) {
// Fallback for older browsers
const textarea = document.createElement('textarea');
textarea.value = jsonString;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
alert('JSON copied to clipboard!');
}
}
const convertBtn = document.getElementById("convertBtn");
const saveJsonBtn = document.getElementById("saveJsonBtn");
const copyBtn = document.getElementById("copyBtn");
const output = document.getElementById("output");
let currentIdentifier = "";
convertBtn.addEventListener("click", async () => {
const originalUrl = document.getElementById("xmlUrl").value.trim();
output.value = "Loading...";
saveJsonBtn.disabled = true;
copyBtn.disabled = true;
currentIdentifier = getIdentifierFromUrl(originalUrl);
if (!currentIdentifier) {
output.value = "Could not derive identifier from URL.";
return;
}
const fetchUrl = "https://corsproxy.io/?" + encodeURIComponent(originalUrl);
try {
const res = await fetch(fetchUrl);
if (!res.ok) {
output.value = "Error fetching XML: " + res.status + " " + res.statusText;
return;
}
const xmlText = await res.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "application/xml");
const defaultGenre = getMostFrequentGenre(xmlDoc);
const fileNodes = Array.from(xmlDoc.getElementsByTagName("file"));
const mp3Nodes = fileNodes.filter(f => {
const nameAttr = f.getAttribute("name") || "";
return nameAttr.toLowerCase().endsWith(".mp3");
});
const baseUrl = "https://archive.org/download/" + currentIdentifier + "/";
const items = mp3Nodes.map(f => {
const nameAttr = f.getAttribute("name") || "";
const creators = Array.from(f.getElementsByTagName("creator"))
.map(n => n.textContent.trim())
.filter(Boolean);
const name = creators.join(", ");
const titleEl = f.getElementsByTagName("title")[0];
const albumEl = f.getElementsByTagName("album")[0];
const genreEl = f.getElementsByTagName("genre")[0];
const title = titleEl ? titleEl.textContent.trim() : "";
const album = albumEl ? albumEl.textContent.trim() : "";
const genre = genreEl && genreEl.textContent.trim() ?
genreEl.textContent.trim() : defaultGenre;
const fileUrl = baseUrl + encodeURIComponent(nameAttr);
return {
name: name,
title: title,
album: album,
year: "0000", // DuckDuckGo unreliable, using fixed value
genre: genre,
file: fileUrl
};
});
output.value = JSON.stringify(items, null, 2);
updateStats(output.value);
saveJsonBtn.disabled = false;
copyBtn.disabled = false;
} catch (err) {
output.value = "Error: " + err.message;
saveJsonBtn.disabled = true;
copyBtn.disabled = true;
}
});
saveJsonBtn.addEventListener("click", () => {
saveJson(output.value, currentIdentifier);
});
copyBtn.addEventListener("click", () => {
copyJson(output.value);
});
</script>
</body>
</html>