Skip to content

Commit

Permalink
Merge pull request #126 from jlobos/PR_123
Browse files Browse the repository at this point in the history
Add getMediaLikes and getMediaComments endpoints
  • Loading branch information
revall authored May 20, 2020
2 parents 200a442 + 39beffa commit 6bb9d9c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const client = new Instagram({ username, password, cookieStore })
* [.getPhotosByUsername({username, first, after})](#getphotosbyusernameparams)
* [.getPrivateProfilesFollowRequests(cursor)](#getPrivateProfilesFollowRequests)
* [.getChainsData({ userId })](#getChainsData)
* [.getMediaLikes({ shortcode, first, after })](#getMediaLikesParams)
* [.getMediaComments({ shortcode, first, after })](#getMediaCommentsParams)

### Instagram(credentials, opts)
```js
Expand Down Expand Up @@ -470,6 +472,42 @@ await client.getPhotosByHashtag({ hashtag: 'unicorn' })
> This will return the similar accounts, that you see, when you click on the ARROW in a profile.
- `params`
- `userId`: The user id

### getMediaLikes(params)
```js
await client.getMediaLikes({ shortcode: 'B-0000000', first: '49', after: '' })
```
> This will return the media likes.
- `params`
- `shortcode`: The shortcode media like this: https://www.instagram.com/p/B-00000000/, only put shortcode like this : B-000000000
- `first`: A `number` of records to return max is `49`
- `after`: The query cursor `String` for pagination

### getMediaComments(params)
```js
await client.getMediaComments({ shortcode: 'B-0000000', first: '12', after: '' }).catch((error) => {
console.log(error);
})
.then((response) => {
console.log(response);
});

//The query cursor 'after' maybe return an array, if array you need to convert like this:
let pointer = response.page_info.end_cursor;
// this will try to convert array to json stringify
try{
pointer = JSON.parse(pointer);
pointer = JSON.stringify(pointer);
}catch(e){
console.log('Pointer is not array!, don't need to be converted!');
}
```
> This will return the media comments.
- `params`
- `shortcode`: The shortcode media like this: https://www.instagram.com/p/B-00000000/, only put shortcode like this : B-000000000
- `first`: A `number` of records to return max is `49`
- `after`: The query cursor `String` for pagination
## License
Expand Down
42 changes: 39 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ class Instagram {
include_reel: false,
include_suggested_users: false,
include_logged_out_extras: false,
include_highlight_reels:false
include_highlight_reels: false
})
}
})
.then(data => data.data.user.edge_chaining)
.then(({ edges }) => edges.map(edge => edge.node));
.then(({ edges }) => edges.map(edge => edge.node))
}

async getActivity() {
return this.request('/accounts/activity/?__a=1').then(
data => data.graphql.user
Expand Down Expand Up @@ -396,6 +396,42 @@ class Instagram {
)
}

async getMediaComments({ shortcode, first = 12, after = '' }) {
return this.request('/graphql/query/', {
qs: {
query_hash: 'bc3296d1ce80a24b1b6e40b1e72903f5',
variables: JSON.stringify({ shortcode, first, after })
}
})
.then(response => response.data.shortcode_media || {})
.then(media => media.edge_media_to_parent_comment || {})
.then(({ count = 0, page_info = {}, edges = [] }) => ({
count,
page_info,
edges
}))
}

async getMediaLikes({ shortcode, first = 12, after = '' }) {
return this.request('/graphql/query/', {
qs: {
query_hash: 'd5d763b1e2acf209d62d22d184488e57',
variables: JSON.stringify({
shortcode,
first,
after
})
}
})
.then(response => response.data.shortcode_media || {})
.then(media => media.edge_liked_by || {})
.then(({ count = 0, page_info = {}, edges = [] }) => ({
count,
page_info,
edges
}))
}

async addComment({ mediaId, text, replyToCommentId }) {
return this.request.post(`/web/comments/${mediaId}/add/`, {
form: { comment_text: text, replied_to_comment_id: replyToCommentId }
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ test('getChainsData', async t => {
const response = await client.getChainsData({ userId: users.Maluma.id })
t.true(Array.isArray(response))
})

test('getMediaComments', async t => {
const response = await client.getMediaComments({
shortcode: 'BWl6P',
first: 12
})
t.true(Number.isInteger(response.count))
t.true(Array.isArray(response.edges))
t.true(typeof response.page_info === 'object')
})

test('getMediaLikes', async t => {
const response = await client.getMediaLikes({ shortcode: 'BWl6P', first: 12 })
t.true(Number.isInteger(response.count))
t.true(Array.isArray(response.edges))
t.true(typeof response.page_info === 'object')
})

0 comments on commit 6bb9d9c

Please sign in to comment.