-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstagram.js
191 lines (153 loc) · 6.72 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
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
const puppeteer = require('puppeteer');
const LOGIN_URL = 'https://www.instagram.com/accounts/login/';
let browser = null;
let page = null;
const instagram = {
// initialize: async() => {
// browser = await puppeteer.launch({
// args: [
// '--incognito'
// ],
// headless: false
// });
// page = await browser.newPage();
// await page.goto(LOGIN_URL, { waitUntil: "networkidle2" });
// await browser.close();
// },
login: async(username, password) => {
browser = await puppeteer.launch({
args: [
'--incognito'
],
headless: false
});
page = await browser.newPage();
await page.goto(LOGIN_URL, { waitUntil: "networkidle2" });
await page.type('input[name="username"]', username, { delay: 20 });
await page.type('input[name="password"]', password, { delay: 20 });
await page.click('button[type="submit"]', { delay: 20 });
await page.waitFor(5000);
const notifyBtns = await page.$x("//button[contains(text(), 'Not Now')]");
if (notifyBtns.length > 0) {
await notifyBtns[0].click();
} else {
console.log('Not notify button');
}
await page.goto(`https://www.instagram.com/${username}`, { waitUntil: "networkidle2" });
await page.waitFor(2000);
const followersBtns = await page.$('#react-root > section > main > div > header > section > ul > li:nth-child(2) > a');
await followersBtns.evaluate(btn => btn.click())
await page.waitFor(3000);
const followersDialog = 'div[role="dialog"] > div > div:nth-child(2)';
await page.waitForSelector('div[role="dialog"] > div > div:nth-child(2) > ul');
await scrollDown(followersDialog, page);
console.log("getting followers");
const lists1 = await page.$$('div[role="dialog"] > div > div:nth-child(2) > ul > div > li > div > div:nth-child(2) > div > div > div> a');
const avatarPath = [
'div[role="dialog"] > div > div:nth-child(2) > ul > div > li > div > div > div > a > img',
'div[role="dialog"] > div > div:nth-child(2) > ul > div > li > div > div > div > span > img'
];
const picc1 = await avatarPath.reduce(async(accPro, path) => {
const acc = await accPro;
const arr = await page.$$eval(path, res => {
return res.map(pic => {
const alt = pic.getAttribute('alt');
const word = alt.split(/([,])/g);
return {
username: word[0],
avatar: pic.getAttribute('src')
}
});
});
return acc.concat([...arr]);
}, Promise.resolve([]));
const followers = await Promise.all(lists1.map(async item => {
const username = await (await item.getProperty('innerText')).jsonValue();
const pic = picc1.find(p => p.username === username || { avatar: "" });
return {
avatar: await pic.avatar,
username
}
}));
const closeBtn = await page.$('div[role="dialog"] > div > div > div > div:nth-child(3) > button');
await closeBtn.evaluate(btn => btn.click());
const followingBtn = await page.$('div[id="react-root"] > section > main > div > header > section > ul > li:nth-child(3) > a');
await followingBtn.evaluate(btn => btn.click());
await page.waitFor(3000);
const followingDialog = 'div[role="dialog"] > div > div:nth-child(3)';
await page.waitForSelector('div[role="dialog"] > div > div:nth-child(3) > ul');
await scrollDown(followingDialog, page);
console.log('get following....');
const list2 = await page.$$('div[role="dialog"] > div > div:nth-child(3) > ul > div > li > div > div:nth-child(2) > div > div > div > a');
await page.waitForSelector('div[role="dialog"] > div > div:nth-child(3) > ul > div > li > div > div > div > a > img');
const avatarPath2 = [
'div[role="dialog"] > div > div:nth-child(3) > ul > div > li > div > div > div > a > img',
'div[role="dialog"] > div > div:nth-child(3) > ul > div > li > div > div > div > span > img'
];
const picc2 = await avatarPath2.reduce(async(accPro, path) => {
const acc = await accPro;
const arr = await page.$$eval(path, res => {
return res.map(pic => {
const alt = pic.getAttribute('alt');
const word = alt.split(/([,])/g);
return {
username: word[0],
avatar: pic.getAttribute('src')
}
});
});
return acc.concat([...arr]);
}, Promise.resolve([]));
const following = await Promise.all(list2.map(async item => {
const username = await (await item.getProperty('innerText')).jsonValue();
const pic = picc2.find(p => p.username === username || { avatar: "" });
return {
avatar: await pic.avatar,
username
}
}));
const followerCount = followers.length;
const followingCount = following.length;
console.log(`Follower:${followerCount}`);
console.log(`Following:${followingCount}`);
const notFollowingYou = following.filter(item => {
return !followers.find(f => f.username === item.username)
});
const notFollowingThem = followers.filter(item => {
return !following.find(f => f.username === item.username)
});
// await browser.close();
const data_total = {
followerCount,
followingCount,
//notFollowingYou,
//notFollowingThem,
followers,
following
};
debugger;
return data_total;
},
end: async() => {
await browser.close();
}
};
async function scrollDown(selector, page) {
await page.evaluate(async selector => {
const section = document.querySelector(selector);
await new Promise((resolve, reject) => {
let totalHeight = 0;
let distance = 100;
const timer = setInterval(() => {
var scrollHeight = section.scrollHeight;
section.scrollTop = 100000000;
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
});
}, 100);
}, selector);
};
module.exports = instagram;