-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-playlist-total-watch-time.js
66 lines (54 loc) · 1.82 KB
/
youtube-playlist-total-watch-time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Inspired by curiosity to know the total time of:
// [Docker and Kubernetes Tutorial for Beginners - TechWorld with Nana](https://www.youtube.com/playlist?list=PLy7NrYWoggjwPggqtFsI_zMAwvG0SqYCb)
(() => {
const CONFIG = {
DEBUG: false,
TIME_SEPARATOR: ":",
MINUTES_IN_SECONDS: 60,
HOURS_IN_SECONDS: 60 * 60,
};
const rawWatchTimes = [
...document.querySelectorAll(
"span.ytd-thumbnail-overlay-time-status-renderer"
),
];
const watchTimes = rawWatchTimes.map((element) => element.innerHTML.trim());
if (CONFIG.DEBUG) {
console.log({ watchTimes });
}
/**
* @param {String} element
*/
const parseRawTimeToSeconds = (element) => {
const [first, second, third] = element
.split(CONFIG.TIME_SEPARATOR)
.map(Number);
const isInMinutes = !third;
if (isInMinutes) {
return first * 60 + second;
}
// is in hours
return first * 60 * 60 + second * 60 + third;
};
const watchTimesValuesInSeconds = watchTimes.map(parseRawTimeToSeconds);
const totalWatchTimeInSeconds = watchTimesValuesInSeconds.reduce(
(prev, acc) => prev + acc,
0
);
const totalWatchTimeInMinutes = totalWatchTimeInSeconds / 60;
const totalWatchTimeInHours = totalWatchTimeInMinutes / 60;
const totalWatchTimeInDays = totalWatchTimeInHours / 24;
const details = {
totalWatchTimeInSeconds,
totalWatchTimeInMinutes,
totalWatchTimeInHours,
totalWatchTimeInDays,
};
let remaining = totalWatchTimeInSeconds;
const hours = Math.round(remaining / CONFIG.HOURS_IN_SECONDS);
remaining = remaining % CONFIG.HOURS_IN_SECONDS;
const minutes = Math.round(remaining / CONFIG.MINUTES_IN_SECONDS);
const seconds = Math.round(remaining % CONFIG.MINUTES_IN_SECONDS);
const formatted = { hours, minutes, seconds };
console.log(formatted, { details });
})();