Skip to content

Instantly share code, notes, and snippets.

@cursorial
Created April 17, 2021 19:29
Show Gist options
  • Save cursorial/9afdaa8abf3725d36344ef21b1780316 to your computer and use it in GitHub Desktop.
Save cursorial/9afdaa8abf3725d36344ef21b1780316 to your computer and use it in GitHub Desktop.
Get Instagram Post Likes
const write = (arr) => { /* utility function to write our array of user id's somewhere */ }
const getGraphqlUrl = (queryHash, variables) => {
return `https://www.instagram.com/graphql/query/?query_hash=${queryHash}&variables=${encodeURIComponent(JSON.stringify(variables))}`
}
const getPageRequest = ({
err,
res,
body,
account,
shortcode,
endCursor,
queryHash,
graphqlVariables
}) => {
const errors = handlePageRequestErrors(err, body)
if (errors) return null
const {
edges,
page_info,
count
} = body.data.shortcode_media.edge_liked_by
const { end_cursor } = page_info
let unfollowed = []
edges.forEach((user) => {
const { id } = user.node
if (unfollowed.indexOf(id) < 0) unfollowed.push(id)
})
write(unfollowed)
if (end_cursor) {
getPage({
res,
queryHash,
shortcode,
graphqlVariables,
endCursor: end_cursor,
})
} else {
return true
}
}
const getPage = ({
queryHash,
shortcode,
endCursor
) => {
setTimeout(() => {
const graphqlVariables = {
shortcode,
include_reel: true,
first: 12,
...(!!endCursor && { after: endCursor })
}
request(
getGraphqlUrl(queryHash, graphqlVariables),
{ json: true },
(err, response, body) => {
getPageRequest({
err,
res,
body,
...params
})
}
)
}, 2000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment