-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.js
102 lines (77 loc) · 2.68 KB
/
instagram.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
const puppeteer = require("puppeteer")
const BASE_URL = "https://instagram.com/"
const TAG_URL = tag => `https://www.instagram.com/explore/tags/${tag}`
const instagram = {
browser: null,
page: null,
initialize: async () => {
instagram.browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"]
})
instagram.page = await instagram.browser.newPage()
},
login: async (username, password) => {
await instagram.page.goto(BASE_URL, { waitUntil: "networkidle2" })
let loginButton = await instagram.page.$x('//a[contains(text(), "Log in")]')
// Click on the login URL button
await loginButton[0].click()
await instagram.page.waitForNavigation({ waitUntil: "networkidle2" })
await instagram.page.waitFor(1000)
// Writing the username & password
await instagram.page.type('input[name="username"]', username, {
delay: 50
})
await instagram.page.type('input[name="password"]', password, {
delay: 50
})
//Click on the login button
loginButton = await instagram.page.$x('//div[contains(text(), "Log In")]')
await loginButton[0].click()
console.log("Logged In")
await instagram.page.waitForNavigation({ waitUntil: "networkidle2" })
let NotNowButton = await instagram.page.$x(
'//button[contains(text(), "Not Now")]'
)
if (NotNowButton) {
await NotNowButton[0].click()
}
},
likeTagsProcess: async (tags = []) => {
for (let tag of tags) {
// Go to the tag page
await instagram.page.goto(TAG_URL(tag), { waitUntil: "networkidle2" })
await instagram.page.waitFor(1000)
await instagram.page.waitForSelector(
'article > div:nth-child(3) img[decoding="auto"]'
)
let posts = await instagram.page.$$(
'article > div:nth-child(3) img[decoding="auto"]'
)
for (let i = 0; i < 3; i++) {
let post = posts[i]
// Click on the post
await post.click()
// Wait for modal to appear
await instagram.page.waitFor('[style="overflow: hidden;"]')
await instagram.page.waitFor(1000)
let likeButton = await instagram.page.$(
'span[class^="glyphsSpriteHeart__outline"]'
)
if (likeButton) {
await likeButton.click()
console.log("Post Liked")
}
await instagram.page.waitFor(1000)
// Close the modal
let closeModalButton = await instagram.page.$x(
'//button[contains(text(), "Close")]'
)
await closeModalButton[0].click()
await instagram.page.waitFor(2000)
}
await instagram.page.waitFor(1000)
}
}
}
module.exports = instagram