-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.js
144 lines (117 loc) · 3.62 KB
/
crawler.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
#!/usr/bin/env node
const { promisify } = require("util");
const fs = require("fs");
const puppeteer = require("puppeteer");
const dom = require("./dom.json");
const writeFileAsync = promisify(fs.writeFile);
const CrawlerInstagram = class {
constructor() {}
async start(userName, postLimit) {
this.browser = await puppeteer.launch({
args: [
"--lang=en-US",
"--disk-cache-size=0",
"--no-sandbox",
"--disable-setuid-sandbox"
]
});
this.page = await this.browser.newPage();
await this.page.setExtraHTTPHeaders({
"Accept-Language": "en-US"
});
await this.page.goto(`https://instagram.com/${userName}`, {
waitUntil: "networkidle0"
});
if (await this.page.$(dom.notExist)) {
process.exit();
}
try {
this.dataProfile = {
urlProfile: this.page.url(),
...(await this.getInfoProfile()),
posts: await this.getDataPostProfile(postLimit)
};
await this.writeProfile(userName);
} catch (error) {
console.error(error);
process.exit();
}
await this.browser.close();
return this.dataProfile;
}
// Get info in profil
async getInfoProfile() {
return this.page.evaluate(dom => {
return {
numberPosts: document.querySelector(dom.numberPosts)
? document.querySelector(dom.numberPosts).innerText
: null
};
}, dom);
}
// Get data for each post
async getDataPostProfile(postLimit) {
let numberPosts = await this.page.evaluate(dom => {
return document.querySelector(dom.numberPosts).innerText;
}, dom);
const convertedNumber = parseInt(numberPosts.split(",").join(""));
if (postLimit > convertedNumber) {
postLimit = convertedNumber;
}
const listPostUrl = new Set();
while (listPostUrl.size < Number(postLimit)) {
const listUrl = await this.page.$$eval(dom.listPost, list =>
list.map(n => n.getAttribute("href"))
);
listUrl.forEach(url => {
if (!postLimit || (postLimit && listPostUrl.size < postLimit)) {
listPostUrl.add(url);
}
});
await this.page.evaluate(() => {
window.scrollTo(0, 1000000);
});
}
await this.page.close();
const listPost = [];
for (const url of listPostUrl) {
console.log("Url => " + url);
const page = await this.browser.newPage();
await page.goto(`https://instagram.com${url}`, {
waitUntil: "networkidle0"
});
const data = await page.evaluate(dom => {
return {
urlImage: document.querySelector(dom.urlImage)
? document.querySelector(dom.urlImage).getAttribute("src")
: null,
video: document.querySelector(dom.video)
? document.querySelector(dom.video).getAttribute("src")
: null,
description: document.querySelector(dom.description)
? document.querySelector(dom.description).textContent
: null,
likeNumber: document.querySelector(dom.likeNumber)
? document.querySelector(dom.likeNumber).textContent
: null,
videoViewsNumber: document.querySelector(dom.viewNumber)
? document.querySelector(dom.viewNumber).textContent
: null
};
}, dom);
await page.close();
listPost.push(data);
}
return listPost;
}
async writeProfile(userName) {
let write = writeFileAsync(
`${userName}.json`,
JSON.stringify(this.dataProfile, null, 2)
);
return write.then(x => {
console.log("File created.");
});
}
};
module.exports = CrawlerInstagram;