Skip to content

Commit ccd1379

Browse files
better logging and lesser throttle time
1 parent 1c4887b commit ccd1379

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

.idea/inspectionProfiles/Project_Default.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

InstaAutoComment.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {IgLoginTwoFactorRequiredError} from 'instagram-private-api/dist/errors/i
88
import {AccountRepository} from "instagram-private-api/dist/repositories/account.repository";
99
// import {inquirer} from "inquirer";
1010
import {IgActionSpamError, IgResponseError, UserFeed} from "instagram-private-api";
11+
const chalk = require('chalk');
1112

1213
let ref, urlSegmentToInstagramId, instagramIdToUrlSegment;
1314
ref = require('instagram-id-to-url-segment');
@@ -31,6 +32,7 @@ export class InstaAutoComment {
3132
public account: AccountRepository;
3233
public idsToCommentOn = [];
3334
private userPk: number;
35+
private throttleSeconds = 1000;
3436

3537
constructor(userPk: number) {
3638
this.ig = new IgApiClient();
@@ -139,8 +141,7 @@ export class InstaAutoComment {
139141
do {
140142
userFeedItems = await userFeed.items();
141143
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
142-
console.log('.');
143-
await new Promise(r => setTimeout(r, waitSeconds));
144+
await new Promise(r => setTimeout(r, this.throttleSeconds));
144145
for (let userFeedItem of userFeedItems) {
145146
if ((nowTimestamp - userFeedItem.taken_at) <= secondsOld || secondsOld === -1) {// if the post isn't older than secondsOld
146147
if (doComment == DoComment.IfThereIsNoComment) {
@@ -203,16 +204,14 @@ export class InstaAutoComment {
203204
let mediaCommentsFeed = this.ig.feed.mediaComments(feedItemId);
204205
let currentUser = await this.ig.account.currentUser();
205206
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
206-
console.log('.');
207-
await new Promise(r => setTimeout(r, waitSeconds));
207+
await new Promise(r => setTimeout(r, this.throttleSeconds));
208208
let subscription;
209209
let res = false;
210210
let feedItems;
211211
do {
212212
feedItems = await mediaCommentsFeed.items();
213213
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
214-
console.log('.');
215-
await new Promise(r => setTimeout(r, waitSeconds));
214+
await new Promise(r => setTimeout(r, this.throttleSeconds));
216215
if (feedItems !== undefined) {
217216
for (let feedItem of feedItems) {
218217
if (feedItem.user_id === currentUser.pk) {
@@ -240,9 +239,8 @@ export class InstaAutoComment {
240239
comment.text = InstaAutoComment.choseRandomComment(commentsArray);
241240
try {
242241
commentResult = await mediaRepo.comment(comment);
243-
console.log("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text);
242+
console.log(chalk.green("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text));
244243
// console.log("SETTING TIMEOUT PROMISE FOR: " + (delayBetweenComments) + " SECONDS", new Date().toLocaleTimeString());
245-
console.log('.');
246244
await new Promise(r => setTimeout(r, delayBetweenComments));
247245
} catch (err) {
248246
if (err instanceof IgActionSpamError) {
@@ -309,7 +307,7 @@ export class InstaAutoComment {
309307
}
310308
}
311309

312-
enum DoComment {
310+
export enum DoComment {
313311
IfThereIsNoComment,
314312
EvenIfThereIsAComment
315313
}

index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {InstaAutoComment} from "./InstaAutoComment";
2+
import {DoComment} from "./InstaAutoComment";
23
import * as util from "util";
34
require('dotenv').config();
5+
const chalk = require('chalk');
46

57
let ref, urlSegmentToInstagramId, instagramIdToUrlSegment;
68
ref = require('instagram-id-to-url-segment');
@@ -28,9 +30,12 @@ let iac = new InstaAutoComment(trackingUserPk);
2830
(async () => {
2931
iac.login().then(async r => {
3032
let intervalFunc = async () => {
31-
await iac.selectUserItemsWithTimeRestriction(postSecondsOld);
33+
console.log(chalk.blue('LOOP START'), new Date().toLocaleTimeString());
34+
await iac.selectUserItemsWithTimeRestriction(postSecondsOld, DoComment.IfThereIsNoComment);
3235
// await iac.printIdsToCommentOn();
33-
await iac.postRandomCommentsOnSelectedItems(commentsArray, 3000);
36+
await iac.postRandomCommentsOnSelectedItems(commentsArray, 1000);
37+
console.log(chalk.blue('LOOP END'), new Date().toLocaleTimeString());
38+
console.log('\n');
3439
};
3540
await intervalFunc();
3641
let interval = setInterval(intervalFunc, loopInterval);

0 commit comments

Comments
 (0)