Created
September 8, 2018 18:32
-
-
Save shekohex/b10b7ce6a0ca8a9ddbfa41ba89c8fc64 to your computer and use it in GitHub Desktop.
Get YouTube Playlist total time
This file contains hidden or 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
//@ts-check | |
console.time('Elapsed time'); | |
const playlistVideos = document.querySelector( | |
"#contents > ytd-playlist-video-list-renderer > #contents" | |
); | |
const spanTime = document.querySelectorAll( | |
"#overlays > ytd-thumbnail-overlay-time-status-renderer > span" | |
); | |
let playlistTime = 0; | |
for (let i = 0; i < playlistVideos.childElementCount; i++) { | |
const videoTime = humanTimeToSeconds(spanTime[i].textContent.trim()); | |
playlistTime += videoTime; | |
} | |
// output | |
console.log(`Total Time: ${secondsToHumanTime(playlistTime)}`); | |
console.timeEnd("Elapsed time"); | |
/** | |
* Convert Human Time to seconds | |
* @param {String} time the input human time | |
* @returns {Number} | |
*/ | |
function humanTimeToSeconds(time) { | |
let totalTime = 0; | |
const parts = time.split(":"); | |
if (parts.length > 2) { | |
totalTime = | |
parseInt(parts[0]) * 60 * 60 + | |
parseInt(parts[1]) * 60 + | |
parseInt(parts[2]); | |
} else { | |
totalTime = parseInt(parts[0]) * 60 + parseInt(parts[1]); | |
} | |
return totalTime; | |
} | |
/** | |
* Convert Seconds to Human Time | |
* @param {Number} seconds | |
* @returns {String} | |
*/ | |
function secondsToHumanTime(seconds) { | |
const dump = new Date(null); | |
dump.setSeconds(seconds); | |
const isoDate = dump.toISOString(); | |
const dateParts = isoDate.split("T"); | |
return dateParts[1].replace(".000Z", ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment