-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
153 lines (136 loc) · 3.77 KB
/
utils.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
const random = require("crypto");
const base64 = require("base64-js");
const getIDFromUrn = (urn) => {
return urn.split(":")[3];
};
const getUrnFromRawUpdate = (rawString) => {
return rawString.split("(")[1].split(",")[0];
};
const getUpdateAuthorName = (dIncluded) => {
try {
return dIncluded.actor.name.text;
} catch (error) {
return error instanceof TypeError ? "None" : "";
}
};
const getUpdateOld = (dIncluded) => {
try {
return dIncluded.actor.subDescription.text;
} catch (error) {
return error instanceof TypeError ? "None" : "";
}
};
const getUpdateContent = (dIncluded, baseUrl) => {
try {
return dIncluded.commentary.text.text;
} catch (error) {
if (error instanceof TypeError) {
try {
const urn = getUrnFromRawUpdate(dIncluded["*resharedUpdate"]);
return `${baseUrl}/feed/update/${urn}`;
} catch (error) {
return error instanceof TypeError ? "None" : "IMAGE";
}
} else {
return "";
}
}
};
const getUpdateAuthorProfile = (dIncluded, baseUrl) => {
try {
const urn = dIncluded.actor.urn;
const urnId = urn.split(":").pop();
if (urn.includes("company")) {
return `${baseUrl}/company/${urnId}`;
} else if (urn.includes("member")) {
return `${baseUrl}/in/${urnId}`;
}
} catch (error) {
return error instanceof TypeError ? "None" : "";
}
};
const getUpdateURL = (dIncluded, baseUrl) => {
try {
const urn = dIncluded.updateMetadata.urn;
return `${baseUrl}/feed/update/${urn}`;
} catch (error) {
return error instanceof TypeError ? "None" : "";
}
};
const appendUpdatePostFieldToPostsList = (
dIncluded,
lPosts,
postKey,
postValue,
) => {
const elementsCurrentIndex = lPosts.length - 1;
if (elementsCurrentIndex === -1) {
lPosts.push({ [postKey]: postValue });
} else {
if (!lPosts[elementsCurrentIndex].hasOwnProperty(postKey)) {
lPosts[elementsCurrentIndex][postKey] = postValue;
} else {
lPosts.push({ [postKey]: postValue });
}
}
return lPosts;
};
const parseListRawUrns = (lRawUrns) => {
return lRawUrns.map((urn) => getUrnFromRawUpdate(urn));
};
const parseListRawPosts = (lRawPosts, linkedinBaseUrl) => {
const lPosts = [];
lRawPosts.forEach((item) => {
const authorName = getUpdateAuthorName(item);
if (authorName) {
appendUpdatePostFieldToPostsList(item, lPosts, "author_name", authorName);
}
// ... (similar logic for other fields)
const url = getUpdateURL(item, linkedinBaseUrl);
if (url) {
appendUpdatePostFieldToPostsList(item, lPosts, "url", url);
}
});
return lPosts;
};
const getListPostsSortedWithoutPromoted = (lUrns, lPosts) => {
const lPostsSortedWithoutPromoted = [];
lPosts = lPosts.filter((d) => !d.old.includes("Promoted"));
lUrns.forEach((urn) => {
lPosts.forEach((post) => {
if (post.url.includes(urn)) {
lPostsSortedWithoutPromoted.push(post);
lPosts = lPosts.filter((d) => !d.url.includes(urn));
}
});
});
return lPostsSortedWithoutPromoted;
};
const generateTrackingIdAsCharString = () => {
const randomIntArray = Array.from({ length: 16 }, () =>
random.randomInt(256),
);
return String.fromCharCode(...randomIntArray);
};
const generateTrackingId = () => {
const randomIntArray = Array.from({ length: 16 }, () =>
random.randomInt(256),
);
const randByteArray = Buffer.from(randomIntArray);
return base64.fromByteArray(randByteArray).toString();
};
module.exports = {
getIDFromUrn,
getUrnFromRawUpdate,
getUpdateAuthorName,
getUpdateOld,
getUpdateContent,
getUpdateAuthorProfile,
getUpdateURL,
appendUpdatePostFieldToPostsList,
parseListRawUrns,
parseListRawPosts,
getListPostsSortedWithoutPromoted,
generateTrackingIdAsCharString,
generateTrackingId,
};