Skip to content

Instantly share code, notes, and snippets.

@Lulzx
Created August 29, 2023 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lulzx/01a43ea17a946130f16e41b0247a77a9 to your computer and use it in GitHub Desktop.
Save Lulzx/01a43ea17a946130f16e41b0247a77a9 to your computer and use it in GitHub Desktop.
Parser for chapters provided within description of YouTube videos
const parseChapters = (description) => {
// Extract timestamps (either 00:00:00, 0:00:00, 00:00 or 0:00)
const lines = description.split("\n")
const regex = /(\d{0,2}:?\d{1,2}:\d{2})/g
const chapters = []
for (const line of lines) {
// Match the regex and check if the line contains a matched regex
const matches = line.match(regex)
if (matches) {
const ts = matches[0]
const title = line
.split(" ")
.filter((l) => !l.includes(ts))
.join(" ")
chapters.push({
timestamp: ts,
title: title,
})
}
}
return chapters
}
@Lulzx
Copy link
Author

Lulzx commented Aug 29, 2023

  const allElements = Array.from(
    document.querySelectorAll(
      "#panels ytd-engagement-panel-section-list-renderer:nth-child(2) #content ytd-macro-markers-list-renderer #contents ytd-macro-markers-list-item-renderer #endpoint #details"
    )
  );

  const withTitleAndTime = allElements.map((node) => ({
    title: node.querySelector(".macro-markers")?.textContent,
    timestamp: node.querySelector("#time")?.textContent,
  }));

  const filtered = withTitleAndTime.filter(
    (element) =>
      element.title !== undefined &&
      element.title !== null &&
      element.timestamp !== undefined &&
      element.timestamp !== null
  );

  const withoutDuplicates = [
    ...new Map(filtered.map((node) => [node.timestamp, node])).values(),
  ];

  return withoutDuplicates;
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment