-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
79 lines (69 loc) · 2.29 KB
/
index.js
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
const dotenv = require('dotenv').load();
const request = require('request-promise')
const { parseProfile, parsePosts } = require('./utils/parser')
const { bulkUpdate, insertOne } = require('./database/insert')
const { insertObjects } = require('./algolia/insert')
const USERNAME = 'moonbao.np'
const URL_PROFILE = `https://www.instagram.com/${USERNAME}/?__a=1`
const URL_POSTS = 'https://www.instagram.com/graphql/query';
const rateLimit = 100
+async function(profileUrl, postsUrl) {
try {
const resProfile = await http({
uri: URL_PROFILE,
json: true
})
const Profile = await insertOne(parseProfile(resProfile))
let hasNext = false
const query = {
query_id: '17888483320059182',
variables: JSON.stringify({
id: Profile.id,
first: 100
})
}
let postWithObjectIDs = []
// let diffDays = 0
do {
console.log('Next Query', query)
const resPosts = await http({
uri: URL_POSTS,
qs: query,
json: true
})
resPosts['name'] = Profile.name
resPosts['username'] = Profile.username
resPosts['profile_display'] = Profile.display
const Posts = parsePosts(resPosts)
postWithObjectIDs = postWithObjectIDs.concat(await bulkUpdate(Posts))
const pageInfo = resPosts['data']['user']['edge_owner_to_timeline_media']['page_info']
hasNext = pageInfo['has_next_page']
const nextQuery = Function(`return ${query.variables}`)()
nextQuery.after = pageInfo['end_cursor']
query.variables = JSON.stringify(nextQuery)
// const timeDiff = Math.abs(Date.now() - Posts[0]['timestamp'] * 1000);
// diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
// console.log('Difference in days:: ', diffDays)
} while (hasNext && postWithObjectIDs.length <= rateLimit)
const alogliaResult = await insertObjects(postWithObjectIDs)
} catch (e) {
console.log("Error::", e)
}
}(URL_PROFILE, URL_POSTS);
async function http (options = {}) {
const defaults = {
method: 'GET',
uri: 'http://www.google.com',
headers: {
'Connection': 'close'
},
resolveWithFullResponse: true
}
const opts = Object.assign({}, defaults, options)
try {
const res = await request(opts)
return res.body
} catch (e) {
console.log("Error::", e)
}
}