Created
August 29, 2023 05:11
-
-
Save Lulzx/01a43ea17a946130f16e41b0247a77a9 to your computer and use it in GitHub Desktop.
Parser for chapters provided within description of YouTube videos
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
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 | |
} |
Author
Lulzx
commented
Aug 29, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment