-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-playlist-extract.js
58 lines (53 loc) · 2.24 KB
/
youtube-playlist-extract.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
function youtube_playlist_is_broken() {
try {
playlistVideoListRenderer = ytInitialData.contents.twoColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].playlistVideoListRenderer;
playlistId = new URLSearchParams(window.location.search).entries().next().value[1];
if (playlistVideoListRenderer.playlistId != playlistId) {
throw true;
}
videos = playlistVideoListRenderer.contents;
} catch (error) {
console.log('Refresh page to load ytInitialData');
return true;
}
for (let vi = 0;vi < videos.length;vi++) {
index = vi + 1;
if ('continuationItemRenderer' in videos[vi]) {
continue;
}
video_index = videos[vi].playlistVideoRenderer.index.simpleText || videos[vi].playlistVideoRenderer.index.runs[0].text;
if (video_index != index.toString()) {
console.log('youtube_playlist_is_broken(): Index ' + video_index + ' is not ' + index +
' for "' + videos[vi].playlistVideoRenderer.title.runs[0].text + '"');
return true;
}
}
return false;
}
function youtube_playlist_extract() {
// Detect YouTube's playlist-disappearing-videos
if (youtube_playlist_is_broken()) {
return;
}
videos = ytInitialData.contents.twoColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].playlistVideoListRenderer.contents;
// Check if list is not fully loaded
for (let vi = 0;vi < videos.length;vi++) {
if (typeof videos[vi].continuationItemRenderer != 'undefined') {
console.log('Scroll to the bottom of the playlist and re-run this script');
return;
}
}
// Print the URLs
count = 0;
urls = '';
for (let vi = 0;vi < videos.length;vi++) {
count += 1;
videoId = videos[vi].playlistVideoRenderer.videoId;
urls += 'https://www.youtube.com/watch?v=' + videoId + '\n';
}
console.log('########## START ##########');
console.log(urls);
console.log('########## END ##########');
console.log('Extracted ' + count + ' URLs from playlist.');
}
youtube_playlist_extract();