Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from Tanuj69/version-2
Browse files Browse the repository at this point in the history
Version 2- More Data about the Username- BIO,Profile Picture. Improved meta data
  • Loading branch information
tanuj101 authored Aug 24, 2019
2 parents 7caefac + a08bec9 commit 6f95614
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 4,068 deletions.
166 changes: 56 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,132 +1,78 @@
# Instagram Api for Public profiles

<p align="left">
<img src="https://img.shields.io/npm/v/instagram-public-api.svg">
<img src="https://badges.frapsoft.com/os/v1/open-source.svg?v=102">
<img src="https://img.shields.io/npm/dt/instagram-public-api.svg">
<img src="https://img.shields.io/github/license/tanuj69/instagram-api.svg">
</p>
<h1> Instagram Api for Public profiles <sup>v2</sup></h1>

> A simple Instagram module to get a user's :-
- No. of followers
- No. of following
- No. of posts
- No. of likes and comments on latest 10 posts

### Try it out now (Warning: Host URL might be sleeping just wait a little after making a request)

To get Limited Data:-
![enter image description here](https://img.shields.io/npm/v/instagram-public-api.svg) ![](https://badges.frapsoft.com/os/v1/open-source.svg?v=102) ![enter image description here](https://img.shields.io/npm/dt/instagram-public-api.svg) ![](https://img.shields.io/github/license/tanuj69/instagram-api.svg)

```
https://mighty-depths-56624.herokuapp.com/api/meta/[username]
```

To get detailed Data:-
> A simple Instagram module to get a user's :-
1. Biography
2. Number of Followers
3. Number of Followings
4. Profile Picture thumbnail URL
5. Profile Picture HD URL
6. Each post object will contain
1. Dimensions of Image
2. Image's URL in HD
3. Image's thumbnail URL
4. Number of Likes
5. Number of Comments
6. URL of the POST



### Try it out now (Warning: Host URL might be sleeping just wait a little after making a request)
```
https://mighty-depths-56624.herokuapp.com/api/full/[username]
https://mighty-depths-56624.herokuapp.com/api/[username]
```
>Note- Enter username without ```[]```
## Installation

```sh
$ npm install instagram-public-api --save
```

## Usage

```js
const myApi = require("instagram-public-api");
npm install instagram-public-api
```

#### To get information of 10 latest posts of a user

## Usage
```js
myApi.getPostData("selenagomez").then(data=>{
---Do your stuff---
const Profile = require("instagram-public-api");
const user = new Profile("selenagomez");
user.getData().then(data=>{
---Do your Stuff---
})
```

#### Output

```
{username: 'selenagomez',
followers: 147800000,
following: 59,
noOfPosts: 1478,
postLinks:
[ 'https://www.instagram.com/p/BvSAdFYAEau/',
'https://www.instagram.com/p/Bu6um0bg4c2/',
'https://www.instagram.com/p/BuuOdfrgu3t/',
'https://www.instagram.com/p/BucOYdPAttg/',
'https://www.instagram.com/p/Bubv5TggD0v/',
'https://www.instagram.com/p/Bt35UNMAhm7/',
'https://www.instagram.com/p/BtxEL1EgbYa/',
'https://www.instagram.com/p/BtBuE0Hg-6j/',
'https://www.instagram.com/p/Bsqa7_GgJif/',
'https://www.instagram.com/p/BsoZalUA8HZ/',
'https://www.instagram.com/p/BoFlrM7gwnK/',
'https://www.instagram.com/p/BoBdx-Ng9JC/' ],
postData:
{ 'https://www.instagram.com/p/BvSAdFYAEau/': { likes: 5800000, comments: 52700 },
'https://www.instagram.com/p/Bu6um0bg4c2/': { likes: 5400000, comments: 43800 },
'https://www.instagram.com/p/BuuOdfrgu3t/': { likes: 3900000, comments: 34600 },
'https://www.instagram.com/p/BucOYdPAttg/': { likes: 1900000, comments: 30600 },
'https://www.instagram.com/p/Bubv5TggD0v/': { likes: 9000000, comments: 94200 },
'https://www.instagram.com/p/Bt35UNMAhm7/': { likes: 6100000, comments: 68700 },
'https://www.instagram.com/p/BtxEL1EgbYa/': { likes: 14200000, comments: 138300 },
'https://www.instagram.com/p/BtBuE0Hg-6j/': { likes: 10100000, comments: 94200 },
'https://www.instagram.com/p/Bsqa7_GgJif/': { likes: 7600000, comments: 85300 },
'https://www.instagram.com/p/BsoZalUA8HZ/': { likes: 9000000, comments: 178400 }
}
}
```

#### To get Meta data

## Output
```js
myApi.getMetaData("selenagomez").then(data => {
console.log(data);
});
```

#### Output

```
{
username: 'selenagomez',
followers: 147800000,
following: 59,
noOfPosts: 1478,
postLinks:
[ 'https://www.instagram.com/p/BvSAdFYAEau/',
'https://www.instagram.com/p/Bu6um0bg4c2/',
'https://www.instagram.com/p/BuuOdfrgu3t/',
'https://www.instagram.com/p/BucOYdPAttg/',
'https://www.instagram.com/p/Bubv5TggD0v/',
'https://www.instagram.com/p/Bt35UNMAhm7/',
'https://www.instagram.com/p/BtxEL1EgbYa/',
'https://www.instagram.com/p/BtBuE0Hg-6j/',
'https://www.instagram.com/p/Bsqa7_GgJif/',
'https://www.instagram.com/p/BsoZalUA8HZ/',
'https://www.instagram.com/p/BoFlrM7gwnK/',
'https://www.instagram.com/p/BoBdx-Ng9JC/' ],
postData: {} }
```

### If you're working with this API on a Ubuntu server

```sh
sudo apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
"username": "selenagomez",
"url": "https://www.instagram.com/selenagomez",
"bio": "Light, space, zest -that’s God. With him on my side I’m FEARLESS, afraid of no one and nothing. A21 global anti human trafficking organization A21.org",
"followers": 155698399,
"following": 61,
"totalPosts": 1506,
"posts": [
{
"dimensions": {
"height": 1080,
"width": 1080
},
"imageURL": "https://instagram.fdel1-1.fna.fbcdn.net/vp/e522acfad3fd3c4091ea88a5b8479888/5E0C70E1/t51.2885-15/e35/s1080x1080/66656269_1648697008594563_8361695703211281609_n.jpg?_nc_ht=instagram.fdel1-1.fna.fbcdn.net",
"likes": 3956372,
"comments": 24986,
"url": "https://instagram.com/p/B1WxkKfDpQy",
"imageThumbnail": "https://instagram.fdel1-1.fna.fbcdn.net/vp/c72416cc9678de364449ab8f37793a0d/5DDDA525/t51.2885-15/sh0.08/e35/s640x640/66656269_1648697008594563_8361695703211281609_n.jpg?_nc_ht=instagram.fdel1-1.fna.fbcdn.net"
}
],
"profilePicThumb": "https://instagram.fdel1-1.fna.fbcdn.net/vp/b104c292ae8e59d902586c2107a39a72/5DF2E637/t51.2885-19/s150x150/52780205_395221154575465_269834356913078272_n.jpg?_nc_ht=instagram.fdel1-1.fna.fbcdn.net",
"profilePicHD": "https://instagram.fdel1-1.fna.fbcdn.net/vp/073c225007a67fa0563350a2441f0053/5DFDDF3A/t51.2885-19/s320x320/52780205_395221154575465_269834356913078272_n.jpg?_nc_ht=instagram.fdel1-1.fna.fbcdn.net"
}
```

#### Feel free to contribute or raise issues !
See full output [here](https://pastebin.com/7GT98pNa)

#### Feel free to contribute or raising issues.

## License

GPL-V2 © [Tanuj Nagpal](www.github.com/Tanuj69)


GPL-V2 © [Tanuj Nagpal](www.github.com/Tanuj69)
19 changes: 5 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
const Profile = require("./lib/Profile.js");
const express = require("express");
const app = express();

app.get("/api/meta/:username", async (req, res) => {
app.get("/api/:username", async (req, res) => {
let NewProfile = new Profile(req.params.username);
try {
await NewProfile.getMetaData();
return res.json(NewProfile).end();
let d = await NewProfile.getData();
return res.json(d).end();
} catch (error) {
console.error(error);
return res.status(500).send(error.message);
}
});
app.get("/api/full/:username", async (req, res) => {
let NewProfile = new Profile(req.params.username);
try {
await NewProfile.getMetaData();
await NewProfile.getPostData();
return res.json(NewProfile).end();
} catch (error) {
console.error(error);
return res.status(500).send(error.message);
}
app.get("/api/", (req, res) => {
res.status(400).send("Username can't be empty");
});

app.listen(process.env.PORT || 3000, () => {
Expand Down
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
const getData = require('./lib/app.js').getData;
const getMetaData = require('./lib/app.js').getMetaData;
exports.getPostData = getData;
exports.getMetaData = getMetaData;
exports.Profile = require("./lib/Profile");
138 changes: 36 additions & 102 deletions lib/Profile.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,44 @@
const puppeteer = require("puppeteer");
const stringEvaluater = require("./evalString");

const parser = require("jsdom");
const axios = require("axios");
const { JSDOM } = parser;
class Profile {
constructor(username) {
this.username = username;
this.followers = "";
this.following = "";
this.noOfPosts = "";
this.postLinks = [];
this.postData = {};
this.url = `https://www.instagram.com/${username}`;
this.bio = "";
this.followers = 0;
this.following = 0;
this.totalPosts = 0;
this.posts = [];
this.profilePicThumb = "";
this.profilePicHD = "";
}

async getPostData() {
if (this.postLinks.length >= 1) {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", req => {
if (
req.resourceType() == "stylesheet" ||
req.resourceType() == "font" ||
req.resourceType() == "image"
) {
req.abort();
} else {
req.continue();
}
});
for (const url of this.postLinks.slice(0, 10)) {
await page.goto(url);
let data = await page.evaluate(() => {
const allMetaTags = Array.from(document.getElementsByTagName("meta"));
let postContent = allMetaTags.filter(
el => el.getAttribute("name") == "description"
)[0].content;
postContent = postContent.replace(/,/g, "");
let postSplit = postContent.split(" ");
return {
likes: postSplit[0],
comments: postSplit[2]
};
});
this.postData[url] = {
likes: stringEvaluater(data.likes),
comments: stringEvaluater(data.comments)
};
}
// console.log(this.postData);
} else {
console.log("no posts");
}
}

async getMetaData() {
try {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
//await page.setRequestInterception(true);
/* page.on("request", req => {
if (
req.resourceType() == "stylesheet" ||
req.resourceType() == "font" ||
req.resourceType() == "image"
) {
req.abort();
} else {
req.continue();
}
}); */
await page.goto("https://www.instagram.com/" + this.username, {
waitUntil: "networkidle2"
});
await page.waitForSelector(".kIKUG");
const data = await page.evaluate(async () => {
let links = Array.from(document.querySelectorAll(".kIKUG a")).map(
e => e.href
);
const allMetaTags = Array.from(document.getElementsByTagName("meta"));
let meta = allMetaTags.filter(
el => el.getAttribute("name") == "description"
)[0].content;
if (!meta) {
throw new Error("Please check your username");
}
meta = meta.replace(/,/g, "");
const contentSplit = meta.split(" ");
return {
followers: contentSplit[0],
following: contentSplit[2],
noOfPosts: contentSplit[4],
links: links
};
});
this.followers = stringEvaluater(data.followers);
this.following = stringEvaluater(data.following);
this.noOfPosts = stringEvaluater(data.noOfPosts);
this.postLinks = data.links;
await browser.close();
return data;
} catch (error) {
console.error(error);
async getData() {
const page = await axios.get(this.url);
const document = new JSDOM(page.data, { runScripts: "dangerously" });
const userInfo =
document.window._sharedData.entry_data.ProfilePage[0].graphql.user;
this.bio = userInfo.biography;
this.following = userInfo.edge_follow.count;
this.followers = userInfo.edge_followed_by.count;
this.profilePicHD = userInfo.profile_pic_url_hd;
this.profilePicThumb = userInfo.profile_pic_url;
const postData = userInfo.edge_owner_to_timeline_media;
const posts = userInfo.edge_owner_to_timeline_media.edges;
this.totalPosts = postData.count;
for (let i = 0; i < posts.length; i++) {
let post = posts[i].node;
const data = {
dimensions: post.dimensions,
imageURL: post.display_url,
likes: post.edge_liked_by.count,
comments: post.edge_media_to_comment.count,
url: `https://instagram.com/p/${post.shortcode}`,
imageThumbnail: post.thumbnail_src
};
this.posts.push(data);
}
return this;
}
}

Expand Down
14 changes: 0 additions & 14 deletions lib/app.js

This file was deleted.

Loading

0 comments on commit 6f95614

Please sign in to comment.