forked from twolfson/Facebook-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
170 lines (146 loc) · 4.69 KB
/
scrape.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
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
var Scraper = (function () {
// TODO: Abstract scraping
// TODO: Build up list of pages to scrape then scrape (this will be good for custom lists)
// TODO: Performance enhancers
// Move away from using each
var _public = {},
friendShorts = {},
doneParsing = true,
appendFriendShorts,
getAllFriendShorts,
incrSectionsRetrieved,
generateFriendBlock,
sectionsRequested = 0,
sectionsRetrieved = 0,
touchedURLs = {};
incrSectionsRetrieved = function (onComplete) {
sectionsRetrieved += 1;
if( sectionsRetrieved < sectionsRequested ) {
return;
}
return onComplete();
};
appendFriendShorts = function (pageData) {
var $friendShorts = friendShorts;
// Create HTMLDocument via jQuery (just easier)
jQuery(pageData).find('#sections a[name][href]').each( function () {
var name = this.name,
href = this.href,
img = '',
imgElt = this,
isImg = function (elt) {
return elt.tagName.toLowerCase() === "img";
},
$arr,
body = document.body;
// Determine proper URI
href += ( name.indexOf('?') === -1 ) ? '?' : '&';
href += 'v=info';
// Find profile picture
while( !isImg(imgElt) ) {
if( imgElt === body ) {
break;
}
$arr = jQuery(imgElt).siblings('img');
if( $arr && $arr.length > 0 ) {
imgElt = $arr[0];
} else {
imgElt = imgElt.parentNode;
}
}
if( imgElt !== body ) {
// q is small and square, t is taller, s is medium, b (or n) is big, o to download big
img = (imgElt.src + '').replace('_q', '_s');
}
// Store by name
$friendShorts[ name ] = {'name': name, 'href': href, 'img': img};
});
};
getAllFriendShorts = function getAllFriendShorts(url, callback) {
var $appendFriendShorts = appendFriendShorts,
$friendShorts = friendShorts;
touchedURLs[url] = true;
sectionsRequested += 1;
// TODO: Implement failsafe upon failed retrieval
// Create HTMLDocument via jQuery (just easier)
jQuery.get(url, function (data) {
var $$appendFriendShorts = $appendFriendShorts,
$nameSections = jQuery(data).find('#sections a.sec'),
callbackFn;
if( $nameSections && $nameSections.length > 0 ) {
sectionsRequested -= 1;
$nameSections.each( function () {
var href = this.href;
if( !touchedURLs[href] ) {
getAllFriendShorts( this.href, callback );
} else {
$$appendFriendShorts(data);
incrSectionsRetrieved( callback );
}
});
} else {
$$appendFriendShorts(data);
incrSectionsRetrieved( callback );
}
});
// Overwrite so no extra calls are made
getAllFriendShorts = $friendShorts;
return $friendShorts;
};
// TODO: Change to generateAllFriendBlocks (with custom callback for generating button to go to next step; aggregate selected friend data)
getAllFriendInfo = function (urls) {
var i, len, key, keys, $friendShorts;
// Only one scrape at a time (because I am not that smart)
if( !doneParsing ) {
return {};
}
if( !urls ) {
return getAllFriendShorts('http://m.facebook.com/friends.php?refid=0', function () {
$friendShorts = friendShorts;
keys = [];
console.log( 'Completed sort' );
document.body.innerHTML = '';
// Collect keys and sort reverse alphabetically
for( key in $friendShorts ) {
if( $friendShorts.hasOwnProperty(key) ) {
keys[ keys.length ] = key;
}
}
keys.sort( function (a, b) { return a < b; } );
// Moved from tail to head creating 'FriendBlock's
for( i = keys.length; i--; ) {
document.body.appendChild( generateFriendBlock( $friendShorts[ keys[i] ] ) );
}
});
}
// TODO: Handle url's portion
};
generateFriendBlock = function (friendInfo) {
var doc = document,
container = doc.createElement('div'),
rightDiv = doc.createElement('div'),
img = doc.createElement('img'),
name = doc.createElement('h3'),
input = doc.createElement('input');
// TODO: Transfer style settings
container.className = 'friendBlock';
container.setAttribute( 'style', 'border: 1px solid black; margin: 3px; padding: 2px; display: inline-block;' );
rightDiv.className = 'friendBlockRight';
rightDiv.setAttribute( 'style', 'display: inline-block;' );
img.src = friendInfo.img;
input.type = 'checkbox';
input.name = 'friendCheckbox';
input.value = friendInfo.href;
input.setAttribute( 'checked', 'checked' );
name.innerText = friendInfo.name;
name.textContent = friendInfo.name;
container.appendChild( img );
rightDiv.appendChild( name );
rightDiv.appendChild( input );
container.appendChild( rightDiv );
return container;
};
_public.getAllFriendInfo = getAllFriendInfo;
return _public;
}());
Scraper.getAllFriendInfo();