-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreacts.js
139 lines (131 loc) · 5.4 KB
/
reacts.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
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
// Analyzes friends interactions and the number of my likes, loves, laughs, wows, cry, angry reacts!
// Reaction images:
// like - https://www.facebook.com/rsrc.php/v3/yB/r/lDwm6Y_i0v8.png
// love - https://www.facebook.com/rsrc.php/v3/yn/r/Q2ZsBFJIdXg.png
// laugh - https://www.facebook.com/rsrc.php/v3/yX/r/85Fysyalo_E.png
// wow - https://www.facebook.com/rsrc.php/v3/yT/r/fhpn7HuBJXG.png
// cry - https://www.facebook.com/rsrc.php/v3/yk/r/x-r8xo-ZCcu.png
// angry - https://www.facebook.com/rsrc.php/v3/yz/r/XTeRB5Z20Am.png
var cheerio = require('cheerio')
var fs = require('fs')
var data = fs.readFileSync('sample.html', 'utf8'),
$ = cheerio.load(data);
var images = ["https://www.facebook.com/rsrc.php/v3/yB/r/lDwm6Y_i0v8.png", "https://www.facebook.com/rsrc.php/v3/yn/r/Q2ZsBFJIdXg.png", "https://www.facebook.com/rsrc.php/v3/yX/r/85Fysyalo_E.png", "https://www.facebook.com/rsrc.php/v3/yT/r/fhpn7HuBJXG.png", "https://www.facebook.com/rsrc.php/v3/yk/r/x-r8xo-ZCcu.png", "https://www.facebook.com/rsrc.php/v3/yz/r/XTeRB5Z20Am.png"];
var reactions = [0, 0, 0, 0, 0, 0];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var friendsMadePerMonth = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var totalFriends = 0;
var friends = new Map();
// Only looks at interactions that happened in 2017
// Searches through the HTML and does three things:
// 1) Compares the image of the activity to the like/love/laugh/wow/cry/angry images
// a) If it matches one of the images, increments that index of the reactions array
// 2) Finds all the friends I made and increments that month's index of the friendsMadePerMonth array
// 3) Gets the name of friends whose content I either commented on or liked/reacted to
// a) Puts that name into a map: Key -> name, Value -> # of times I've interacted with them
// b) Sort the map by # of times I've interacted with them
$('div[id="year_2017"]').each(function() {
$(this).find('div[class="clearfix"]').each(function(i, elem) {
var stringedHTML = String(($(this).html()));
for (var j = 0; j < images.length; j++) {
if (stringedHTML.includes(images[j])) {
reactions[j]++;
}
}
// If we find an accepted friend request, we want to increment the
// friendsMadePerMonth array appropriately
if (stringedHTML.includes("became friends with")) {
for (var j = 0; j < months.length; j++) {
if (stringedHTML.includes(months[j])) {
friendsMadePerMonth[j]++;
totalFriends++;
}
}
}
// Most activity will be in the form:
// [YOUR NAME] likes [FRIEND NAME'S] post
// [YOUR NAME] reacted to [FRIEND NAME'S] post
// [YOUR NAME] commented on [FRIEND NAME'S] post
// We extract the friend's name and get rid of the "'s" at the end
var stringedText = $(this).text();
var strings = stringedText.split(" ");
if(stringedText.indexOf("likes") > 0 || stringedText.indexOf("liked") > 0) {
var friendsName = "";
var found = false;
for (var i = 4; i < strings.length && found == false; i++) {
if (strings[i].indexOf("'") > 0) {
found = true;
for (var j = 3; j <= i; j++) {
friendsName += strings[j] + " ";
}
}
}
friendsName = friendsName.substring(0, friendsName.length - 3);
if (friends.get(friendsName) != undefined) {
friends.set(friendsName, friends.get(friendsName) + 1);
} else {
friends.set(friendsName, 1);
}
} else if(stringedText.indexOf("to") > 0) {
if (strings[5] != undefined) {
var friendsName = "";
var found = false;
for (var i = 5; i < strings.length && found == false; i++) {
if (strings[i].indexOf("'") > 0) {
found = true;
for (var j = 4; j <= i; j++) {
friendsName += strings[j] + " ";
}
}
}
friendsName = friendsName.substring(0, friendsName.length - 3);
if (friends.get(friendsName) != undefined) {
friends.set(friendsName, friends.get(friendsName) + 1);
} else {
friends.set(friendsName, 1);
}
}
} else if(stringedText.indexOf("on") > 0) {
if (strings[5] != undefined) {
var friendsName = "";
var found = false;
for (var i = 5; i < strings.length && found == false; i++) {
if (strings[i].indexOf("'") > 0) {
found = true;
for (var j = 4; j <= i; j++) {
friendsName += strings[j] + " ";
}
}
}
friendsName = friendsName.substring(0, friendsName.length - 3);
if (friends.get(friendsName) != undefined) {
friends.set(friendsName, friends.get(friendsName) + 1);
} else {
friends.set(friendsName, 1);
}
}
}
});
// Now that we have all the friends I've interacted with, I want to be able to sort them.
// So, I make a friend object and put it into an array so that I can sort it by value later.
var friendsArray = [];
friends.forEach(function(value, key, map) {
var friendObject = new Object();
friendObject.name = key;
friendObject.value = value;
friendsArray.push(friendObject);
});
friendsArray.sort(function(a,b) {
return b.value - a.value;
});
console.log("Like, love, laugh, wow, cry, angry reactions: " + "[" + reactions + "]");
console.log();
console.log("Friends made per month: " + "[" + friendsMadePerMonth + "]");
console.log();
console.log("Total friends made in 2017: " + totalFriends);
console.log();
console.log("How much you interact with your friends:");
for (var i = 0; i < friendsArray.length; i++) {
console.log(friendsArray[i]);
}
});