Skip to content

Commit

Permalink
Refactor PR#123
Browse files Browse the repository at this point in the history
  • Loading branch information
revall committed May 20, 2020
1 parent 9c76790 commit 39beffa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
48 changes: 24 additions & 24 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 @@ -395,43 +395,43 @@ class Instagram {
data => data.graphql.shortcode_media
)
}
//get comments in media with paginator
//!warning, the pointer can be array or not, if array, you need to convert this!
//to convert pick pointer, just do something like this: let pointer = JSON.parse(pointer); pointer = JSON.stringify(pointer)
async getMediaComments({shortcode, first = 12, after = ''}){

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

async getMediaLikes({ shortcode, first = 12, after = '' }) {
return this.request('/graphql/query/', {
qs: {
query_hash: 'd5d763b1e2acf209d62d22d184488e57',
variables: JSON.stringify({
shortcode: shortcode,
first: first,
after: after
shortcode,
first,
after
})
}
})
.then(data => data.data.shortcode_media.edge_liked_by)
.then(({count, page_info, edges }) => ({
count, page_info, data: edges.map(edge => edge.node)
}))


.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 39beffa

Please sign in to comment.