Created
November 11, 2022 15:31
-
-
Save OllieJT/40e7023786ed6e9e1be18ebb4377941d to your computer and use it in GitHub Desktop.
A utility script to group and sort videos from a YouTube playlist. I hope to make a full featured browser extension someday.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rawVideoList = []; | |
let videoList = {}; | |
// Taken from angus-c/just: array-group-by | |
function groupBy(arr, cb) { | |
if (!Array.isArray(arr)) { | |
throw new Error("expected an array for first argument"); | |
} | |
if (typeof cb !== "function") { | |
throw new Error("expected a function for second argument"); | |
} | |
var result = {}; | |
for (var i = 0; i < arr.length; i++) { | |
var item = arr[i]; | |
var bucketCategory = cb(item); | |
var bucket = result[bucketCategory]; | |
if (!Array.isArray(bucket)) { | |
result[bucketCategory] = [item]; | |
} else { | |
result[bucketCategory].push(item); | |
} | |
} | |
return result; | |
} | |
// The YouTube utility | |
let videos = document.querySelectorAll("ytd-playlist-video-renderer"); | |
videos.forEach((v) => { | |
const [title] = v.querySelector("#video-title").innerText.split("\n"); | |
const [link] = v.querySelector("a#video-title").href.split("&"); | |
const channel = v.querySelector("div#metadata").innerText; | |
if (!title || !link || !channel) return; | |
rawVideoList.push({ title, link, channel }); | |
}); | |
rawVideoList = groupBy(rawVideoList, (item) => item.channel); | |
Object.keys(rawVideoList).forEach((key) => { | |
const group = rawVideoList[key].reduce((acc, { link, title }) => { | |
return { | |
...acc, | |
[title]: link, | |
}; | |
}, {}); | |
Object.assign(videoList, { | |
[key]: group, | |
}); | |
}); | |
console.log(JSON.stringify(videoList, null, 3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment