-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
337 lines (304 loc) · 11.1 KB
/
reader.js
File metadata and controls
337 lines (304 loc) · 11.1 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//Created by James Pearson 26/05/18, last updated 19/02/19
//This file reads json data from Facebook Messenger
//This part gets a JSON file from the file picker thingy
//Based on code from:
//http://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api
window.onload = function(){
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function(e){
var file = fileInput.files[0];
var textType = ".json";
if(file.type.match(textType)){
var reader = new FileReader();
reader.onload = function(e){
parse(reader.result);
}
reader.readAsText(file);
}
else{
alert("File not supported!");
}
});
}
//Parse the data
function parse(jsonString){
$('#name').empty();
var parsedData = JSON.parse(jsonString);
if(parsedData.hasOwnProperty('messages')){
//Add the title of the chat
var members = getParticipants(parsedData);
$('#name').append(getTitle(parsedData.title, members));
run(parsedData);
}
else{
alert("This doesn't appear to be a Facebook Messenger JSON file");
}
}
//Put the functions you want to run with parsed JSON data here
function run(parsedData){
date(parsedData);
firstMessage(parsedData);
count(parsedData); //run this last as it clears fields if there are no messages
}
//Count the number of messages
function count(parsedData){
$('#outputTable').empty();
$('#error').empty();
$('#instructions').empty();
var output = "";
var count = 0;
var participantsArray = getParticipants(parsedData);
var instructions = "<br>";
instructions += "<p>Click a table header to sort the table in ascending order by that category</p>";
instructions += "<p>Tap again to sort by descending order</p>";
output += "<tr>";
output += "<th onclick='sortTable(0);'>Name</th>";
output += "<th onclick='sortTable(1);'>Number of Messages</th>";
output += "<th onclick='sortTable(2);'>Number of Reactions Sent</th>";
output += "<th onclick='sortTable(3);'>Number of Reactions Recieved</th>";
output += "<th onclick='sortTable(4);'>Average Reactions per Message</th>";
output += "</tr>";
for(person in participantsArray){
output += "<tr>";
name = participantsArray[person];
messageCount = getMessageCount(parsedData.messages, name);
reactionCount = getReactionCount(parsedData.messages, name);
reactionRecievedCount = getReactionRecievedCount(parsedData.messages, name);
reactionPerMessage = (reactionRecievedCount/messageCount).toFixed(4).toString();
if (isNaN(reactionPerMessage)){
reactionPerMessage = "0.0000";
}
//Flag Facebook User as different to everyone else
if(name == "Facebook User"){
output += "<td id='FBU' class='name'>" + name + "</td>";
output += "<td id='FBU'>" + messageCount + "</td>";
output += "<td id='FBU'>" + reactionCount + "</td>";
output += "<td id='FBU'>" + reactionRecievedCount + "</td>";
output += "<td id='FBU'>" + reactionPerMessage + "</td>";
}
else{
output += "<td class='name'>" + name + "</td>";
output += "<td>" + messageCount + "</td>";
output += "<td>" + reactionCount + "</td>";
output += "<td>" + reactionRecievedCount + "</td>";
output += "<td>" + reactionPerMessage + "</td>";
}
output += "</tr>";
count++;
}
//Check to see if there actually is anything to output
if(count != 0){
$('#instructions').append(instructions);
//If there are messages then add them
$('#outputTable').append(output);
}
else{
//count == 0 so no messages
$('#instructions').empty();
$('#firstMessage').empty();
$('#firstMessageDate').empty();
$('#error').append("<p>No messages found :c</p>");
}
}
//Find data about the first message
function firstMessage(parsedData){
//Empty first message field
$('#firstMessage').empty();
var messages = parsedData.messages;
//First message position
var firstMessage = messages.length-1;
//Time
var firstTimestamp = messages[firstMessage].timestamp_ms;
//Sender
var firstSender = messages[firstMessage].sender_name;
//Content
var firstMessageContent = messages[firstMessage].content;
//Put data into fields
$('#firstMessage').append('<p>The first message was "' + firstMessageContent + '" by ' + firstSender +
". It was sent on " + prettyDate(firstTimestamp) + "</p>");
}
//Gets all the participants in the chat
function getParticipants(parsedData){
var participants = parsedData.participants;
var participantArray = [];
for(i=0; i<participants.length; i++){
var participant = participants[i].name;
participantArray.push(participant);
}
if(participantArray.includes("Facebook User")){
$('#error').append('<p>Note: "Facebook User" may include several people\'s data</p>');
}
return participantArray;
}
//Arranges all the timestamps into an array
function date(parsedData){
var messages = parsedData.messages;
var timestampArray = [];
for(i=0; i<messages.length; i++){
var timestamp = messages[i].timestamp_ms;
//console.log(timestamp);
timestampArray.push(timestamp);
}
}
//Generate a human readable date from a timestamp
function prettyDate(timestamp){
var date = new Date(timestamp);
var day = date.getDay();
var d = startZero(date.getDate());
var m = startZero(date.getMonth()+1); //January was 0 but is now 1
var yyyy = date.getFullYear();
var h = startZero(date.getHours());
var min = startZero(date.getMinutes());
var s = startZero(date.getSeconds());
//puts days of the week into an array so you print name not number
var dayName = [];
dayName[0]= "Sunday";
dayName[1] = "Monday";
dayName[2] = "Tuesday";
dayName[3] = "Wednesday";
dayName[4] = "Thursday";
dayName[5] = "Friday";
dayName[6] = "Saturday";
var prettyDate = d+"/"+m+"/"+yyyy+" ("+dayName[day]+") at "+h+":"+min+":"+s+" (UTC)";
return prettyDate;
}
//Add zeros onto the start of a number if it's less than 10
function startZero(number){
var paddedNumber = "";
if (number < 10){
paddedNumber = "0" + number;
}
else{
paddedNumber = number
}
return paddedNumber;
}
//This code counts the messages for a specific user
function getMessageCount(messages, name){
var messageCount = 0;
for(var i = 0; i < messages.length; i++){
if(messages[i].sender_name == name){
messageCount++;
}
}
return messageCount;
}
//Get the number of reactions sent by a user
function getReactionCount(messages, name){
var reactionCount = 0;
for(var i = 0; i < messages.length; i++){
if (messages[i].reactions){
var reactions = messages[i].reactions
for(var j = 0; j < reactions.length; j++){
if (reactions[j].actor == name){
reactionCount++;
}
}
}
}
return reactionCount;
}
//Get the number of reactions sent to a user
function getReactionRecievedCount(messages, name){
var reactionRecievedCount = 0;
for(var i = 0; i < messages.length; i++){
if (messages[i].sender_name == name && messages[i].reactions){
reactionRecievedCount += messages[i].reactions.length
}
}
return reactionRecievedCount;
}
//Find a title for the data
//Eg if only 2 people then x&y or if more then it's a group chat
function getTitle(title, members){
var newTitle = "";
if(members.length > 2){
newTitle = "Group Chat: " + title;
}
else if(members.length == 2){
newTitle = members[0] + " & " + members[1];
}
else if(members.length == 1){
newTitle = members[0] + " on their own";
}
else{
newTitle = "???";
}
return "<h2>" + newTitle + "</h2>";
}
//Modified version of:
//https://www.w3schools.com/howto/howto_js_sort_table.asp
function sortTable(n){
//Setup variables
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0, isNum;
table = document.getElementById("outputTable");
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
//Make a loop that will continue until no switching has been done:
while(switching){
//Start by saying: no switching is done:
switching = false;
rows = table.getElementsByTagName("tr");
//Loop through all table rows (except the first, which contains table headers):
for(i = 1; i < (rows.length - 1); i++){
//Start by saying there should be no switching:
shouldSwitch = false;
//Get the two elements you want to compare, one from current row and one from the next:
x = rows[i].getElementsByTagName("td")[n];
y = rows[i + 1].getElementsByTagName("td")[n];
//Check if the cell contains a date or not
if($(x).hasClass("name")){
//console.log("true");
isName = true;
}
else{
//console.log("false");
isName = false;
}
//Check if the two rows should switch place, based on the direction, asc or desc:
//Also check if the row is numbers or not and sort accordingly
if(dir == "asc" && !isName){
if(parseInt(x.innerHTML, 10) > parseInt(y.innerHTML, 10)){
//If so, mark as a switch and break the loop:
shouldSwitch = true;
//console.log("Case 1");
break;
}
}
else if(dir == "desc" && !isName){
if(parseInt(x.innerHTML, 10) < parseInt(y.innerHTML, 10)){
shouldSwitch = true;
//console.log("Case 2");
break;
}
}
else if(dir == "asc" && isName){
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
else if(dir == "desc" && isName){
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
if(shouldSwitch){
//If a switch has been marked, make the switch and mark that a switch has been done:
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
}
else{
//If no switching has been done AND the direction is "asc", set the direction to "desc" and run the while loop again.
if(switchcount == 0 && dir == "asc"){
dir = "desc";
switching = true;
}
}
}
}