-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (66 loc) · 2.3 KB
/
index.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
const puppeteer = require('puppeteer');
// The username of the linkeding account
const USERNAME = process.env.LINKEDIN_USERNAME;
if(!USERNAME) {
console.error("Missing username")
process.exit(1);
}
// The password of the linkedin account
const PASSWORD = process.env.LINKEDIN_PASSWORD;
if(!PASSWORD){
console.error("Missing PASSWORDs")
process.exit(1);
}
// The url of the posts to like
const ALL_POST_URL = process.env.ALL_POST_URL;
if(!ALL_POST_URL){
console.error("Missing ALL_POST_URL")
process.exit(1);
}
// The company name to like
const COMPANY_NAME = process.env.COMPANY_NAME;
if(!COMPANY_NAME){
console.error("Missing COMPANY_NAME")
process.exit(1);
}
// The interval of time where the function will be executed (like one time each TIME_INTERVAL)
const TIME_INTERVAL = Number.parseInt(process.env.TIME_INTERVAL) || 900;
/**
* Login into linkedin
* Goes in the posts page
* Like the first posts of the page
* Close the windows
*/
const autoLiker = async () => {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.setViewport({
width: 1080,
height: 720,
});
await page.goto('https://www.linkedin.com/checkpoint/rm/sign-in-another-account?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
await page.waitForNetworkIdle();
try {
await page.$eval('input[id=username]', (element, username) => element.value=username, USERNAME);
} catch(error) {
await page.$eval('input[id=email-or-phone]', (element, username) => element.value=username, USERNAME);
}
await page.$eval('input[id=password]', (element, password) => element.value=password, PASSWORD);
await page.click('button[type="submit"]');
await page.waitForNetworkIdle();
await page.goto(ALL_POST_URL, {waitUntil: "networkidle2"})
try {
await page.click(`button[aria-label="Aimer le post de ${COMPANY_NAME}"]`);
console.log("New post liked")
} catch(err) {
console.log("No new post")
}
await browser.close();
}
// Auto like every 15 minutes
console.log("Auto liker started");
// Execute the function a first time
autoLiker();
// Execute the function in the given interval
// TODO: improve, do not use interval but a "chain" of setTimeout
setInterval(autoLiker, TIME_INTERVAL * 1000);