-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.ts
166 lines (151 loc) · 4.03 KB
/
data.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type {
TopChannels,
YouTubeVideo,
Days,
Video,
MonthData,
} from "./types";
export const year = 2023;
export function sortTopChannels(obj: TopChannels): TopChannels {
// Ty ChatGPT
let entries = Object.entries(obj);
// Filter out numeric keys
entries = entries.filter(([key]) => !/^\d+$/.test(key));
// Sort by descending order of videosWatched
entries.sort((a, b) => b[1].videosWatched - a[1].videosWatched);
return Object.fromEntries(entries);
}
export function filterVideos(vids: Array<YouTubeVideo>): Array<YouTubeVideo> {
return vids.filter((obj) => {
// If it wasn't a watched video
if (!obj.activityControls?.includes("YouTube watch history")) return false;
// Some takeouts don't contain the titleUrl for some reason so
// Just make sure it isn't from music.youtube.com IF we have the field
if (obj.titleUrl) {
if (obj.titleUrl.includes("music.youtube.com")) return false;
}
// Not from {year}? BYE BYE
if (!(new Date(obj.time).getFullYear() === year)) return false;
return true;
});
}
export function processData(videos: Array<YouTubeVideo>): {
days: Days;
top: TopChannels;
} {
const groupedByDate: Days = {};
let topChannels: TopChannels = {};
videos.forEach((vid) => {
// Extract only the date part
const dateKey = vid.time.split("T")[0];
let vidObj: Video;
if (vid.subtitles) {
// If the video isn't deleted and is still up
vidObj = {
title: vid.title.split(" ").slice(1).join(" "),
video: vid.titleUrl,
channel: vid.subtitles[0].name,
time: vid.time,
channelURL: vid.subtitles[0].url,
};
} else {
// If it is deleted
vidObj = {
title: vid.title.split(" ").slice(1).join(" "),
video: vid.titleUrl,
channel: "[deleted or unknown]",
time: vid.time,
};
}
if (vidObj.channel in topChannels) {
topChannels[vidObj.channel].videosWatched += 1;
} else {
topChannels[vidObj.channel] = {
channelURL: vidObj.channelURL,
videosWatched: 1,
};
}
if (dateKey in groupedByDate) {
groupedByDate[dateKey].push(vidObj);
} else {
groupedByDate[dateKey] = [vidObj];
}
});
return {
days: groupedByDate,
top: sortTopChannels(topChannels),
};
}
const monthMap = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December",
};
export function getStats(days: Days) {
const reversedDays = Object.fromEntries(Object.entries(days).reverse());
const videosPerDay: number[] = [];
const videosOverYear: number[] = [];
const months: MonthData = {};
const dates = Object.keys(reversedDays);
for (const day in reversedDays) {
videosPerDay.push(reversedDays[day].length);
let m = day.split("-")[1];
if (m in months) {
months[m] += reversedDays[day].length;
} else {
months[m] = reversedDays[day].length;
}
// culmulative
const today = reversedDays[day].length;
videosOverYear.push(
videosOverYear.length == 0 ? today : videosOverYear.slice(-1)[0] + today
);
}
let maxMonthKey = "01";
let maxMonthValue = -Infinity;
let maxDayKey = "null";
let maxDayValue = -Infinity;
for (const key in months) {
if (months[key] > maxMonthValue) {
maxMonthValue = months[key];
maxMonthKey = key.toString();
}
}
for (let i = 0; i < dates.length; i++) {
if (videosPerDay[i] > maxDayValue) {
maxDayValue = videosPerDay[i];
maxDayKey = dates[i];
}
}
return {
// @ts-ignore
mostWatchedMonth: monthMap[maxMonthKey],
mostWatchedMonthValue: maxMonthValue,
mostWatchedDay: new Date(maxDayKey).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
}),
mostWatchedDayValue: maxDayValue,
dates,
videosOverYear,
videosPerDay,
};
}
export function totalVideos(days: Days) {
return Object.values(days).reduce((sum, day) => sum + day.length, 0);
}
export function average(days: Days, duration: number) {
const totalVideosWatched = totalVideos(days);
// meh it works
return parseInt((totalVideosWatched / duration).toFixed()).toLocaleString();
}