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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/**
* Client-side Script of the Netflix Stats Generator
* @author Marvin Borner
* @copyright Marvin Borner 2019
*/
const cookie = document.querySelector("#cookie");
const cookieWrap = document.querySelector("#cookie_wrap");
const loading = document.querySelector("#loading");
const stats = document.querySelector("#stats");
const heatMap = document.querySelector("#heatMap");
cookie.addEventListener("keyup", e => {
if (e.key === "Enter") {
const request = new XMLHttpRequest();
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
analyze(request.responseText);
loading.style.display = "none";
stats.style.display = "block";
} else if (request.readyState === 4 && request.status !== 200)
alert("Cookie is not valid!")
};
request.open("POST", "assets/php/getData.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("cookie=" + cookie.value);
loading.style.display = "block";
cookieWrap.style.display = "none";
}
});
function analyze(data) {
data = JSON.parse(data).flat(1);
let totalWatchedSeconds = 0;
const hourObject = Array(24).fill(0);
const watchCountObject = {};
data.forEach(element => {
let title;
const seriesTitle = element.seriesTitle;
const movieTitle = element.title;
const watchDate = element.date;
const duration = element.duration;
// Generate watch time array (eg. 12am)
hourObject[(new Date(watchDate)).getHours()]++;
if (seriesTitle !== undefined) title = seriesTitle;
else title = movieTitle;
if (watchCountObject[title] !== undefined) {
watchCountObject[title].date.push(new Date(watchDate));
watchCountObject[title].watchTimeInSeconds += duration;
watchCountObject[title].watchTime = secondsToHours(watchCountObject[title].watchTimeInSeconds);
watchCountObject[title].count++;
totalWatchedSeconds += duration
} else {
watchCountObject[title] = {
date: [new Date(watchDate)],
watchTime: secondsToHours(duration),
watchTimeInSeconds: duration,
count: 1
};
totalWatchedSeconds += duration;
}
});
renderTotalSpent(totalWatchedSeconds);
renderHourChart(hourObject);
renderTopChart(watchCountObject);
renderHeatMap(watchCountObject);
console.log(watchCountObject);
}
function renderTotalSpent(total) {
document.querySelector("#totalSpent").innerHTML = `
Days: ${Math.floor(total / 60 / 60 / 24)},
Hours: ${Math.floor(total / 60 / 60)},
Minutes: ${Math.round(total / 60)},
Seconds: ${total}`
}
function renderHourChart(hourObject) {
const element = document
.getElementById("hourChart")
.getContext("2d");
new Chart(element, {
type: "line",
data: {
labels: [
"12am",
"1am",
"2am",
"3am",
"4am",
"5am",
"6am",
"7am",
"8am",
"9am",
"10am",
"11am",
"12pm",
"1pm",
"2pm",
"3pm",
"4pm",
"5pm",
"6pm",
"7pm",
"8pm",
"9pm",
"10pm",
"11pm"
],
datasets: [{
label: "Average watch times",
borderColor: "rgb(255, 99, 132)",
cubicInterpolationMode: "monotone",
pointRadius: 0,
pointHitRadius: 15,
data: hourObject
}]
},
options: {
scales: {
yAxes: [{
ticks: {
display: false
}
}]
},
legend: {
display: false
}
}
});
}
function renderTopChart(object) {
const sorted = Object.keys(object).sort((a, b) => {
return object[b].watchTimeInSeconds - object[a].watchTimeInSeconds
});
const data = sorted.map(element => object[element].watchTimeInSeconds);
const labels = sorted.map(element => {
return element + " (" + Math.floor(object[element].watchTimeInSeconds / 60 / 60) + " hours)"
});
const colorArray = Array.from({length: data.length}, () =>
"#" + ((1 << 24) * Math.random() | 0).toString(16));
const element = document
.getElementById("topChart")
.getContext("2d");
new Chart(element, {
type: 'doughnut',
data: {
datasets: [{
data: data,
backgroundColor: colorArray
}],
labels: labels,
},
options: {
animation: {
animateScale: true,
animateRotate: true
},
legend: {
display: false
}
}
});
}
function renderHeatMap(object) {
const allDates = Object.keys(object).map(element => object[element].date).flat(10)
.map(element => element.setHours(0, 0, 0, 0));
const watchedPerWeek = [[], [], [], [], [], [], []];
for (let i = 0; i < 366; i++) {
const date = new Date();
date.setDate(date.getDate() - i);
date.setHours(0, 0, 0, 0);
watchedPerWeek[date.getDay()].push(allDates.map(element => element === date.getTime()).filter(Boolean).length);
}
const maxWatchedPerDay = Math.max.apply(Math, watchedPerWeek.flat(2));
watchedPerWeek.map((element, i) => {
watchedPerWeek[i].push(["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][i]);
return watchedPerWeek[i].reverse();
});
watchedPerWeek.forEach(element => {
const tableRow = document.createElement("tr");
element.forEach(count => {
const tableData = document.createElement("td");
tableData.style.backgroundColor = "rgba(255,13,0," + count / maxWatchedPerDay + ")";
if (typeof count !== "number") tableData.appendChild(document.createTextNode(count));
tableRow.appendChild(tableData);
tableData.addEventListener("mouseover", () => {
document.querySelector("#information").innerText = `You've watched ${count} titles on that day!`;
});
});
heatMap.appendChild(tableRow)
})
}
function secondsToHours(seconds) {
const date = new Date(null);
date.setSeconds(seconds);
return date.toISOString().substr(11, 8)
}
|