-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (114 loc) · 3.19 KB
/
index.js
File metadata and controls
122 lines (114 loc) · 3.19 KB
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
120
121
122
const Promise = require('bluebird')
const _ = require('lodash')
let request = require('request-promise')
const getUserData = username => {
return request({
agent: false,
uri: `https://www.instagram.com/${username}/`,
qs: {
__a: 1
},
timeout: 10000,
json: true
})
}
const getUserPostsRecursive = (userId, after) => {
const endpoint = 'https://www.instagram.com/graphql/query/'
return request({
agent: false,
uri: endpoint,
qs: {
query_id: '17862015703145017',
id: userId,
first: 500,
after
},
timeout: 10000,
json: true
}).then(response => {
const postData = _.get(response, 'data.user.edge_owner_to_timeline_media') || {}
const edges = postData.edges || []
const posts = _.map(edges, 'node')
const hasNext = _.get(postData, 'page_info.has_next_page')
if (hasNext) {
return getUserPostsRecursive(userId, postData.page_info.end_cursor).then(nextPosts => {
return posts.concat(nextPosts)
})
}
return posts
})
}
const getUserPosts = username => {
return getUserData(username)
.then(({user}) => {
const posts = user.media.nodes
if (user.media.page_info.has_next_page)
return getUserPostsRecursive(user.id, user.media.page_info.end_cursor).then(data => {
return posts.concat(data);
})
else
return posts
})
}
const getPostCommentsRecursive = (postCode, after, commentsLength, commentsLimit) => {
const endpoint = 'https://www.instagram.com/graphql/query/'
return request({
agent: false,
uri: endpoint,
qs: {
query_id: '17852405266163336',
shortcode: postCode,
first: 1000,
after
},
timeout: 10000,
json: true
}).then(response => {
const commentData = _.get(response, 'data.shortcode_media.edge_media_to_comment') || {}
const edges = commentData.edges || []
const comments = _.map(edges, 'node')
const hasNext = _.get(commentData, 'page_info.has_next_page')
const currentCommentsLength = commentsLength + comments.length
if (hasNext && currentCommentsLength < commentsLimit) {
return getPostCommentsRecursive(
postCode,
commentData.page_info.end_cursor,
currentCommentsLength,
commentsLimit
).then(nextComments => {
return comments.concat(nextComments)
})
}
return comments
})
}
const getPostComments = (postCode, commentsLimit=500) => {
return request({
agent: false,
uri: `https://www.instagram.com/p/${postCode}/`,
qs: {
__a: 1
},
timeout: 10000,
json: true
}).then(response => {
const commentsInfo = _.get(response, 'graphql.shortcode_media.edge_media_to_comment')
const commentEdges = commentsInfo.edges || []
const comments = _.map(commentEdges, 'node')
if(_.get(commentsInfo, 'page_info.has_next_page')) {
return getPostCommentsRecursive(postCode, commentsInfo.page_info.end_cursor, 0, commentsLimit).then(nextComments => {
return comments.concat(nextComments)
})
}
return comments
})
}
const setRequest = _request_ => {
request = _request_
}
module.exports = {
getUserData,
getUserPosts,
getPostComments,
setRequest
}