-
Notifications
You must be signed in to change notification settings - Fork 2
/
instagram-scraper.js
132 lines (116 loc) · 4.2 KB
/
instagram-scraper.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
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
123
124
125
126
127
128
129
130
131
132
var Client = require('instagram-private-api').V1;
const getMidPicture = function(pictures, def) {
const defaultPic = { url: def, height: 150, width: 150 };
return pictures.filter(function(pic) { return pic.width != 640 })[0] || defaultPic;
}
const makeSessionId = function() {
return Math.random().toString(36).substring(2, 18);
}
const buildSessionAccount = function(sessionId, params) {
picture = getMidPicture(params.hdProfilePicVersions, params.profilePicUrl);
return {
sessionId: sessionId,
account: {
id: params.id,
mediaCount: params.mediaCount,
profilePicture: picture,
username: params.username,
fullname: params.fullName
}
};
}
const login = function(username, password, callback) {
const sessionId = makeSessionId(username);
var device = new Client.Device(sessionId);
var storage = new Client.CookieFileStorage(__dirname + '/.cookies/' + sessionId + '.json');
Client.Session.create(device, storage, username, password)
.then(function(session) {
session.getAccount().then(function(account) {
const params = account.params;
callback(buildSessionAccount(sessionId, params));
});
});
}
const getSession = function(sessionId, callback, error) {
var device = new Client.Device(sessionId);
var storage = new Client.CookieFileStorage(__dirname + '/.cookies/' + sessionId + '.json');
var session = new Client.Session(device, storage);
if (Object.keys(session._cookiesStore.storage.idx).length == 0) {
error("No active session, need to login");
} else {
callback(session);
}
}
const getCurrentSessionAccount = function(sessionId, callback, error) {
getCurrentAccount(sessionId, function(account) {
callback(buildSessionAccount(sessionId, account.params))
}, error);
}
const getCurrentAccount = function(sessionId, callback, error) {
getSession(sessionId, function(session) {
session.getAccount().then(function(account) {
callback(account);
});
}, error);
}
const getAccountMedia = function(sessionId, callback) {
getSessionAccount(sessionId, function(session, account) {
var feed = new Client.Feed.UserMedia(session, account.params.id, 100);
feed.get().then(function(media) {
const mediaList = media
.map(function(mediaItem) { return mediaItem.getParams(); })
.map(reduceMediaObject)
callback(mediaList);
});
});
}
const reduceMediaObject = function(media) {
var image = media.images.filter(function(pic) { return pic.width < 400 })[0] || media.images[0];
return {
id: media.id,
description: media.caption,
image: image,
username: media.user.username
}
}
const reduceFollowerObject = function(follower) {
return {
id: follower.id,
description: follower.fullName,
image: {
height: 150,
width: 150,
url: follower.profilePicUrl
},
username: follower.username
}
}
const like = function(sessionId, mediaId) {
getSession(sessionId, function(session) {
Client.Like.create(session, mediaId);
});
}
const getSessionAccount = function(sessionId, callback) {
getSession(sessionId, function(session) {
getCurrentAccount(sessionId, function(account) {
callback(session, account);
});
});
}
const getFollowers = function(sessionId, callback) {
getSessionAccount(sessionId, function(session, account) {
var accountFollowers = new Client.Feed.AccountFollowers(session, account.params.id, 100);
accountFollowers.get().then(function(followers) {
const followersList = followers
.map(function(follower) { return follower.getParams(); })
.map(reduceFollowerObject)
callback(followersList);
});
});
}
const follow = function(sessionId, followerId) {
getSession(sessionId, function(session) {
Client.Relationship.create(session, followerId);
});
}
module.exports = { login, getCurrentSessionAccount, getAccountMedia, like, getFollowers, follow };