-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
485 lines (411 loc) · 17.8 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
const actions = require("./scripts/actions.js");
const data = require("./scripts/data.js");
const misc = require("./scripts/misc.js");
const navigation = require("./scripts/navigation.js");
const popup = require("./scripts/popup.js");
const channel = require("./scripts/channel.js");
const observer = require("./scripts/observer.js");
const puppeteer = require("puppeteer");
const fs = require("fs");
const { IBError, IBLoginError } = require("./error.js");
const { errorMessage } = require("./message.js");
const { SearchResult, User, UserDetails, DirectMessage } = require("./types.js");
const { Cache } = require("./cache.js");
class Action {
/**
* @constructor
* @param {Function} func
*/
constructor(func) {
this.func = func;
}
/**
* @returns {Promise<any>}
*/
async run() {
return (await this.func());
}
}
class Queue {
/**
* @constructor
*/
constructor() {
this.list = [];
this.shouldRun = true;
// start running Queue
this.run();
}
async run() {
if(!this.shouldRun) return;
if(this.list.length > 0) {
await this.list[0]();
this.list.splice(0, 1);
}
this.timeout = setTimeout(() => this.run(), 1000);
}
stop() {
this.shouldRun = false;
}
/**
*
* @param {Action} action
* @returns {Promise<any>}
*/
push(action) {
return new Promise((resolve, reject) => {
this.list.push(async () => {
try {
const result = await action.run();
resolve(result);
} catch(e) {
reject(e);
}
});
});
}
}
class InstagramBot {
/**
*
* @param {puppeteer.Browser} browser
* @param {puppeteer.Page} page
* @param {Boolean} [ authenticated = false ]
* @property {puppeteer.Browser} browser
* @property {puppeteer.Page} page
* @property {Queue} queue
* @property {Boolean} authenticated
* @property {String} username
* @property {Cache} cache
*/
constructor(browser, page, authenticated = false) {
this.browser = browser;
this.page = page;
this.queue = new Queue();
this.authenticated = authenticated;
this.username = null;
this.cache = Cache.empty();
}
/**
*
* @param {Boolean} [ headless = false ]
* @param {Object} [ session = {} ]
* @returns {Promise<InstagramBot>}
*/
static async launch(headless = false, session = {}) {
const args = ["--no-sandbox", "--disable-setuid-sandbox"];
const browser = await misc.launchBrowser({ headless, args });
const cookies = session.cookies ? session.cookies : [];
const page = await misc.newPage(browser, "en", cookies);
// check if page is already authenticated
const isAuthenticated = await data.isAuthenticated(page);
// create bot
const bot = await new InstagramBot(browser, page, isAuthenticated);
// add observer to bot
await bot.addObserver(async () => {
// will execute everytime the page changes
await popup.dismissCookiePopup(page);
await popup.dismissNotificationPopup(page);
});
return bot;
}
/**
*
* @param {String} filePath
* @returns {Promise<Object>} session, which probably stores your credentials
*/
static async loadSession(filePath) {
try {
const raw = await fs.promises.readFile(filePath);
const session = JSON.parse(raw);
return session;
} catch(e) {
throw new IBError(errorMessage.failedToLoadSession.code, errorMessage.failedToLoadSession.message, e);
}
}
/**
* closes the browser
* @returns {Promise<void>}
*/
async close() {
await this.page.close();
await this.browser.close();
await this.browser.disconnect();
await this.queue.stop();
}
/**
* stops the bot
* @returns {Promise<void>}
*/
async stop() {
await this.page.close();
await this.browser.close();
await this.browser.disconnect();
await this.queue.stop();
}
/**
*
* @param {Function} func
* @returns {Promise}
*/
async addObserver(func) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
await observer.addObserver(this.page, func);
}
/**
* @returns {Promise<Object>}
*/
async getCookies() {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
const action = new Action(() => data.getCookies(this.page));
const cookies = await this.queue.push(action);
return cookies;
}
/**
*
* @returns {Promise<Object>} session, which normally stores credentials
*/
async getSession() {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
const cookies = await this.getCookies();
return { cookies };
}
/**
*
* @param {String} filePath
* @returns {Promise}
*/
async saveSession(filePath) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
const session = await this.getSession();
const text = JSON.stringify(session);
await fs.promises.writeFile(filePath, text);
}
/**
*
* @param {String} username
* @param {String} password
* @returns {Promise<any>}
*/
async login(username, password) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(this.authenticated) throw new IBLoginError(errorMessage.botAlreadyAuthenticated.code, errorMessage.botAlreadyAuthenticated.message);
const action = new Action(() => actions.login(this.page, username, password));
await this.queue.push(action);
this.username = username;
this.authenticated = true;
}
/**
*
* @returns {Promise<any>}
*/
async logout() {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) return;
const action = new Action(() => actions.logout(this.page, this.username));
await this.queue.push(action);
this.username = null;
this.authenticated = false;
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @param {Number} [ minLength = 50 ]
* @returns {Promise<User[]>}
*/
async getFollowing(identifier, minLength = 50) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getFollowing(this.page, identifier, minLength));
const following = await this.queue.push(action);
// store data in cache, if types are compatible
if(identifier instanceof User) {
this.cache = this.cache.addFollowingList(identifier, following);
}
return following;
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @param {Number} [ minLength = 50 ]
* @returns {Promise<User[]>}
*/
async getFollower(identifier, minLength = 50) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getFollower(this.page, identifier, minLength));
const follower = await this.queue.push(action);
// store data in cache, if types are compatible
if(identifier instanceof User) {
this.cache = this.cache.addFollowerList(identifier, follower);
}
return follower;
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @returns {Promise<any>}
*/
async follow(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => actions.follow(this.page, identifier));
await this.queue.push(action);
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @returns {Promise<any>}
*/
async unfollow(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => actions.unfollow(this.page, identifier));
await this.queue.push(action);
}
/**
*
* @param {String | User | SearchResult} userIdentifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @returns {Promise<Boolean>} whether you are following the specified user or not
*/
async isFollowing(userIdentifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.isFollowing(this.page, userIdentifier));
const result = await this.queue.push(action);
return result;
}
/**
*
* @param {String} searchTerm
* @returns {Promise<SearchResult[]>}
*/
async search(searchTerm) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => navigation.search(this.page, searchTerm));
const searchResults = await this.queue.push(action);
return searchResults;
}
/**
*
* @param {String | SearchResult | User | Post} identifier can either be a link, username, SearchResult, User or Post
* @returns {Promise<any>}
*/
async goto(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => navigation.goto(this.page, identifier));
await this.queue.push(action);
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @returns {Promise<UserDetails>}
*/
async getUserDetails(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getUserDetails(this.page, identifier));
const userDetails = await this.queue.push(action);
return userDetails;
}
/**
*
* @param {String | User | SearchResult} identifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @param {Number} [ minLength = 50 ]
* @returns {Promise<Post[]>}
*/
async getPosts(identifier, minLength = 50) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getPosts(this.page, identifier, minLength));
const posts = await this.queue.push(action);
return posts;
}
/**
*
* @param {String | Post} identifier this can either be the link of a post or an instance of the Post Class
* @returns {Promise<PostDetails>}
*/
async getPostDetails(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getPostDetails(this.page, identifier));
const postDetails = await this.queue.push(action);
return postDetails;
}
/**
*
* @param {String | Post} identifier this can either be the link of a post or an instance of the Post Class
* @returns {Promise<any>}
*/
async likePost(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => actions.likePost(this.page, identifier));
await this.queue.push(action);
}
/**
*
* @param {String | Post} identifier this can either be the link of a post or an instance of the Post Class
* @returns {Promise<any>}
*/
async unlikePost(identifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => actions.unlikePost(this.page, identifier));
await this.queue.push(action);
}
/**
*
* @param {String | Post} postIdentifier this can either be the link of a post or an instance of the Post Class
* @param {String} comment the text you want to comment on the post
* @returns {Promise<any>}
*/
async commentPost(postIdentifier, comment) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => actions.commentPost(this.page, postIdentifier, comment));
await this.queue.push(action);
}
/**
*
* @param {String | Post} postIdentifier can either be the link to a post or an instance of the Post class
* @param {Number} [ minComments = 5 ]
* @returns {Promise<Comment[]>}
*/
async getPostComments(postIdentifier, minComments = 5) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => data.getPostComments(this.page, postIdentifier, minComments));
const comments = await this.queue.push(action);
return comments;
}
/**
*
* @param {String | SearchResult | User} userIdentifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @param {String} message
* @returns {Promise<void>}
*/
async directMessageUser(userIdentifier, message) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => channel.directMessageUser(this.page, userIdentifier, message));
await this.queue.push(action);
}
/**
*
* @param {puppeteer.Page} page
* @param {User | SearchResult | String} userIdentifier can either be a username, link, an instance of the User class or a SearchResult which links to a User
* @returns {Promise<DirectMessage[]>}
*/
async getChannelMessages(userIdentifier) {
if(!this.browser.isConnected()) throw new IBError(errorMessage.browserNotRunning.code, errorMessage.browserNotRunning.message);
if(!this.authenticated) throw new IBError(errorMessage.notAuthenticated.code, errorMessage.notAuthenticated.message);
const action = new Action(() => channel.getChannelMessages(this.page, userIdentifier));
const messages = await this.queue.push(action);
return messages;
}
}
module.exports = InstagramBot;