From b4217244d65cf856971fa2f02c89a02bfa3ec01f Mon Sep 17 00:00:00 2001 From: Elias youssef Date: Sat, 22 Aug 2020 00:23:10 +0200 Subject: [PATCH] Added pushbullet notification --- .env.example | 2 + .idea/dictionaries/d_s_a.xml | 1 + InstaAutoComment.ts | 8 +-- index.ts | 96 +++++++++++++++++++++++------------- package-lock.json | 63 +++++++++++++++++++++++ package.json | 1 + 6 files changed, 134 insertions(+), 37 deletions(-) diff --git a/.env.example b/.env.example index 3f94a2c..dd75802 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,5 @@ IG_USERNAME= IG_PASSWORD= TRACKED_USERS_USERNAMES="eminem, logic" COMMENTS_ARRAY="Comment 1, Comment 2, Comment 3" +PUSHBULLET_NOTIFICATION=true +PUSHBULLET_ACCESS_TOKEN= diff --git a/.idea/dictionaries/d_s_a.xml b/.idea/dictionaries/d_s_a.xml index f35a581..68e9d17 100644 --- a/.idea/dictionaries/d_s_a.xml +++ b/.idea/dictionaries/d_s_a.xml @@ -2,6 +2,7 @@ insta + pushbullet usernames diff --git a/InstaAutoComment.ts b/InstaAutoComment.ts index fa18336..7ec68f9 100644 --- a/InstaAutoComment.ts +++ b/InstaAutoComment.ts @@ -242,6 +242,7 @@ export class InstaAutoComment { try { commentResult = await mediaRepo.comment(comment); + console.log('\n'); console.log(chalk.green("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text)); } catch (err) { if (err instanceof IgActionSpamError) { @@ -300,10 +301,11 @@ export class InstaAutoComment { /** * A wrapper function to log to the console * @param name - * @returns {(data) => void} + * @param data + * @returns void */ - public logEvent(name: string) { - return (data: any) => console.log(chalk.blue(name), chalk.blue(data)); + public logEvent(name: string, data?) { + console.log(chalk.blue(name), chalk.blue(data)); } public async usernamesToPks(usernames: string[]){ diff --git a/index.ts b/index.ts index 0f4008e..830e8eb 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ import {InstaAutoComment} from "./InstaAutoComment"; -import {DoComment} from "./InstaAutoComment"; import * as util from "util"; + require('dotenv').config(); const chalk = require('chalk'); const ora = require('ora'); @@ -23,49 +23,77 @@ util.inspect.defaultOptions.maxArrayLength = null; const loopInterval: number = +process.env.LOOP_INTERVAL; const trackingUserPk: number = +process.env.TRACKING_USER_PK; const postSecondsOld: number = +process.env.POST_SECONDS_OLD; -const commentsArray = process.env.COMMENTS_ARRAY.split(",").map((item)=>item.trim()); -const trackedUsersUsernames: string[] = process.env.TRACKED_USERS_USERNAMES.split(",").map((item)=>item.trim()); +const commentsArray = process.env.COMMENTS_ARRAY.split(",").map((item) => item.trim()); +const trackedUsersUsernames: string[] = process.env.TRACKED_USERS_USERNAMES.split(",").map((item) => item.trim()); let trackedUsersPks: number[]; +let pushbulletNotificationEnabled: boolean = stringToBool(process.env.PUSHBULLET_NOTIFICATION); +let pushbulletAccessToken = process.env.PUSHBULLET_ACCESS_TOKEN; +let pusher; + +if (pushbulletNotificationEnabled) { + const PushBullet = require('pushbullet'); + pusher = new PushBullet(pushbulletAccessToken); +} let iac = new InstaAutoComment(); (async () => { - await iac.login().then(async r => { - trackedUsersPks = await iac.usernamesToPks(trackedUsersUsernames); - iac.trackedUsersUsernames = trackedUsersUsernames; - trackedUsersUsernames.forEach(trackedUsersUsername => { - console.log(chalk.blue("Tracking: ", trackedUsersUsername)); - }); - console.log('\n'); - // you received a notification - iac.ig.fbns.push$.subscribe( - (push) => { - if (push.pushCategory === "post" && trackedUsersPks.includes(Number(push.sourceUserId))) { - iac.commentOnPostWithRandomComment(push.actionParams["id"], commentsArray); + try { + await iac.login().then(async r => { + trackedUsersPks = await iac.usernamesToPks(trackedUsersUsernames); + iac.trackedUsersUsernames = trackedUsersUsernames; + console.log('\n'); + // you received a notification + iac.ig.fbns.push$.subscribe( + (push) => { + if (push.pushCategory === "post" && trackedUsersPks.includes(Number(push.sourceUserId))) { + iac.commentOnPostWithRandomComment(push.actionParams["id"], commentsArray); + } } - } - ); + ); - // the client received auth data - // the listener has to be added before connecting - iac.ig.fbns.auth$.subscribe(async (auth) => { - // logs the auth - // iac.logEvent('auth')(auth); - //saves the auth - await iac.saveState(iac.ig); - }); + // the client received auth data + // the listener has to be added before connecting + iac.ig.fbns.auth$.subscribe(async (auth) => { + // logs the auth + // iac.logEvent('auth')(auth); + //saves the auth + await iac.saveState(iac.ig); + }); - // 'error' is emitted whenever the client experiences a fatal error - iac.ig.fbns.error$.subscribe(iac.logEvent('error')); + // 'error' is emitted whenever the client experiences a fatal error + iac.ig.fbns.error$.subscribe((fbnsError) => { + iac.logEvent('error', fbnsError); + if (pushbulletAccessToken) pusher.note('', 'InstaAutoComment | Error with MQTT client', fbnsError.message); + }); - // 'warning' is emitted whenever the client errors but the connection isn't affected - iac.ig.fbns.warning$.subscribe(iac.logEvent('warning')); + // 'warning' is emitted whenever the client errors but the connection isn't affected + iac.ig.fbns.warning$.subscribe((fbnsWarning) => { + iac.logEvent('warning', fbnsWarning); + }); - // this sends the connect packet to the server and starts the connection - // the promise will resolve once the client is fully connected (once /push/register/ is received) - await iac.ig.fbns.connect(); + // this sends the connect packet to the server and starts the connection + // the promise will resolve once the client is fully connected (once /push/register/ is received) + await iac.ig.fbns.connect(); - const spinner = ora('Waiting for notification').start(); - }); + trackedUsersUsernames.forEach(trackedUsersUsername => { + console.log(chalk.blue("Tracking: ", trackedUsersUsername)); + }); + + const spinner = ora('Waiting for a notification').start(); + }); + } catch (e) { + if (pushbulletAccessToken) pusher.note('', 'InstaAutoComment | Error with program', e.message); + } })(); + +function stringToBool(stringBool: string) { + if (stringBool.toLowerCase() === "true") { + return true; + } else if (stringBool.toLowerCase() === "false") { + return false; + } else { + return false; + } +} diff --git a/package-lock.json b/package-lock.json index 8163c1a..d8c78c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -113,6 +113,11 @@ "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" }, + "async-limiter": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -552,6 +557,11 @@ "resolved": "https://registry.npmjs.org/map-obj/-/map-obj-4.1.0.tgz", "integrity": "sha512-glc9y00wgtwcDmp7GaE/0b0OnxpNJsVf3ael/An6Fe2Q51LLwN1er6sdomLRzz5h0+yMpiYLhWYF5R7HeqVd4g==" }, + "mime": { + "version": "2.4.6", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.6.tgz", + "integrity": "sha512-RZKhC3EmpBchfTGBVb8fb+RL2cWyw/32lshnsETttkBAyAUXSGHxbEJWWRXc751DrIxG1q04b8QwMbAwkRPpUA==" + }, "mime-db": { "version": "1.43.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz", @@ -601,6 +611,11 @@ "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==" }, + "node-forge": { + "version": "0.7.6", + "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-0.7.6.tgz", + "integrity": "sha512-sol30LUpz1jQFBjOKwbjxijiE3b6pjd74YwfD0fJOKPjF+fONKb2Yg8rYgS6+bK6VDl+/wfr4IYpC7jDzLUIfw==" + }, "oauth-sign": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", @@ -649,6 +664,34 @@ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, + "pushbullet": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/pushbullet/-/pushbullet-2.4.0.tgz", + "integrity": "sha512-9CkgzAmBOv/ekxI4oqlFXdFIdKEMQgv1NBUbQYYxo/OsGYdnM2D9xEtWLQLkMAkpZUwbNr9lHq7a27082+eQMw==", + "requires": { + "clone": "^2.1.2", + "mime": "^2.4.0", + "node-forge": "^0.7.6", + "request": "^2.88.0", + "request-promise-native": "^1.0.5", + "ws": "^6.1.2" + }, + "dependencies": { + "clone": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", + "integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=" + }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } + }, "qs": { "version": "6.5.2", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", @@ -717,6 +760,26 @@ "lodash": "^4.17.15" } }, + "request-promise-native": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/request-promise-native/-/request-promise-native-1.0.9.tgz", + "integrity": "sha512-wcW+sIUiWnKgNY0dqCpOZkUbF/I+YPi+f09JZIDa39Ec+q82CpSYniDp+ISgTTbKmnpJWASeJBPZmoxH84wt3g==", + "requires": { + "request-promise-core": "1.1.4", + "stealthy-require": "^1.1.1", + "tough-cookie": "^2.3.3" + }, + "dependencies": { + "request-promise-core": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.4.tgz", + "integrity": "sha512-TTbAfBBRdWD7aNNOoVOBH4pN/KigV6LyapYNNlAPA8JwbovRti1E88m3sYAwsLi5ryhPKsE9APwnjFTgdUjTpw==", + "requires": { + "lodash": "^4.17.19" + } + } + } + }, "restore-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", diff --git a/package.json b/package.json index df52974..f519d95 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "instagram-private-api": "^1.39.2", "instagram_mqtt": "^0.2.16", "ora": "^5.0.0", + "pushbullet": "^2.4.0", "tsc": "^1.20150623.0" }, "scripts": {