Repository navigation
Expand file tree
/
Copy pathdownload-song-charts.user.js
More file actions
320 lines (265 loc) · 10.1 KB
/
Copy pathdownload-song-charts.user.js
File metadata and controls
320 lines (265 loc) · 10.1 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
// ==UserScript==
// @name RateYourMusic Song Charts Data Extractor
// @namespace RateYourMusic scripts
// @version 1.2
// @description Extract and download song chart data as CSV or plain text from RateYourMusic charts pages.
// @author dbeley
// @match https://rateyourmusic.com/charts/top/song/*
// @match https://rateyourmusic.com/charts/popular/song/*
// @match https://rateyourmusic.com/charts/esoteric/song/*
// @match https://rateyourmusic.com/charts/diverse/song/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function getTimestamp() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0"); // Months are zero-indexed
const day = String(now.getDate()).padStart(2, "0");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
}
function getChartInfo() {
const path = window.location.pathname;
const searchParams = new URLSearchParams(window.location.search);
// Extract chart type (top, popular, esoteric, diverse)
const chartTypeMatch = path.match(
/\/charts\/(top|popular|esoteric|diverse)\//
);
const chartType = chartTypeMatch ? chartTypeMatch[1] : "unknown";
// Extract filters from URL path and query params
const filters = [];
// Year filter
const yearMatch = path.match(/\/(\d{4})\/?/);
if (yearMatch) filters.push(yearMatch[1]);
// Genre/descriptor from path
const pathParts = path
.split("/")
.filter(
(p) =>
p &&
!["charts", "top", "popular", "esoteric", "diverse", "song"].includes(
p
) &&
!/^\d{4}$/.test(p)
);
filters.push(...pathParts);
// Additional query parameters that might be useful
if (searchParams.has("page"))
filters.push(`page${searchParams.get("page")}`);
return {
type: chartType,
filters: filters.join("_"),
};
}
// Function to extract relevant information from each div
function extractInfo(div) {
const info = {};
// Extract song title
const titleElement = div.querySelector(
".page_charts_section_charts_item_title .ui_name_locale_original, .page_charts_section_charts_item_title .ui_name_locale"
);
info.title = titleElement ? titleElement.textContent.trim() : "N/A";
// Extract artist name(s)
const artistElements = div.querySelectorAll(
".page_charts_section_charts_item_credited_text a.artist"
);
info.artist =
Array.from(artistElements)
.map((el) => el.textContent.trim())
.join(" ")
.trim() || "N/A";
// Extract release date
const dateElement = div.querySelector(
".page_charts_section_charts_item_title_date_compact span:first-child"
);
info.release_date = dateElement ? dateElement.textContent.trim() : "N/A";
// Extract genre
const genreElements = div.querySelectorAll(
".page_charts_section_charts_item_genres_primary a, .page_charts_section_charts_item_genres_secondary a"
);
info.genre = Array.from(genreElements)
.map((genre) => genre.textContent.trim())
.join(", ");
// Extract average rating
const ratingElement = div.querySelector(
".page_charts_section_charts_item_stats_ratings .page_charts_section_charts_item_details_average_num"
);
info.average_rating = ratingElement
? ratingElement.textContent.trim()
: "N/A";
// Extract number of ratings
const ratingsElement = div.querySelector(
".page_charts_section_charts_item_stats_ratings .page_charts_section_charts_item_details_ratings .abbr"
);
info.number_of_ratings = ratingsElement
? ratingsElement.textContent.trim()
: "N/A";
// Extract image URL
const imageElement = div.querySelector(
".page_charts_section_charts_item_image img"
);
info.image_url = imageElement ? imageElement.src.trim() : "N/A";
return info;
}
// Function to convert data to CSV format
function convertToCSV(data) {
if (data.length === 0) {
console.log("No data found.");
return null;
}
const headers = Object.keys(data[0]);
const csvRows = [headers.join(",")];
data.forEach((row) => {
const values = headers.map((header) => {
let value = row[header] ? String(row[header]) : "";
// Escape double quotes in values by doubling them
value = value.replace(/"/g, '""');
// Wrap each value in double quotes
return `"${value}"`;
});
csvRows.push(values.join(","));
});
return csvRows.join("\n");
}
// Function to convert data to plain text format
function convertToPlainText(data) {
if (data.length === 0) {
console.log("No data found.");
return null;
}
return data.map((item) => `${item.artist} - ${item.title}`).join("\n");
}
// Function to download a file
function downloadFile(content, filename, type) {
if (!content) {
alert("No data available to download.");
return;
}
const blob = new Blob([content], { type: type });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.setAttribute("hidden", "");
a.setAttribute("href", url);
a.setAttribute("download", filename);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// Cleanup URL object
window.URL.revokeObjectURL(url);
}
// Function to handle CSV download button click
function handleDownloadCSVClick() {
console.log("Download CSV button clicked. Starting data extraction...");
// Select all div elements with the relevant class
const divElements = document.querySelectorAll(
"div.page_charts_section_charts_item"
);
// Initialize an array to store the extracted information
const extractedData = [];
// Iterate over each div element and extract the information
divElements.forEach((div) => {
const info = extractInfo(div);
extractedData.push(info);
});
console.log("Data extraction complete. Data:", extractedData);
// Convert the extracted data to CSV format
const csvData = convertToCSV(extractedData);
console.log("CSV data prepared. Starting download...");
// Generate timestamp
const timestamp = getTimestamp();
// Get chart info for filename
const chartInfo = getChartInfo();
const chartPart = chartInfo.filters
? `${chartInfo.type}_${chartInfo.filters}`
: chartInfo.type;
// Generate filename with timestamp
const filename = `rym_song_charts_${chartPart}_${timestamp}.csv`;
// Download the CSV file
downloadFile(csvData, filename, "text/csv");
}
// Function to handle Plain Text download button click
function handleDownloadPlainTextClick() {
console.log(
"Download Plain Text button clicked. Starting data extraction..."
);
// Select all div elements with the relevant class
const divElements = document.querySelectorAll(
"div.page_charts_section_charts_item"
);
// Initialize an array to store the extracted information
const extractedData = [];
// Iterate over each div element and extract the information
divElements.forEach((div) => {
const info = extractInfo(div);
extractedData.push(info);
});
console.log("Data extraction complete. Data:", extractedData);
// Convert the extracted data to Plain Text format
const plainTextData = convertToPlainText(extractedData);
console.log("Plain Text data prepared. Starting download...");
// Generate timestamp
const timestamp = getTimestamp();
// Get chart info for filename
const chartInfo = getChartInfo();
const chartPart = chartInfo.filters
? `${chartInfo.type}_${chartInfo.filters}`
: chartInfo.type;
// Generate filename with timestamp
const filename = `rym_song_charts_${chartPart}_${timestamp}.txt`;
// Download the text file
downloadFile(plainTextData, filename, "text/plain");
}
// Function to create and insert the download buttons
function addButtons() {
const navContainer = document.querySelector(
".page_charts_section_charts_nav_view"
);
if (
navContainer &&
!navContainer.querySelector(".download-buttons-container")
) {
const buttonContainer = document.createElement("div");
buttonContainer.className = "download-buttons-container";
const csvButton = document.createElement("button");
csvButton.textContent = "Download CSV";
csvButton.style.marginLeft = "10px";
csvButton.style.padding = "5px 10px";
csvButton.style.backgroundColor = "#4CAF50";
csvButton.style.color = "white";
csvButton.style.border = "none";
csvButton.style.borderRadius = "5px";
csvButton.style.cursor = "pointer";
csvButton.addEventListener("click", handleDownloadCSVClick);
const textButton = document.createElement("button");
textButton.textContent = "Download Plain Text";
textButton.style.marginLeft = "10px";
textButton.style.padding = "5px 10px";
textButton.style.backgroundColor = "#2196F3";
textButton.style.color = "white";
textButton.style.border = "none";
textButton.style.borderRadius = "5px";
textButton.style.cursor = "pointer";
textButton.addEventListener("click", handleDownloadPlainTextClick);
buttonContainer.appendChild(csvButton);
buttonContainer.appendChild(textButton);
navContainer.appendChild(buttonContainer);
console.log("Download buttons added to the page.");
}
}
// Set up MutationObserver
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === "childList") {
addButtons();
}
});
});
// Observe changes in the document body
observer.observe(document.body, { childList: true, subtree: true });
// Add buttons initially if the content is already loaded
addButtons();
})();