Skip to content

Instantly share code, notes, and snippets.

@project237
Last active February 17, 2023 17:57
Show Gist options
  • Save project237/6bc11f425bbc0f95761d44e8cc18cb78 to your computer and use it in GitHub Desktop.
Save project237/6bc11f425bbc0f95761d44e8cc18cb78 to your computer and use it in GitHub Desktop.
Get a list of video metadata of any public or private youtube playlist (title, id, time left, channel, url) as a json file
// Modifier: user237
// Forked from: https://gist.github.com/silver-mixer/43009c048a5e7e60f2182198ad0a105b
// Description: To download a list of video metadata of any public or private youtube playlist, as a json document, run this script on the playlist page.
// date created: 2023-09-02
(() => {
const VERSION = "user237_v1";
const REGEX_LIST_ID = /\?list=([a-zA-Z0-9-_]+)($|&)/;
const REGEX_TIMELEFT = /t=([0-9]+)/; // get the first capture group using [1]
// location.href refers to the current URL
let list_id = location.href.match(REGEX_LIST_ID)[1];
// let list_id = location.href.match(/\?list=([a-zA-Z0-9-_]+)($|&)/)[1];
// TEST1 - print the id
console.log(list_id);
let title = document.title;
console.log(title);
let created_at = Date.now();
let videos = [];
document.querySelectorAll('ytd-playlist-video-renderer').forEach(e => {
let id = e.querySelector('a').href.match(/\?v=([a-zA-Z0-9-_]+)($|&)/)[1];
let time_left = e.querySelector('a').href.match(REGEX_TIMELEFT);
time_left = time_left ? time_left[1] : '0';
videos.push({
title: e.querySelector('#video-title').innerText,
id: id,
time_left_secs: time_left,
// URL has to be in format https://www.youtube.com/watch?v=VIDEO_ID&t=TIME_LEFT
// generate from title and id
URL: "https://www.youtube.com/watch?v=" + id + "&t=" + time_left,
channel: e.querySelector('#channel-name').innerText
});
});
let data = {
version: VERSION,
title: title,
id: list_id,
created_at: created_at,
videos: videos
};
let a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([JSON.stringify(data)], {type: 'text/plain'}));
a.download = 'playlist_' + list_id + '.json';
a.click();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment