-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
151 lines (122 loc) · 5.2 KB
/
main.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
const puppeteer = require('puppeteer');
const inquirer = require('inquirer');
const fs = require('fs');
(async () => {
if (!fs.existsSync('screenshots')) {
fs.mkdirSync('screenshots');
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
async function login() {
await page.goto('https://www.linkedin.com');
const email = (await inquirer.prompt({
type: 'input',
name: 'value',
message: 'What\'s your LinkedIn email address?'
})).value;
const password = (await inquirer.prompt({
type: 'password',
name: 'value',
message: 'And what\'s your password?',
mask: '*'
})).value;
console.log('Attempting to log in...');
await page.type('#login-email', email, {delay: 100});
await page.type('#login-password', password, {delay: 100});
await page.click('#login-submit');
await page.waitForNavigation();
await page.goto('https://www.linkedin.com/mynetwork/invite-connect/connections/');
}
async function harvestConnections() {
let connections = [];
console.log('Searching for connections...');
while (true) {
const allConnectionEls = await page.$$('[data-control-name="connection_profile"]');
let newConnections = [];
let newConnectionEls = [];
for (let connectionEl of allConnectionEls) {
const connection = await page.evaluate(e => ({
name: e.textContent.trim(),
url: e.getAttribute('href')
}), connectionEl);
if (
newConnections.every(c => c.url !== connection.url) &&
connections.every(c => c.url !== connection.url)
) {
newConnections.push(connection);
newConnectionEls.push(connectionEl);
}
}
if (newConnections.length > 0) {
connections = [...connections, ...newConnections];
console.log(`Found ${connections.length} connections so far...`);
} else {
break;
}
for (let resultEl of newConnectionEls) {
await resultEl.hover();
await page.waitFor(200);
}
}
console.log(`Looks like that's everyone! Found ${connections.length} connections overall.`);
return connections;
}
async function promptConnectionsForUnfollowDecisions(connections) {
const connectionsToUnfollow = [];
for (let connection of connections) {
const shouldUnfollow = (await inquirer.prompt({
type: 'confirm',
message: `${connections.indexOf(connection) + 1}/${connections.length}: ` +
`Unfollow ${connection.name} [${connection.url}]?`,
name: 'value',
default: false
})).value;
if (shouldUnfollow) {
connectionsToUnfollow.push(connection);
}
}
return connectionsToUnfollow;
}
async function unfollowConnection(connection, number, total) {
try {
console.log(`(${number}/${total}) Attempting to unfollow ${connection.name} [${connection.url}]...`);
await page.goto(`https://linkedin.com${connection.url}`);
await page.click('.pv-s-profile-actions__overflow-toggle');
await page.waitFor(200);
const unfollowButton = await page.$('.pv-s-profile-actions--unfollow');
if (!unfollowButton) {
console.log(`> You have already unfollowed ${connection.name}.`);
} else {
await unfollowButton.click();
await page.waitFor(200);
console.log(`> Successfully unfollowed ${connection.name}.`);
}
} catch (e) {
console.log(
`> Failed to unfollow ${connection.name}. A screenshot has been saved for debugging.`
);
await page.screenshot({path: `screenshots/ERROR_${encodeURIComponent(connection.url)}_${Date.now()}.png`});
}
}
async function unfollowConnections(connections) {
console.log('Applying your changes...');
for (let connection of connections) {
await unfollowConnection(connection, connections.indexOf(connection) + 1, connections.length);
}
console.log('All done!');
}
async function main() {
try {
await login();
const connections = await harvestConnections();
const unfollows = await promptConnectionsForUnfollowDecisions(connections);
await unfollowConnections(unfollows);
} catch (e) {
console.error(e);
console.log(`An error occurred. A screenshot has been saved for debugging.`);
await page.screenshot({path: `screenshots/ERROR_${Date.now()}.png`});
}
}
await main();
await browser.close();
})();