-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlinkedin.js
170 lines (153 loc) · 7.56 KB
/
linkedin.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
zeeschuimer.register_module(
'LinkedIn',
"linkedin.com",
function (response, source_platform_url, source_url) {
let domain = source_platform_url.split("/")[2].toLowerCase().replace(/^www\./, '');
if (domain !== 'linkedin.com') {
return [];
}
// objects embedded in HTML are identified by this bit of text
let items = [];
let data = [];
let data_type = "";
try {
if(response.indexOf('<!DOCTYPE html>') >= 0) {
throw new Error();
}
// when dealing with JSON, just parse that JSON and process it
const json_data = JSON.parse(response);
data.push(json_data);
data_type = "JSON";
} catch (e) {
// data is not JSON, so it's probably HTML
// HTML has data embedded in <code> tags
// store these for processing
const code_regex = RegExp(/<code.*>([^<]+)<\/code>/g);
for (const code_bit of response.matchAll(code_regex)) {
// console.log("Code; checking for JSON");
try {
// use he to decode from HTML entities (the way the data is embedded)
data.push(JSON.parse(he.decode(code_bit[1].trim())));
data_type = "HTML";
// console.log("Found JSON in code block");
} catch (e) {
}
}
}
const eligible_list_types = ["feedDashMainFeedByMainFeed", "feedDashInterestUpdatesByInterestFeedByKeywords", "feedDashProfileUpdatesByMemberShareFeed", "searchDashClustersByAll"]
const uninteresting_list_types = ["*dashMySettings", "messagingDashMessagingSettings", "*searchDashSearchHome", "searchDashTypeaheadByGlobalTypeahead", "messagingDashAffiliatedMailboxesAll", "legoDashPageContentsByPageKeyAndSlotId", "searchDashFilterClustersByFilters"]
for (const data_bit of data) {
// now we have the data, try to parse it
// is this object post data?
let item_index = [];
let location = "";
if ("data" in data_bit && "included" in data_bit) {
// items may be referenced as 'results' for search result pages or 'elements' for the feed
let item_key = '';
if ("*elements" in data_bit["data"]) {
item_index = data_bit["data"]["*elements"];
location = "data.*elements";
} else if ("results" in data_bit["data"]) {
item_index = data_bit["data"]["results"];
location = "data.results";
} else if ("data" in data_bit["data"] && Object.keys(data_bit["data"]["data"]).filter(k => eligible_list_types.includes(k))) {
for(const k of eligible_list_types) {
if(k in data_bit["data"]["data"]) {
const elements_key = (data_bit["data"]["data"]['*elements'] !== undefined) ? '*elements' : 'elements';
item_index = data_bit["data"]["data"][k][elements_key];
location = `data.data.${k}.${elements_key}`;
if (typeof (item_index) !== 'string' && item_index.length > 0 && item_index[0]['items'] !== undefined) {
// embedded results on search page
item_index = item_index[0]['items'].map(item => {
return item['item']['searchFeedUpdate']['*update'];
});
}
break;
}
}
if (location === "") {
// Found nothing eligible
let uninteresting = false;
for (const k of uninteresting_list_types) {
if(k in data_bit["data"]["data"]) {
uninteresting = true;
}
}
if (!uninteresting) {
// Possibly interesting data
// console.log("No items found in data_bit:");
// console.log(data_bit);
}
continue;
}
} else {
// console.log("No items found in data:");
// console.log(data_bit);
continue;
}
//console.log(`Searching items at ${location} from ${data_type} data on ${source_platform_url}`);
// there is a list of objects, each with an ID
// and a separate list of items to display, a list of those IDs
// so the first step is to map item IDs to objects
let mapped_objects = [];
data_bit["included"].forEach(object => {
mapped_objects[object["entityUrn"]] = object;
});
// then we get the objects with the IDs in the item list
// and that is our result set!
let num_items = 0;
for (let object_ref in item_index) {
let result = item_index[object_ref];
if (typeof result !== 'string') {
continue;
}
// there are many types of content in these responses
// we are (for now?) only interested in posts, which are identified in this way
if (result.indexOf('urn:li:fs_updateV2:(urn:li:activity:') !== 0
&& result.indexOf('urn:li:fsd_update:(urn:li:activity:') !== 0) {
// console.log(`Skipping non-post item ${result}`);
continue;
}
let result_object = recursively_enrich(mapped_objects[result], mapped_objects);
result_object["id"] = result;
items.push(result_object);
num_items++;
}
console.log(`Found ${num_items} items in ${location} from ${data_type} data on ${source_platform_url}`);
}
}
return items;
});
/**
* Enrich an object
*
* Some fields may contain references to other objects stored in a response's "include" field
* This function recursively resolves these references
*
* @param object Object to enrich
* @param mapped_objects Map of all available objects
* @returns object Enriched object
*/
function recursively_enrich(object, mapped_objects) {
if (typeof (object) != 'object') {
return object;
}
for (let field in object) {
if (typeof field === 'string' && field.indexOf('*') === 0) {
if (typeof object[field] === 'string' && object[field].indexOf('urn:') === 0) {
// singular reference
object[field] = recursively_enrich(mapped_objects[object[field]], mapped_objects);
} else if (typeof object[field] === 'object') {
// list of references
for (let i in object[field]) {
if (typeof object[field][i] === 'string' && object[field][i].indexOf('urn:') === 0) {
object[field][i] = recursively_enrich(mapped_objects[object[field]], mapped_objects);
}
}
}
} else {
object[field] = recursively_enrich(object[field], mapped_objects);
}
}
return object;
}