-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrateyourmusic-song-csv-tracker.user.js
More file actions
582 lines (516 loc) · 16.2 KB
/
rateyourmusic-song-csv-tracker.user.js
File metadata and controls
582 lines (516 loc) · 16.2 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// ==UserScript==
// @name RateYourMusic Song CSV Tracker
// @namespace https://github.com/dbeley/rym-userscripts
// @version 1.0.0
// @description Capture song metadata on RateYourMusic song and chart pages and keep a CSV in sync (auto-save or manual download).
// @author dbeley
// @match https://rateyourmusic.com/song/*
// @match https://rateyourmusic.com/charts/*/song/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant GM_download
// @run-at document-idle
// ==/UserScript==
(function () {
"use strict";
const STORAGE_KEY = "rateyourmusic-song-csv::records";
const DB_NAME = "rateyourmusic-song-csv";
const STORE_NAME = "handles";
const FILE_KEY = "csv-output-songs";
GM_registerMenuCommand("Set song CSV output file", () => {
pickCsvFile(true).catch(console.error);
});
GM_registerMenuCommand("Download song CSV once", () => {
downloadCsv().catch(console.error);
});
main().catch(console.error);
async function main() {
if (window.location.pathname.includes("/charts/")) {
const records = extractChartRecords();
if (records.length > 0) {
for (const record of records) {
await upsertRecord(record);
}
console.info(
`[rateyourmusic-song-csv] Recorded ${records.length} songs from chart page`
);
await writeCsvToDisk();
}
return;
}
const record = extractSongRecord();
if (!record) return;
await upsertRecord(record);
console.info(
`[rateyourmusic-song-csv] Recorded ${record.name || "unknown"} (${record.slug}) updated at ${record.updatedAt}`
);
await writeCsvToDisk();
}
function extractSongRecord() {
const name =
document
.querySelector(
".page_song_header_main_info h1 .ui_name_locale_original"
)
?.textContent?.trim() ||
document.querySelector('meta[property="og:title"]')?.content?.trim() ||
document.title;
if (!name) return null;
const artist =
document
.querySelector(
".page_song_header_info_artist .artist .ui_name_locale_original"
)
?.textContent?.trim() ||
document
.querySelector(".page_song_header_info_artist .artist")
?.textContent?.trim() ||
"";
const albumLink = document.querySelector(
".page_song_header_info_rest a.album"
);
const album = albumLink?.textContent?.trim() || "";
const albumUrl = albumLink
? new URL(albumLink.href, location.href).href
: "";
const type = extractType();
const releaseDate = extractReleaseDate();
const rank = extractRankInfo();
const ratingValue = cleanNumber(
document.querySelector(
".page_section_main_info_music_rating_value_rating"
)?.textContent
);
const ratingCount = cleanNumber(
document.querySelector(
".page_section_main_info_music_rating_value_number"
)?.textContent
);
const primaryGenres = extractList(
document.querySelectorAll(".page_song_header_info_genre a.genre")
);
const secondaryGenres = ""; // Secondary genres are not surfaced on song pages
const descriptors = extractDescriptors();
const description =
document.querySelector('meta[name="description"]')?.content ||
document.querySelector('meta[property="og:description"]')?.content ||
"";
const image =
document.querySelector('meta[property="og:image"]')?.content ||
document.querySelector(".page_song_header_art img")?.src ||
"";
const url = new URL(
document.querySelector('link[rel="canonical"]')?.href || location.href,
location.href
).href;
const slug = url.split("/").filter(Boolean).pop() || "song";
const now = new Date().toISOString();
return {
slug,
name,
artist,
album,
albumUrl,
type,
releaseDate,
rank,
ratingValue: ratingValue ?? "",
maxRating: "", // Not exposed on song pages
ratingCount: ratingCount ?? "",
reviewCount: "", // Not exposed on song pages
primaryGenres,
secondaryGenres,
descriptors,
image,
description,
url,
updatedAt: now,
};
}
function extractType() {
const spans = document.querySelectorAll(
".page_song_header_info_rest .pipe_separated"
);
for (const span of spans) {
const text = span.textContent.trim();
if (text && !/^Released/i.test(text) && !/#/g.test(text)) {
return text;
}
}
return "Song";
}
function extractReleaseDate() {
const spans = document.querySelectorAll(
".page_song_header_info_rest .pipe_separated"
);
for (const span of spans) {
const text = span.textContent.trim();
const match = text.match(/Released\s+(.*)/i);
if (match) return match[1].trim();
}
return "";
}
function extractRankInfo() {
const rankLinks = document.querySelectorAll(
".page_song_header_info_rest a"
);
const ranks = Array.from(rankLinks)
.map((link) => link.textContent.trim().replace(/\s+/g, " "))
.filter(Boolean);
return ranks.join("; ");
}
function extractDescriptors() {
const descriptorEl = document.querySelector(
".page_song_header_info_descriptors"
);
if (!descriptorEl) return "";
return descriptorEl.textContent
.split(/,|;/)
.map((part) => part.trim())
.filter(Boolean)
.join("; ");
}
function cleanNumber(value) {
if (!value) return "";
return value
.replace(/\s+/g, " ")
.replace(/ratings?$/i, "")
.trim();
}
function extractChartRecords() {
const chartItems = document.querySelectorAll(
".page_charts_section_charts_item.object_song"
);
const records = [];
chartItems.forEach((item) => {
const link = item.querySelector(
".page_charts_section_charts_item_link.song"
);
if (!link) return;
const url = new URL(link.href, location.href).href;
const slug = new URL(url).pathname.split("/").filter(Boolean).pop();
if (!slug) return;
const nameNode = link.querySelector(".ui_name_locale_original");
const name = nameNode
? nameNode.textContent.trim()
: link.textContent.trim();
const artistLink = item.querySelector(
".page_charts_section_charts_item_credited_links_primary a.artist"
);
const artistNameNode = artistLink?.querySelector(
".ui_name_locale_original"
);
const artist = artistNameNode
? artistNameNode.textContent.trim()
: artistLink?.textContent.trim() || "";
const dateNode = item.querySelector(
".page_charts_section_charts_item_title_date_compact span"
);
const releaseDate = dateNode ? dateNode.textContent.trim() : "";
const typeNode = item.querySelector(
".page_charts_section_charts_item_release_type"
);
const type = (typeNode ? typeNode.textContent.trim() : "") || "Song";
const ratingNode = item.querySelector(
".page_charts_section_charts_item_details_average_num"
);
const ratingValue = ratingNode ? ratingNode.textContent.trim() : "";
const ratingCountNode = item.querySelector(
".page_charts_section_charts_item_details_ratings .abbr"
);
const ratingCount = ratingCountNode
? ratingCountNode.textContent.trim()
: "";
const primaryGenres = extractList(
item.querySelectorAll(
".page_charts_section_charts_item_genres_primary a.genre"
)
);
const secondaryGenres = extractList(
item.querySelectorAll(
".page_charts_section_charts_item_genres_secondary a.genre"
)
);
const imgNode = item.querySelector(
".page_charts_section_charts_item_image img"
);
const image = imgNode ? imgNode.src : "";
const now = new Date().toISOString();
records.push({
slug,
name,
artist,
album: "",
albumUrl: "",
type,
releaseDate,
rank: "", // Not available on chart items
ratingValue,
maxRating: "",
ratingCount,
reviewCount: "",
primaryGenres,
secondaryGenres,
descriptors: "",
image,
description: "",
url,
updatedAt: now,
isPartial: true,
});
});
return records;
}
async function upsertRecord(record) {
const records = await loadRecords();
const existing = records[record.slug] || {};
if (record.isPartial && !existing.isPartial) {
records[record.slug] = {
...existing,
slug: record.slug,
...(record.name && { name: record.name }),
...(record.artist && { artist: record.artist }),
...(record.type && { type: record.type }),
...(record.releaseDate && { releaseDate: record.releaseDate }),
...(record.ratingValue && { ratingValue: record.ratingValue }),
...(record.ratingCount && { ratingCount: record.ratingCount }),
...(record.primaryGenres && { primaryGenres: record.primaryGenres }),
...(record.secondaryGenres && {
secondaryGenres: record.secondaryGenres,
}),
...(record.descriptors && { descriptors: record.descriptors }),
...(record.image && { image: record.image }),
...(record.url && record.url !== existing.url && { url: record.url }),
updatedAt: record.updatedAt,
firstSeen: existing.firstSeen || record.updatedAt,
};
} else {
const merged = {
...existing,
...record,
firstSeen: existing.firstSeen || record.updatedAt,
};
if (record.isPartial === undefined) {
delete merged.isPartial;
}
records[record.slug] = merged;
}
await saveRecords(records);
}
async function loadRecords() {
try {
const stored = await GM_getValue(STORAGE_KEY, {});
return stored || {};
} catch (_) {
return {};
}
}
async function saveRecords(records) {
try {
await GM_setValue(STORAGE_KEY, records);
} catch (err) {
console.error("[rateyourmusic-song-csv] Unable to persist records", err);
}
}
function buildCsv(records) {
const headers = [
"name",
"slug",
"artist",
"album",
"albumUrl",
"type",
"releaseDate",
"rank",
"ratingValue",
"maxRating",
"ratingCount",
"reviewCount",
"primaryGenres",
"secondaryGenres",
"descriptors",
"image",
"description",
"url",
"firstSeen",
"updatedAt",
];
const rows = Object.values(records)
.sort((a, b) => a.name.localeCompare(b.name))
.map((entry) =>
headers.map((key) => escapeCsv(entry[key] ?? "")).join(",")
);
return [headers.join(","), ...rows].join("\n");
}
function escapeCsv(value) {
const str = String(value ?? "");
if (/[",\n]/.test(str)) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}
async function writeCsvToDisk() {
const records = await loadRecords();
const csv = buildCsv(records);
const handle = await loadStoredHandle();
if (!handle) {
console.info(
"[rateyourmusic-song-csv] Pick an output file via the menu to auto-save the CSV."
);
return;
}
const permission = await ensurePermission(handle);
if (permission !== "granted") {
console.warn(
"[rateyourmusic-song-csv] File permission was denied. Re-select the output file."
);
return;
}
const writable = await handle.createWritable();
await writable.write(csv);
await writable.close();
console.debug("[rateyourmusic-song-csv] CSV written to disk.");
}
async function ensurePermission(handle) {
if (!handle.queryPermission || !handle.requestPermission) return "denied";
let status = await handle.queryPermission({ mode: "readwrite" });
if (status === "granted") return status;
if (status === "prompt") {
try {
status = await handle.requestPermission({ mode: "readwrite" });
} catch (_) {
status = "denied";
}
}
return status;
}
async function pickCsvFile(writeCurrentCsv = false) {
if (!window.showSaveFilePicker) {
alert(
"Your browser does not support the File System Access API. Use the 'Download song CSV once' menu instead."
);
return;
}
const handle = await window.showSaveFilePicker({
suggestedName: "rateyourmusic-songs.csv",
types: [
{
description: "CSV file",
accept: { "text/csv": [".csv"] },
},
],
});
await storeHandle(handle);
if (writeCurrentCsv) {
await writeCsvToDisk();
}
}
async function downloadCsv() {
const records = await loadRecords();
const csv = buildCsv(records);
console.info(
`[rateyourmusic-song-csv] Download command triggered (records=${Object.keys(records).length || 0})`
);
const filename = "rateyourmusic-songs.csv";
const blob = new Blob([csv], { type: "text/csv" });
const blobUrl = URL.createObjectURL(blob);
const isFirefox =
typeof navigator === "object" && /Firefox/.test(navigator.userAgent);
const attempts = [
async () => {
if (isFirefox) throw new Error("skip GM_download on Firefox");
if (typeof GM_download !== "function")
throw new Error("GM_download missing");
await GM_download({ url: blobUrl, name: filename, saveAs: true });
console.info("[rateyourmusic-song-csv] GM_download succeeded.");
},
async () => {
const anchor = document.createElement("a");
anchor.href = blobUrl;
anchor.download = filename;
anchor.style.display = "none";
document.body.append(anchor);
anchor.dispatchEvent(
new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
})
);
anchor.click();
anchor.remove();
console.info(
"[rateyourmusic-song-csv] Anchor click fallback attempted."
);
},
async () => {
const dataUrl = `data:text/csv;charset=utf-8,${encodeURIComponent(csv)}`;
const win = window.open(dataUrl, "_blank");
if (!win) throw new Error("Popup blocked");
console.info("[rateyourmusic-song-csv] Opened data URL in new tab.");
},
];
let success = false;
for (const attempt of attempts) {
try {
await attempt();
success = true;
break;
} catch (err) {
console.warn("[rateyourmusic-song-csv] Download path failed:", err);
}
}
if (!success) {
alert(
"CSV download was blocked. Check popup/download permissions for this site and try again."
);
}
setTimeout(() => URL.revokeObjectURL(blobUrl), 1500);
}
async function loadStoredHandle() {
try {
const db = await openDb();
return await readHandle(db);
} catch (err) {
console.error(
"[rateyourmusic-song-csv] Unable to load stored handle",
err
);
return null;
}
}
async function storeHandle(handle) {
const db = await openDb();
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, "readwrite");
tx.onerror = () => reject(tx.error);
tx.oncomplete = () => resolve();
tx.objectStore(STORE_NAME).put(handle, FILE_KEY);
});
}
function readHandle(db) {
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, "readonly");
const req = tx.objectStore(STORE_NAME).get(FILE_KEY);
req.onsuccess = () => resolve(req.result || null);
req.onerror = () => reject(req.error);
});
}
function openDb() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, 1);
request.onupgradeneeded = () => {
request.result.createObjectStore(STORE_NAME);
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
function extractList(nodes) {
return Array.from(nodes)
.map((node) => node.textContent.trim())
.filter(Boolean)
.join("; ");
}
})();