-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport_posts.ts
257 lines (225 loc) · 7.76 KB
/
export_posts.ts
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import puppeteer, { Page } from "puppeteer";
import readline from "readline";
import csvWriter from "csv-write-stream";
import fs from "fs";
interface PostData {
content: string;
likes: number;
comments: number;
republications: number;
imageUrl: string;
publicationDate: string;
}
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page: Page = await browser.newPage();
await page.goto("https://www.linkedin.com/uas/login", {
waitUntil: "networkidle2",
});
// Log in to LinkedIn
await page.type("#username", "YOUR_EMAIL");
await page.type("#password", "YOUR_PASSWORD");
await page.click('button[type="submit"]');
await page.waitForNavigation();
// Check if the navigation took us to the LinkedIn feed
const currentUrl = page.url();
if (currentUrl !== "https://www.linkedin.com/feed/") {
// Wait for manual verification code entry
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
await new Promise<void>((resolve) => {
rl.question(
"Please enter the verification code manually and press Enter to continue...",
() => {
rl.close();
resolve();
}
);
});
}
// Navigate to the specific LinkedIn page
await page.goto(
"https://www.linkedin.com/in/benoitdubos/recent-activity/all/",
{ waitUntil: "domcontentloaded" }
);
// await page.goto('https://www.linkedin.com/in/kevindufraisse/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// await page.goto('https://www.linkedin.com/in/oussamaammar/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// await page.goto('https://www.linkedin.com/in/gregoiregambatto/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// await page.goto('https://www.linkedin.com/in/marclouvion/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// await page.goto('https://www.linkedin.com/in/thibault-louis/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// await page.goto('https://www.linkedin.com/in/eric-djavid-2154b991/recent-activity/all/', { waitUntil: 'domcontentloaded' });
// Wait for 1 seconds to ensure all content is loaded
await page.evaluate(
() => new Promise((resolve) => setTimeout(resolve, 3000))
);
let postData: PostData[] = [];
// Enable request interception
await page.setRequestInterception(true);
// Listen for requests and block navigation
page.on("request", (request: any) => {
if (request.isNavigationRequest() && request.frame() === page.mainFrame()) {
request.abort();
} else {
request.continue();
}
});
let loadMoreVisible = true;
// let loadMoreCount = 0;
while (loadMoreVisible) {
// while (loadMoreCount < 10) {
try {
// Wait for the button to be visible and click it
await page.waitForSelector(".scaffold-finite-scroll__load-button", {
visible: true,
timeout: 10000,
});
await page.evaluate(() => {
const button = document.querySelector(
".scaffold-finite-scroll__load-button"
);
if (button) {
(button as HTMLElement).click();
}
});
await page.evaluate(
() => new Promise((resolve) => setTimeout(resolve, 3000))
);
// loadMoreCount++;
} catch (e) {
console.log("No more 'Load More' button found or an error occurred:", e);
loadMoreVisible = false;
// scroll to top of the page
await page.evaluate(
() => new Promise((resolve) => setTimeout(resolve, 3000))
);
await page.evaluate(() => window.scrollTo(0, 0));
}
}
const postsExist = await page.evaluate(() => {
return (
document.querySelectorAll(
"li.profile-creator-shared-feed-update__container"
).length > 0
);
});
if (!postsExist) {
console.log("No posts found. Check the CSS selector.");
const currentUrl = page.url();
console.log("Current URL:", currentUrl);
// break;
}
// Extract posts using page.evaluate
const posts = await page.evaluate(() => {
const postElements = document.querySelectorAll(
"li.profile-creator-shared-feed-update__container"
);
return Array.from(postElements)
.map((post) => {
const postElement = post as HTMLElement;
// Check if the post is a republication
const headerText =
postElement.querySelector(".update-components-header__text-view")
?.textContent || "";
if (headerText.includes("a republié ceci")) {
return null; // Skip this post
}
const content =
(postElement.querySelector("span.break-words") as HTMLElement)
?.innerText || "";
// Parse likes, handling commas and non-breaking spaces
const likesText =
postElement
.querySelector(".social-details-social-counts__reactions-count")
?.textContent?.trim() ?? "0";
const likes = parseInt(likesText.replace(/[\s,]/g, ""));
// Extract comments amount
const commentsText =
(
postElement.querySelector(
'.social-details-social-counts__comments span[aria-hidden="true"]'
) as HTMLElement
)?.innerText ?? "0";
const comments = parseInt(commentsText.replace(/\D/g, ""));
// Extract republications amount
const republicationsText =
(
postElement.querySelector(
'.social-details-social-counts__item--truncate-text span[aria-hidden="true"]'
) as HTMLElement
)?.innerText ?? "0";
const republications = parseInt(republicationsText.replace(/\D/g, ""));
// Extract image URL
const imageUrl =
postElement
.querySelector(".update-components-image__image")
?.getAttribute("src") ?? "";
// Extract publication date
const publicationDate =
(
postElement.querySelector(
'.update-components-actor__sub-descriptin span[aria-hidden="true"]'
) as HTMLElement
)?.innerText ?? "";
return {
content,
likes,
comments,
republications,
imageUrl,
publicationDate,
};
})
.filter((post) => post !== null); // Filter out null values
});
postData.push(...posts);
// After extracting posts
console.log("Extracted posts:", postData);
// Filter out posts with empty content or zero likes
const filteredPosts = postData
.filter((post) => post.content.trim() !== "" && post.likes > 0)
.map((post) => ({
content: post.content,
likes: post.likes,
comments: post.comments,
republications: post.republications,
imageUrl: post.imageUrl,
publicationDate: post.publicationDate,
}));
console.log("Filtered posts:", filteredPosts);
// Write filtered posts to a CSV file
if (filteredPosts.length) {
// Check if the file exists and delete it to ensure it's replaced
const filePath = "linkedin_posts.csv";
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
const writer = csvWriter({
headers: [
"Post Content",
"Likes",
"Comments",
"Republications",
"Image URL",
"Publication Date",
],
});
writer.pipe(fs.createWriteStream(filePath));
filteredPosts.forEach((data) => {
writer.write([
data.content,
data.likes.toString(),
data.comments.toString(),
data.republications.toString(),
data.imageUrl,
data.publicationDate,
]);
});
writer.end();
console.log(`Total number of exported posts: ${filteredPosts.length}`);
} else {
console.log("No valid post data to write to CSV.");
}
await browser.close();
})();