Skip to content

Commit

Permalink
better logging and lesser throttle time
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliasyoussef47 committed Aug 17, 2020
1 parent 1c4887b commit ccd1379
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions InstaAutoComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {IgLoginTwoFactorRequiredError} from 'instagram-private-api/dist/errors/i
import {AccountRepository} from "instagram-private-api/dist/repositories/account.repository";
// import {inquirer} from "inquirer";
import {IgActionSpamError, IgResponseError, UserFeed} from "instagram-private-api";
const chalk = require('chalk');

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

constructor(userPk: number) {
this.ig = new IgApiClient();
Expand Down Expand Up @@ -139,8 +141,7 @@ export class InstaAutoComment {
do {
userFeedItems = await userFeed.items();
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
console.log('.');
await new Promise(r => setTimeout(r, waitSeconds));
await new Promise(r => setTimeout(r, this.throttleSeconds));
for (let userFeedItem of userFeedItems) {
if ((nowTimestamp - userFeedItem.taken_at) <= secondsOld || secondsOld === -1) {// if the post isn't older than secondsOld
if (doComment == DoComment.IfThereIsNoComment) {
Expand Down Expand Up @@ -203,16 +204,14 @@ export class InstaAutoComment {
let mediaCommentsFeed = this.ig.feed.mediaComments(feedItemId);
let currentUser = await this.ig.account.currentUser();
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
console.log('.');
await new Promise(r => setTimeout(r, waitSeconds));
await new Promise(r => setTimeout(r, this.throttleSeconds));
let subscription;
let res = false;
let feedItems;
do {
feedItems = await mediaCommentsFeed.items();
// console.log("SETTING TIMEOUT PROMISE FOR: " + (waitSeconds) + " SECONDS", new Date().toLocaleTimeString());
console.log('.');
await new Promise(r => setTimeout(r, waitSeconds));
await new Promise(r => setTimeout(r, this.throttleSeconds));
if (feedItems !== undefined) {
for (let feedItem of feedItems) {
if (feedItem.user_id === currentUser.pk) {
Expand Down Expand Up @@ -240,9 +239,8 @@ export class InstaAutoComment {
comment.text = InstaAutoComment.choseRandomComment(commentsArray);
try {
commentResult = await mediaRepo.comment(comment);
console.log("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text);
console.log(chalk.green("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text));
// console.log("SETTING TIMEOUT PROMISE FOR: " + (delayBetweenComments) + " SECONDS", new Date().toLocaleTimeString());
console.log('.');
await new Promise(r => setTimeout(r, delayBetweenComments));
} catch (err) {
if (err instanceof IgActionSpamError) {
Expand Down Expand Up @@ -309,7 +307,7 @@ export class InstaAutoComment {
}
}

enum DoComment {
export enum DoComment {
IfThereIsNoComment,
EvenIfThereIsAComment
}
Expand Down
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {InstaAutoComment} from "./InstaAutoComment";
import {DoComment} from "./InstaAutoComment";
import * as util from "util";
require('dotenv').config();
const chalk = require('chalk');

let ref, urlSegmentToInstagramId, instagramIdToUrlSegment;
ref = require('instagram-id-to-url-segment');
Expand Down Expand Up @@ -28,9 +30,12 @@ let iac = new InstaAutoComment(trackingUserPk);
(async () => {
iac.login().then(async r => {
let intervalFunc = async () => {
await iac.selectUserItemsWithTimeRestriction(postSecondsOld);
console.log(chalk.blue('LOOP START'), new Date().toLocaleTimeString());
await iac.selectUserItemsWithTimeRestriction(postSecondsOld, DoComment.IfThereIsNoComment);
// await iac.printIdsToCommentOn();
await iac.postRandomCommentsOnSelectedItems(commentsArray, 3000);
await iac.postRandomCommentsOnSelectedItems(commentsArray, 1000);
console.log(chalk.blue('LOOP END'), new Date().toLocaleTimeString());
console.log('\n');
};
await intervalFunc();
let interval = setInterval(intervalFunc, loopInterval);
Expand Down

0 comments on commit ccd1379

Please sign in to comment.