-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsta.js
119 lines (106 loc) · 2.88 KB
/
insta.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
const puppeteer = require("puppeteer");
const express = require("express");
var cors = require('cors')
const app = express();
app.use(cors())
async function getDataFromInsta(username){
url = `https://www.instagram.com/${username}`;
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
page.setViewport({ width: 1280, height: 1280 });
page.setDefaultNavigationTimeout(0);
await page.goto(url, { waitUntil: "networkidle2" });
//username availability check
var response;
try {
response = await page.$eval("h2", (el) => el.innerText);
} catch {
//h2 tag not found
}
//username exsist
if (response == username) {
//metedata array ['noOfPosts','followers','following']
var metadata = await page.$$eval(".g47SY ", (meta) => {
return meta.map((meta) => meta.innerHTML);
});
const noOfPosts = metadata[0];
const followers = metadata[1];
const following = metadata[2];
var bio,
website,
profilePic,
isPrivate = false,
verified;
try {
bio = await page.$eval(".-vDIg span", (el) => el.innerText);
website = await page.$eval(".-vDIg a", (el) => el.innerText);
profilePic = await page.$eval("img ", (el) => el.src);
} catch (e) {
//error handling
bio = "";
website = "";
profilePic = "";
}
//verified badge check
try {
await page.$eval(".coreSpriteVerifiedBadge");
verified = true;
} catch (error) {
verified = false;
}
//privacy check
try {
await page.waitForSelector("._4Kbb_._54f4m");
isPrivate = true;
} catch (error) {
isprivate = false;
}
//all posts extracting
var postsarr = [];
if (noOfPosts != 0) {
if (!isPrivate) {
postsarr = await page.$$eval(".KL4Bh img", (post) => {
return post.map((post) => post.src);
});
}
}
// saving into JSON Data
const data = {
username: username,
profilePic: profilePic,
existance: true,
totalPosts: noOfPosts,
followers: followers,
following: following,
verified: verified,
bio: bio,
website: website,
isPrivate: isPrivate,
recentPosts: postsarr,
};
await browser.close();
return (JSON.stringify(data, null, 3));
} else {
const data = {
username: username,
existance: false,
};
await browser.close();
return (JSON.stringify(data, null, 3));
}
}
const port = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.send(
JSON.stringify({ message: "server is Running!", status: true }, null, 3)
);
});
app.get("/:un", async (req, res) => {
username = req.params.un;
console.log("Request for username = "+username);
const data = await getDataFromInsta(username);
res.send(data);
});
app.listen(port, () => {
console.log("Server Listening on port " + port);
});