-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist_graber.js
73 lines (58 loc) · 2.73 KB
/
playlist_graber.js
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
// Function to scrape songs from YouTube Music
function scrapeSongsFromYouTubeMusic() {
const songs = [];
const songElements = document.querySelectorAll('ytmusic-responsive-list-item-renderer'); // Selector for song items
songElements.forEach(song => {
const titleElement = song.querySelector('.yt-simple-endpoint'); // Selector for song title
const artistElement = song.querySelector('a.yt-simple-endpoint.style-scope.yt-formatted-string[href^="channel/"]'); // Selector for artist name
const title = titleElement ? titleElement.innerText : 'Unknown Title'; // Extract song title
const artist = artistElement ? artistElement.innerText : 'Unknown Artist'; // Extract artist name
songs.push({ title, artist });
});
return songs; // Return the songs array
}
// Function to scrape songs from SoundCloud
function scrapeSongsFromSoundCloud() {
const songs = [];
const songElements = document.querySelectorAll('.trackItem__trackTitle'); // Selector for song title
songElements.forEach(song => {
const title = song.innerText; // Extract song title
const artistElement = song.closest('.trackItem').querySelector('.trackItem__username'); // Selector for artist name
const artist = artistElement ? artistElement.innerText : 'Unknown Artist'; // Extract artist name
songs.push({ title, artist });
});
return songs; // Return the songs array
}
// Main function to detect platform and scrape songs
function scrapeSongs() {
let songs = [];
const url = window.location.href;
if (url.includes('music.youtube.com')) {
songs = scrapeSongsFromYouTubeMusic();
} else if (url.includes('soundcloud.com')) {
songs = scrapeSongsFromSoundCloud();
} else {
console.log("Unsupported platform or not on a music site.");
return;
}
// Log the list of songs
console.table(songs);
// Convert the songs array to CSV format
const csvContent = "Title,Artist\n" // Header
+ songs.map(e => {
const title = e.title.replace(/"/g, '""');
const artist = e.artist.replace(/"/g, '""');
return `"${title}","${artist}"`;
}).join("\n"); // Data rows
// Create a Blob with UTF-8 encoding
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement("a");
const urlBlob = URL.createObjectURL(blob);
link.setAttribute("href", urlBlob);
link.setAttribute("download", "playlist_songs.csv");
document.body.appendChild(link); // Required for Firefox
link.click(); // Automatically click the link to trigger the download
document.body.removeChild(link); // Clean up the DOM
}
// Run the function
scrapeSongs();