This repository was archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacebook.js
More file actions
159 lines (138 loc) · 5.46 KB
/
facebook.js
File metadata and controls
159 lines (138 loc) · 5.46 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
"use strict";
console.log("got here");
var webpage = require('webpage');
var utils = require('./utils.js');
var StepManager = require('./stepManager.js');
var jobQueue = require('./jobQueue.js');
var fs = require('fs');
var getStackTrace = function() {
var obj = {};
Error.captureStackTrace(obj, getStackTrace);
return obj.stack;
};
function FacebookCrawler(stepManager, fbConfig) {
var self = this;
var page = webpage.create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
function firstStepGoingToFriendsPage() {
page.open("https://www.facebook.com/"+fbConfig.userBase+"/friends", function(status) {
console.log("open friends page with status", status);
utils.waitComplete(page, stepManager.nextStep); /*if not logged, next step should be login*/
});
}
function handleLoginPage() {
page.evaluate(function(fbConfig) {
document.querySelector("input[name='email']").value = fbConfig.email;
document.querySelector("input[name='pass']").value = fbConfig.password;
document.querySelector("#login_form").submit();
console.log("Login submitted!");
}, fbConfig);
//waitComplete(nextStep);
utils.waitCompletePlusTime(page, stepManager.nextStep); //TODO se não esperar nada ou mesmo tempo pequeno (100ms) o waitComplete só não funciona, continua na mesma pag
}
function handleFriendsPage() {
console.log("ENTROU NO FRIENDS");
var interval = window.setInterval(function() {
var count = page.content.match(/class="uiHeader"/g);//document.querySelector('div[class=uiHeader]')
if(count === null) { // Didn't find
page.evaluate(function() {
// Scrolls to the bottom of page
window.document.body.scrollTop = document.body.scrollHeight;
});
return;
}
else { // Found
var friends = page.evaluate(function() {
var divNodeList = document.querySelectorAll('div[class="fsl fwb fcb"]');
var friends = [];
for(var i = 0; i < divNodeList.length; i++) {
var link = divNodeList[i].children[0];
friends.push ({"name": link.textContent, "url": link.href});
}
return friends;
});
self.friends = friends;
console.log(friends.length);
clearInterval(interval);
stepManager.nextStep();
}
}, 500); // Number of milliseconds to wait between scrolls
}
function handleAFriendPage(page, friend) {
var mora = page.evaluate(function () {
var i = document.querySelector('i[class="_2m_3 _3-91 _8o _8s lfloat _ohe img sp_vSpiFuU7MD8 sx_50af10"]');
if(i == null) {
console.log("Não encontrou ícone cidade onde mora para amigo...");
return null;
}
console.log(i.parentElement.children[1].firstChild.firstChild.firstElementChild.textContent);
return i.parentElement.children[1].firstChild.firstChild.firstElementChild.textContent;
});
if(mora == null)
console.log("Não encontrou cidade onde mora para amigo", friend.name);
var origem = page.evaluate(function () {
var i = document.querySelector('i[class="_2m_3 _3-91 _8o _8s lfloat _ohe img sp_vSpiFuU7MD8 sx_6029c3"]');
if(i == null) {
console.log("Não encontrou ícone cidade de onde veio para amigo...");
return null;
}
console.log(i.parentElement.children[1].firstChild.firstChild.firstElementChild.textContent);
return i.parentElement.children[1].firstChild.firstChild.firstElementChild.textContent;
});
if(origem == null)
console.log("Não encontrou cidade onde veio para amigo", friend.name);
friend.mora = mora;
friend.origem = origem;
}
function handleAFriendWithFriendsListInfo(friend,cb) {
var friendPage = require('webpage').create();
friendPage.open(friend.url, function(status) {
//console.log("open friends page with status", status);
utils.waitComplete(friendPage, function() {
handleAFriendPage(friendPage, friend);
friendPage.close();
cb();
}); /*if not logged, next step should be login*/
});
}
function handleFriendsList() {
var friends = self.friends;
console.log("found", friends.length, "friends");
var friendsJobs = [];
var stream = fs.open(fbConfig.outputFile, {
mode: 'w',
charset: 'UTF-8'
});
stream.write('name;city;from;link\n');
friends.forEach(function (friend) {
friendsJobs.push(function (cbDone) {
handleAFriendWithFriendsListInfo(friend, function() {
console.log("\n\PROCESSADO NOVO USUÁRIO", "nome:", friend.name, "mora em:", friend.mora, "origem:", friend.origem);
stream.write(friend.name+';'+friend.mora+';'+friend.origem+';'+friend.url+'\n');
cbDone();
});
});
});
console.log("Started visiting friends page");
jobQueue(function done() {
console.log("FIM!");
stream.close();
}, 10, friendsJobs);
}
function constructor() {
stepManager.addSteps(
firstStepGoingToFriendsPage,
handleLoginPage,
handleFriendsPage,
handleFriendsList
);
}
constructor();
this.run = function run() {
console.log("run");
stepManager.nextStep();
};
}
module.exports = FacebookCrawler;