-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathig-elements-finder.js
executable file
·230 lines (208 loc) · 8.29 KB
/
ig-elements-finder.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
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
#!/usr/bin/env node
const IgersPostElementsFinder = require('./ig-post-elements-finder');
const template = require("./template.json");
const IgersElementsFinder = class {
constructor(browser) {
this.browser = browser;
}
async getQuerySelectors() {
// Browse new page
this.page = await this.browser.newPage();
await this.page.setExtraHTTPHeaders({
'Accept-Language': 'en-US'
});
// Execute request
const response = await this.page.goto(`https://instagram.com/${template.username}`, {
waitUntil: ['networkidle0', 'domcontentloaded']
});
this._assertPageSuccess(response);
// Expose classname sanitizer function
this.page.exposeFunction("computeClassNames", this._computeClassNames);
let profile = {
// Find username and displayName. Both are h1 headings
...await this._findUsernameAndDisplayName(),
// Find post follower and following count.
...await this._findPostFollowerAndFollowingCounts(),
// Find profile picture.
...await this._findProfilePicture(),
// Find bio and website.
...await this._findBioAndWebsite(),
// Find post URL
...await this._findPostUrls()
};
// Get query selector for post-related data with current template
let videoPosts = await new IgersPostElementsFinder(profile.videoPostUrl, this.browser).getQuerySelectors();
let imgPosts = await new IgersPostElementsFinder(profile.photoPostUrl, this.browser).getQuerySelectors();
let posts = {
img: imgPosts.img,
video: videoPosts.video,
description: imgPosts.description,
tags: imgPosts.tags,
likeCount: imgPosts.likeCount,
viewCount: videoPosts.viewCount
}
await this.page.close();
return {
profile: profile,
posts: posts
};
}
/**
* Ensure page response is alright
* @param response
* @private
*/
_assertPageSuccess(response) {
// Close if profile doesn't exist or if error
if (response.status() === 404) {
throw new Error("Profile doesn't exist");
} else if (response.status() > 200) {
throw new Error("Error: page status is " + response.status());
}
}
/**
* Format classnames for querySelector
* @param className the classes separated by one or multiple spaces (ex: container danger hidden-xs)
* @returns {string} (ex: .container.danger.hidden-xs)
* @private
*/
_computeClassNames(className) {
let cns = className.trim().split(/\s+/);
return cns.reduce((prev, curr) => prev + "." + curr, "");
}
async _findBioAndWebsite() {
let items = await this.page.$$eval("section", async (els) => {
let r = {
bio: null,
website: null
};
for (let i in els) {
if (els[i].childElementCount === 3 && els[i].children[0].tagName === "DIV" && els[i].children[1].tagName === "UL" && els[i].children[2].tagName === "DIV") {
let rootNode = els[i].children[2];
let websiteNode = rootNode.querySelector("a");
if (websiteNode) {
r.website = "div" + await window.computeClassNames(rootNode.className) + " a" + await window.computeClassNames(websiteNode.className);
}
r.bio = "div" + await window.computeClassNames(rootNode.className) + " span";
return r;
}
}
return r;
}, template);
if (!items || !items.bio) {
console.error(items);
throw new Error("Couldn't get bio");
}
return items;
}
async _findProfilePicture() {
let items = await this.page.$$eval("img", async (els, conf) => {
let r = {
imgUrl: null
};
for (let i in els) {
if (els[i].hasAttribute("alt") && (els[i].getAttribute("alt").trim() === "Add a profile photo" || els[i].getAttribute("alt").trim() === conf.username + "'s profile picture")) {
r.imgUrl = "img" + await window.computeClassNames(els[i].className);
return r;
}
}
return r;
}, template);
if (!items || !items.imgUrl) {
console.error(items);
throw new Error("Couldn't get profile picture");
}
return items;
}
async _findUsernameAndDisplayName() {
let displayNameHolder = await this.page.$$eval("h1", async (h1s, conf) => {
let r = {
displayName: null
};
for (let i in h1s) {
if (h1s[i].innerHTML === conf.displayName) {
r.displayName = "h1" + await window.computeClassNames(h1s[i].className);
}
}
return r;
}, template);
let usernameHolder = await this.page.$$eval("h2", async (h2s, conf) => {
let r = {
username: null
};
for (let i in h2s) {
if (h2s[i].innerHTML === conf.username) {
r.username = "h2" + await window.computeClassNames(h2s[i].className);
}
}
return r;
}, template);
const names = {
username: usernameHolder.username,
displayName: displayNameHolder.displayName
}
if (!names.username || !names.displayName) {
console.error(names);
throw new Error("Couldn't get username or displayName");
}
return names;
}
async _findPostFollowerAndFollowingCounts() {
let items = await this.page.$$eval("ul", async els => {
let r = {
postCount: null,
followerCount: null,
followingCount: null
};
for (let i in els) {
if (els[i].childElementCount === 3) {
let parentSelector = "ul" + await window.computeClassNames(els[i].className);
r.postCount = parentSelector + " li:first-child span";
r.followerCount = parentSelector + " li:nth-child(2) span";
r.followingCount = parentSelector + " li:nth-child(3) span";
}
}
return r;
});
if (!items || !items.postCount || !items.followerCount || !items.followingCount) {
console.error(items);
throw new Error("Couldn't get postCount or followerCount or followingCount");
}
return items;
}
async _findPostUrls() {
let postUrls = await this.page.$$eval("img", async (els, conf) => {
let postRootNode = null;
const result = {
photoPostUrl: null,
videoPostUrl: null,
postRootElement: null
}
for (let i in els) {
if (!els[i].hasAttribute("alt")) {
continue;
}
if (els[i].getAttribute("alt").trim() === conf.videoPostAlt) {
// We need to go up the tree 4 times to get the root node for a post
postRootNode = els[i].parentElement.parentElement.parentElement.parentElement;
result.videoPostUrl = els[i].parentElement.parentElement.parentElement.getAttribute("href");
} else if (els[i].getAttribute("alt").trim() === conf.photoPostAlt) {
postRootNode = els[i].parentElement.parentElement.parentElement.parentElement;
result.photoPostUrl = els[i].parentElement.parentElement.parentElement.getAttribute("href");
}
}
if (!postRootNode || !result.videoPostUrl || !result.photoPostUrl) {
return result;
}
result.photoPostUrl = `https://instagram.com${result.photoPostUrl}`;
result.videoPostUrl = `https://instagram.com${result.videoPostUrl}`;
result.postRootElement = "div" + await window.computeClassNames(postRootNode.className) + " a";
return result;
}, template);
if (!postUrls) {
throw new Error("Couldn't get url of photo or video post");
}
return postUrls;
}
};
module.exports = IgersElementsFinder;