-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.js
241 lines (196 loc) · 7.33 KB
/
backend.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
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
function mountApiSuggestionItem(result,entities){
//Get a item from result and a list of entities and do the magic!
var ID = result['semantic'];
var semanticForest = result['semanticForest'];
var query = [];
var display = result['parse']['display'];
var newQuery = [];
var subtext = '';
var category = '';
for(var i in display){
if(typeof(display[i]) == "string"){
newQuery.push(display[i]);
} else if(typeof(display[i]) == "object") {
var entity = display[i];
var getEntityResult = getEntity(entities, entity.uid)
entity.type = getEntityResult.type;
entity.text = getEntityResult.text;
category = getEntityResult.category_rendered;
subtext = getEntityResult.subtext;
newQuery.push(entity);
}
}
var response = {'query': newQuery, 'subtext' : subtext, 'category' : category};
response.semantic = ID;
return response;
}
function getEntity(entities, uid){
for(var i in entities)
if(entities[i].uid == uid) {
return entities[i];
}
return false;
}
function getSuggestions(search_string,callback){
if (typeof(search_string) == 'string'){
search_string = [search_string];
}
var json_query = {
value : JSON.stringify(search_string),
context : 'facebar',
grammar_version : '7ca1b059f1a46f3d3cb62914007c6e86432a41e1',
viewer : userId,
rsp : 'search',
sid : 0.11356468964368105,
qid : 5,
see_more : false,
max_results : 8,
num_entities : 0,
__user : userId,
__a : 1,
__dyn : '7n8ahxoNpGodo',
__req : 35 }
var data = $.get("https://www.facebook.com/ajax/typeahead/search/facebar/query/",json_query, function(data){
var fbJson = JSON.parse(data.substring(9));
var result = [];
fbJson.payload.results.sort(function(a, b){ return a.cost - b.cost; });
for (var i in fbJson['payload']['results']){
if ( fbJson['payload']['results'][i].type == "browse_type_user" || fbJson['payload']['results'][i].type == '{user}')
{
var item = mountApiSuggestionItem(fbJson['payload']['results'][i],fbJson['payload']['entities']);
result.push(item);
}
}
callback(result);
}, "html");
}
// Constants
var searchBaseUrl = "https://www.facebook.com/search/"
var userId = undefined;
var resultsPageBaseUrl = "https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet";
$(function(){
userId = $(".headerTinymanPhoto").attr("id").match(/\d+/)[0];
});
// Builds a search URL from a semantic query.
function makeUrlFromSemantic(semantic){
// We should probably build a tree and traverse it in postorder. But screw that, this works too.
return semantic.match(/[a-zA-Z0-9\-]+/g).reverse().join("/")
}
function getFirstPage(semantic){
var url = makeUrlFromSemantic(semantic);
return $.get(searchBaseUrl + url, null, "html");
}
// Strips trailing for(;;) from response.
function parseJsonResponse(response){
return JSON.parse(response.substring(9));
}
function makeAPIUser(fbUser){
return {
uid: fbUser.uid,
name: fbUser.text,
photoUrl: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/s160x160/20783_10151848827816729_587339413_a.png"
}
}
function getUsersFromHtml(html){
var cHtml = $.parseHTML(html);
var userDivs = $("img.img[src$=\".jpg\"]", cHtml).parent().parent().parent();
var users = [];
userDivs.each(function(k, v){
try{
var user = {
uid: JSON.parse($(v).attr("data-bt")).id,
name: $.trim($("a[href$=\"browse_search\"]", v).text()),
photoUrl: $("img.img", v).attr("src")
};
users.push(user);
} catch(e) { }
});
return users;
}
function getFirstPageUsers(searchPage){
var d = $.Deferred();
var commentedSnippets = searchPage.match(/<!--(.*?)-->/g);
var users = [];
for(var i in commentedSnippets)
users = users.concat(getUsersFromHtml(commentedSnippets[i].replace("<!--", "").replace("-->", "")));
// Does not work for nested objects. Fortunately, we're not dealing with any today.
var candidateObjects = searchPage.match(/\{.*?\}/g);
var queryObject = {};
var cursor = null;
for(var i in candidateObjects){
try{
var parsed = JSON.parse(candidateObjects[i]);
if(parsed.cursor !== undefined)
cursor = parsed.cursor;
if(parsed.view == "list" || parsed.view == "grid")
queryObject = parsed;
} catch(e) { }
}
if(users.length == 0){
d.reject([]);
return d.promise();
}
queryObject.cursor = cursor;
queryObject.ads_at_end = true;
queryObject.view = "grid";
var result = {
users: users,
queryObject: queryObject
}
d.resolve(result);
return d.promise();
}
function getRemainingUsers(result){
var d = $.Deferred();
function getNextPage(callback){
$.get(resultsPageBaseUrl, {
data: JSON.stringify(result.queryObject),
__user: userId,
__a: 1,
__dyn: "7n8ahxoNpGodo",
__req: "r"
}, callback, "html");
}
function processPage(resultPage){
var nextCursor = undefined;
var data = parseJsonResponse(resultPage);
var newUsers = getUsersFromHtml(data.payload);
function findUserObjects(arr){
var elements = [];
if(!arr.length || typeof(arr) == "string"){
if(arr.type && arr.type == "ent:user"){
return [arr];
} else if(arr.cursor) {
nextCursor = arr.cursor;
return [];
}
return [];
}
for(var i in arr){
if(!arr[i]) continue;
elements = elements.concat(findUserObjects(arr[i]));
}
return elements;
}
var data = parseJsonResponse(resultPage);
findUserObjects(data.jsmods.require);
result.users = result.users.concat(newUsers);
if(nextCursor == undefined || newUsers.length == 0){
d.resolve(result.users);
} else {
result.queryObject.cursor = nextCursor;
getNextPage(processPage);
}
}
if(!result.queryObject.cursor)
d.resolve(result.users);
else
getNextPage(processPage);
return d.promise();
}
function getUsers(semantic, callback){
getFirstPage(semantic, callback)
.then(getFirstPageUsers, callback)
.then(getRemainingUsers, callback)
.then(callback, callback);
}