-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost.js
484 lines (355 loc) · 13.4 KB
/
ghost.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
// import Puppeteer from "puppeteer-core";
// import Instagram from "./Instagram.js";
const Puppeteer = require("puppeteer-core");
const Instagram = require("./Instagram.js");
class Ghost
{
constructor(inpInfo,seeEvents) {
this.InputInfo = {
userName: inpInfo.userName,
Password: inpInfo.Password,
hashTag: inpInfo.hashTag,
pageCount: inpInfo.pageCount,
Comment: inpInfo.Comment,
showEvents: !seeEvents,
};
this.Result = {
pgAddresses: new Set(),
// pgAddresses: [ ],
pgIndicator: 1,
closedPgsCounter: 0,
successfullComments: 0,
missedComments: 0
}
this.instaBot = new Instagram(this.InputInfo.showEvents);
}
get getInstaBot(){
return this.instaBot.clone();
}
set setInstaBot(igb){
this.instaBot = igb;
}
get getPublicInfo(){
return this.InputInfo;
}
set setPublicInfo(pi){
this.InputInfo = pi;
}
get getUserName(){
return this.InputInfo.userName.clone();
}
set setUserName(un){
this.InputInfo.userName = un;
}
get getPassword(){
return this.InputInfo.Password.clone();
}
set setPassword(p){
this.InputInfo.Password = p;
}
set setHashTag(h){
this.InputInfo.hashTag = h;
}
get getHashTag(){
return this.InputInfo.hashTag.clone();
}
get getPageCounter(){
return this.InputInfo.pageCount;
}
set setPageCounter(c){
this.InputInfo.pageCount = c;
}
get getContent(){
return this.InputInfo.Comment.clone();
}
set setContent(c){
this.InputInfo.Comment = c;
}
// async goToInstagram(){
//
// this.instaBot.goToAddress("https://instagram.com/accounts/login",0).then(() => {
// console.log("ghost : I opened instagram.");
// }).catch((err) => {
// console.log("ghost : I can't open instagram.");
// });
// }
async Login(){
// debugger;
// entering username //
await this.instaBot.writeInElement("[name=username]",0,this.InputInfo.userName);
// entering password //
await this.instaBot.writeInElement("[name=password]",0,this.InputInfo.Password);
// click on 'login' button //
await this.instaBot.clickOnElement("[type=submit]",0);
await this.instaBot.core.Tabs[0].waitForSelector("p[role=alert]",{timeout: 4000}).then(async () => {
console.log("ghost : we can't login ;)");
await this.instaBot.core.Browser.close();
process.exit(0);
}).catch(() => {
console.log("ghost : we logged in successfullly");
});
}
async typeHashTag(){
// debugger;
this.instaBot.writeInElement("[placeholder=Search]",0,this.InputInfo.hashTag);
}
async selectAHashTag(){
// debugger;
this.instaBot.clickOnElement(".yCE8d",0).then(() => {
console.log("ghost : I clicked on a hashTag.");
}).catch(() => {
console.log("ghost : I can't click on a hashTag.");
});
}
async enterEveryPage(){
await this.instaBot.core.Tabs[0].waitForSelector(".KC1QD",{timeOut: 6000}).then(async () => {
console.log("ghost : I found all instagramPages. ;)");
// working with set //
// under testing part //
var tmp = 0;
while(this.Result.pgAddresses.size <= this.InputInfo.pageCount)
{
// scrolling main page //
await this.instaBot.core.Tabs[0].evaluate(() => {
window.scrollBy({
top: 200,
left: 0,
// behavior: "smooth"
});
});
// extracting pages links //
let arr = await this.instaBot.core.Tabs[0].evaluate(() => {
let linksNode = document.querySelectorAll("a:not([class])");
let linksArr = [ ];
for(let i = 0 ; i < linksNode.length ; i++)
{
linksArr.push("https://instagram.com" + linksNode.item(i).getAttribute("href"));
}
return { linksAddresses: linksArr };
});
for(let x = tmp ; x < arr.linksAddresses.length ; x++)
{
this.Result.pgAddresses.add(arr.linksAddresses[x]);
}
tmp = arr.linksAddresses.length;
}
debugger;
this.Result.pgAddresses.delete("https://instagram.com/explore/");
this.Result.pgAddresses.delete("https://instagram.com/" + this.InputInfo.userName + "/");
// testing //
console.log(this.Result.pgAddresses);
// ---------- //
// ---------------------------------------- //
// working with array //
// let Links = await this.instaBot.core.Tabs[0].evaluate(() => {
//
// let linksNode = document.querySelectorAll("a:not([class])");
// let linksArr = [ ];
//
// for(let i = 0 ; i < linksNode.length ; i++)
// {
// linksArr.push("https://instagram.com" + linksNode.item(i).getAttribute("href"));
// }
//
// return { linksAddresses: linksArr };
// });
//
// // this.Result.pgAddresses = new Set(Links.linksAddresses);
// this.Result.pgAddresses = Links.linksAddresses;
// testing //
// for(let lnkInd = 0 ; lnkInd < this.InputInfo.pageCount ; lnkInd++)
// {
// console.log("link[" + (lnkInd + 1) + "] = " + Links.linksAddresses[lnkInd]);
// }
// ----------- //
await this.commentingOnPages(4,this.Result.pgAddresses.values());
// let pg = null;
//
// for(let j = 0 ; j < this.InputInfo.pageCount ; j++)
// // for(let j = 0 ; j < Links.linksAddresses.length ; j++)
// {
// pg = await this.instaBot.core.Browser.newPage();
//
// this.instaBot.goToLink(Links.linksAddresses[j],pg);
// this.instaBot.core.Tabs.push(pg);
// }
//
// // click on a link to going to instagram page //
// for(let b = 1 ; b <= this.InputInfo.pageCount ; b++)
// // for(let b = 0 ; b < Links.linksAddresses.length ; b++)
// {
// this.enterInPage(b);
// }
}).catch(() => {
console.log("ghost : I can't find InstagramPages.");
this.instaBot.core.Browser.close();
});
}
async commentingOnPages(n,iter){
let pg = null;
let to = (this.Result.pgIndicator + n);
let from = this.Result.pgIndicator;
// under testing part //
// var iter = this.Result.pgAddresses.values();
// for(let j = 0 ; j < this.InputInfo.pageCount ; j++)
// for(let j = 0 ; j < Links.linksAddresses.length ; j++)
for(let j = from ; j < to && (this.Result.pgIndicator <= this.InputInfo.pageCount) ; j++ )
{
// creating new tab //
pg = await this.instaBot.core.Browser.newPage();
this.Result.pgIndicator++;
// testing //
console.log("testing -> pgIndicator = " + this.Result.pgIndicator);
// console.log("testing -> itr.next() = " + iter.next().value);
// ------- //
// going to address in new tab //
this.instaBot.goToLink(iter.next().value,pg);
// this.instaBot.goToLink(this.Result.pgAddresses[j],pg);
this.instaBot.core.Tabs.push(pg);
this.enterInPage(j);
this.insertComment(j,iter);
}
// ----------------------------- //
}
async enterInPage(pgIndex){
await this.instaBot.waitUntilElementAppear(pgIndex,".FPmhX");
await this.instaBot.clickOnElement(".FPmhX",pgIndex);
}
async insertComment(pageIndex,itrIndx){
debugger;
await this.instaBot.core.Tabs[pageIndex].waitForSelector("._2z6nI",{timeout: 60000}).then(async function(){
// for(let postIndex = 0 ; postIndex < 1 ; postIndex++) {
// click on a post of InstagramPage //
await this.instaBot.core.Tabs[pageIndex].evaluate("document.querySelectorAll('a:not([class])')[" + (pageIndex % 3) + "].click();");
// typing and clicking on 'Post' button and inserting comment. //
await this.instaBot.core.Tabs[pageIndex].waitForSelector(".Ypffh", {timeout: 4000}).then(async () => {
var waitTime;
if(parseInt((pageIndex / 5)) == 0) {
waitTime = 0;
}
else{
waitTime = 60000;
}
// wait for one minute... //
await this.instaBot.core.Tabs[pageIndex].waitFor(waitTime).then( function(wt){
console.log("ghost : I will wait for " + wt + ". ;)");
}(waitTime)).catch(() => {
console.log("ghost : I can't wait . error is in page " + pageIndex + " . in insert comment method.");
});
// like post //
// await this.instaBot.core.Tabs[pageIndex].evaluate(() => {
// document.querySelectorAll(".glyphsSpriteHeart__outline__24__grey_9")[1].click();
// }).then(() => {
// console.log("ghost : post liked. ;)");
// }).catch(() => {
// console.log("ghost : can't like post.");
// });
await this.instaBot.core.Tabs[pageIndex].type(".Ypffh", this.InputInfo.Comment[pageIndex - 1]);
// await this.instaBot.core.Tabs[pageIndex].waitFor(2000);
await this.instaBot.core.Tabs[pageIndex].waitFor((pageIndex % 5) * 20000);
await this.instaBot.core.Tabs[pageIndex].click("[type=submit]");
await this.instaBot.core.Tabs[pageIndex].waitFor(2000);
await this.instaBot.core.Tabs[pageIndex].screenshot({path: "./screenshots/" + pageIndex + ".png"});
await this.instaBot.core.Tabs[pageIndex].waitFor(4000);
// dealing with couldn't post comment alert //
await this.instaBot.core.Tabs[pageIndex].waitForSelector(".gxNyb",{timeOut: 6000}).then( async function(){
this.Result.successfullComments++;
}.bind(this)).catch(function(){
this.Result.missedComments++;
}.bind(this));
// click on close button //
// await this.instaBot.core.Tabs[pageIndex].click(".ckWGn");
// close tab //
await this.instaBot.core.Tabs[pageIndex].close().then(function(pgi){
console.log("ghost : page[" + pgi + "] done. ;)");
this.Result.closedPgsCounter++;
if( ( (this.Result.closedPgsCounter % 5) == 4) && (pgi < this.InputInfo.pageCount) )
{
this.commentingOnPages(5,itrIndx);
}
// logout //
if(this.Result.closedPgsCounter == this.InputInfo.pageCount)
{
this.logOut();
}
}.bind(this)(pageIndex));
}).catch(async () => {
console.log("ghost : we deal with limited post. can't find textarea.it is removed.");
await this.instaBot.core.Tabs[pageIndex].close();
this.Result.closedPgsCounter++;
if(this.Result.closedPgsCounter == this.InputInfo.pageCount){
this.logOut();
}
});
// }
}.bind(this));
}
insertCommentForEveryPage(){
let eP = (this.Result.pgIndicator + 5);
// for(let pindex = 1; pindex < this.instaBot.core.Tabs.length ; pindex++)
for(let pindex = this.Result.pgIndicator ; pindex < eP ; pindex++)
{
this.insertComment(pindex);
}
}
async logOut(){
await this.instaBot.core.Tabs[0].click(".glyphsSpriteUser__outline__24__grey_9");
await this.instaBot.core.Tabs[0].waitForSelector(".glyphsSpriteSettings__outline__24__grey_9",{timeOut: 6000}).then(async () => {
await this.instaBot.core.Tabs[0].click(".glyphsSpriteSettings__outline__24__grey_9");
}).catch(() => {
console.log("ghost : can't find gear button.");
});
await this.instaBot.core.Tabs[0].waitForSelector(".mt3GC",{timeOut: 6000}).then(async () => {
await this.instaBot.core.Tabs[0].evaluate(() => {
document.querySelectorAll(".aOOlW")[6].click();
});
console.log("ghost : we logged out. :)");
}).catch(() => {
console.log("ghost : can't logOut .");
});
await this.instaBot.core.Tabs[0].waitFor(4000);
await this.instaBot.core.Browser.close();
// tells us how many comments are inserted successfully //
this.reportResult();
}
reportResult(){
console.log("ghost : mission completed. ;)");
console.log("ghost : successfull comments = " + this.Result.successfullComments);
console.log("ghost : missed comments = " + this.Result.missedComments);
}
async makeAliveMe(){
// invoke all methods that ghost needs to do work e.g commenting , following , ... //
console.log("ghost : Hi Sir. My name is ghost. ready to use. :)");
debugger;
await this.instaBot.Initialize();
await this.Login();
await this.instaBot.waitUntilElementAppear(0,".bIiDR").then(async () => {
console.log("ghost : Notification alert appeared. ;)");
await this.instaBot.clickOnElement(".bIiDR",0).then(() => {
console.log("ghost : I clicked on turnOn button. :)");
}).catch(() => {
console.log("ghost : can't find and click turnOn button");
});
}).catch(() => {
console.log("ghost : Notification aler doesn't appeared.");
});
console.log("ghost : We logged in. ;)");
await this.typeHashTag();
// wait until hashtaglist appear. //
await this.instaBot.waitUntilElementAppear(0,".drKGC").then(async () => {
console.log("ghost : hashTagList appeared. ;)");
// clicking on a hashTag after seeing the hashtaglist //
await this.selectAHashTag().then(function(){
console.log("ghost : I clicked on a hashTag.");
}).catch(() => {
console.log("ghost : I can't click on any hashTag.");
});
}).catch(() => {
console.log("ghost : hashTagList doesn't appear.");
});
await this.instaBot.core.Tabs[0].waitFor(3000);
debugger;
await this.enterEveryPage();
}
}
module.exports = Ghost;