Skip to content

Commit

Permalink
finished monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz99 committed Jan 2, 2019
1 parent 1a803ba commit de120a1
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 191 deletions.
12 changes: 6 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"interval": 5000,
"app": {
"other": {
"twittername": ["cybersole", "washed_kid", "destroyerbots"],
"twittername": ["cybersole", "washed_kid", "destroyerbots", "shtmonitor"],
"keywords": ["Restock", "Password", "Test"]
},
"consumer": {
"key": "sQpklAhQp4GU0U3dQ7qHtLSPL",
"secret": "VO6Cx7q6NlzCknmGiO0bvq7OuzzPO4ro6Bgwsi284St2Xxj1Gv"
"key": "",
"secret": ""
},
"access": {
"token": "3648333676-BOQJc5Rm9udIYwn7ULRBrVNJFveRZh4whFje126",
"secret": "rDiR4CGdL77Z0JIoejSD3oaSCPhADXQ3Kvk9Zs5elHNXk"
"token": "",
"secret": ""
}
},
"webhook": "https://discordapp.com/api/webhooks/510314361865240591/KI080uVUm8w6MSP9XvwxJTW0oWoFnpERat4NJscuXdKeE4P_Lu40GFvxy2Iu65qcpaxv"
"webhook": ""
}
248 changes: 78 additions & 170 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,173 +1,81 @@
/* Special Thank you to _luqy or matt, don't know you but, I want to give you credit! */

/* LINE: 108; just do another if statement to check if keyword does not match, which means we get anything the user tweets from most recent! */

try {
require("console-stamp")(console, {
colors: {
stamp: "yellow",
label: "cyan",
label: true,
metadata: "green"
const log = require("./lib/logger")("Twitter Monitor V1");
const path = require("path");
const twit = require("twit");
const request = require("request");
const notify = require("./lib/notify");

const config = require(path.join(__dirname, "config.json"));

// twitter configuration
var T = new twit({
consumer_key: config.app.consumer.key,
consumer_secret: config.app.consumer.secret,
access_token: config.app.access.token,
access_token_secret: config.app.access.secret
});

let headers = {
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9",
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Cache-Control": "max-age=0",
Connection: "keep-alive"
};

/* Start up function */
(function() {
const Twitter = {
userID: [],
tweetID: [],
init: function() {
this.connector().then(() => this.startMonitor());
},
connector: function() {
log.green("Initializing Twitter Monitor...");
return new Promise((resolve, reject) => {
config.app.other.twittername.forEach(name => {
T.get("/users/show", { screen_name: name }, (err, data, res) => {
if (err) {
reject();
return log.red("ERROR" + err);
}

this.userID.push(data.id_str);
if (this.userID.length == config.app.other.twittername.length) {
resolve();
}
});
});
});
},
startMonitor: function() {
var stream = T.stream("statuses/filter", { follow: this.userID });

stream.on("connected", res => {
log.green(
"Twitter Monitor is connected... ~ Currently monitoring " +
config.app.other.twittername.length +
" profiles"
);
});

stream.on("tweet", tweet => {
if (!this.tweetID.includes(tweet.id_str)) {
this.tweetID.push(tweet.id_str);
log.green("****** TWEET DETECTED ******");
log.blue(`[USER: ${tweet.user.screen_name}] - Just tweeted!`);
log.blue(`[TIMESTAMP] - ${tweet.timestamp_ms}`);
log.yellow("Sent discord webhook!");
// notify user
notify(headers, tweet, config);
}
});
}
});

const path = require("path");
const twit = require("twit");
const request = require("request");

const config = require(path.join(__dirname, "config.json"));

var Twitter = new twit({
consumer_key: config.app.consumer.key,
consumer_secret: config.app.consumer.secret,
access_token: config.app.access.token,
access_token_secret: config.app.access.secret
});

console.log("Custom Twitter OCR Monitor ~ Written by Eric!");
console.log("Scanning for new tweets...");

let keywords = config.app.other.keywords;
let idarray = [];

let headers = {
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9",
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Cache-Control": "max-age=0",
Connection: "keep-alive"
};

function startMonitor() {
/* Only way to send multiple request is by looping through the twitter names */
config.app.other.twittername.forEach(name => {
Twitter.get(
"statuses/user_timeline",
{
screen_name: name,
count: 1,
exclude_replies: true,
include_rts: false
},
function(err, data) {
if (!err) {
data.forEach(function(tweet) {
let text = tweet.text.toLowerCase();
let id = tweet.id_str;

keywords.forEach(keyword => {
if (text.includes(keyword.toLowerCase())) {
if (
text.match(/restock/g) ||
text.match(/live/g) ||
text.match(/hi/g) ||
text.match(/test/g)
) {
if (!(idarray.indexOf(id) > -1)) {
console.log("Found matching keyword: " + keyword + "!");
console.log("Found tweet id: " + id);

/* Send Webhook */
let opts = {
url: config.webhook,
method: "POST",
headers: headers,
json: {
embeds: [
{
title: `Tweet from @${tweet.user.screen_name}`,
url: `https://twitter.com/${
tweet.user.name
}/status/${id}`,
color: 1768289,
footer: {
text: "Custom Twitter OCR Monitor V1"
},
fields: [
{
name: "Description",
value: text,
inline: true
}
]
}
]
}
};
request(opts);
console.log("Sent Hook!");

idarray.push(id);
}
}
} else if (text.match(/[\s\S]+/g)) {
// /[\s\S]+/g
// /^\b\w+\b$/i
// /\/\*([\s\S]*?)\*\//g

if (!(idarray.indexOf(id) > -1)) {
console.log("Found matching keyword: * ");
console.log("Found tweet id: " + id);

/* Send Webhook */
let opts = {
url: config.webhook,
method: "POST",
headers: headers,
json: {
embeds: [
{
title: `Tweet from @${tweet.user.screen_name}`,
url: `https://twitter.com/${
tweet.user.name
}/status/${id}`,
color: 1768289,
footer: {
text: "Custom Twitter OCR Monitor V1"
},
fields: [
{
name: "Description",
value: text,
inline: true
}
]
}
]
}
};
request(opts);
console.log("Sent Hook!");

idarray.push(id);
}
}
});
});
} else {
console.log(
"An error occurred while attempting to search for tweets: " +
err.message
);
}
}
);
});
}

function ocrMonitor() {
/* Intelligence Twitter Bot - hehe */
}

startMonitor();
setInterval(startMonitor, config.interval);
} catch (err) {
if (err) {
console.log(err);
}
}
Twitter.init();
})();
48 changes: 48 additions & 0 deletions lib/logger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// # Logger Class
// is responsible for logging the relevant information to the user

// # Import Dependencies
const dateFormat = require("dateformat");
const colors = require("colors/safe");
const fs = require("fs");

const Logger = function Logger(name) {
this._name = name;
};

Logger.prototype.green = function success(message) {
console.log(
`${getDateString()} [${this._name}] ` + colors.green("[+] " + message)
);
};

Logger.prototype.red = function error(message) {
console.log(
`${getDateString()} [${this._name}] ` + colors.red("[x] " + message)
);
};

Logger.prototype.blue = function won(message) {
console.log(
`${getDateString()} [${this._name}] ` + colors.blue("[$] " + message)
);
};

Logger.prototype.normal = function info(message) {
console.log(`${getDateString()} [${this._name}] [#] ${message}`);
};

Logger.prototype.yellow = function caution(message) {
console.log(
`${getDateString()} [${this._name}] ` + colors.yellow("[?] " + message)
);
};

function getDateString() {
return "[" + dateFormat(new Date(), "HH:MM:ss.l") + "]";
}

// export for use elsewhere
module.exports = function(name) {
return new Logger(name);
};
39 changes: 39 additions & 0 deletions lib/notify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const request = require("request");

module.exports = function(headers, data, config) {
/* Send Webhook */
let opts = {
url: config.webhook,
method: "POST",
headers: headers,
json: {
embeds: [
{
title: `Tweet from @${data.user.screen_name}`,
url: `https://twitter.com/${data.user.screen_name}/status/${
data.id_str
}`,
color: 1768289,
footer: {
text: `Twitter Monitor - ${data.created_at}`
},
fields: [
{
name: "Description",
value: data.text,
inline: true
}
],
image: {
url:
data.entities.media !== undefined
? data.entities.media[0].media_url
: null
}
}
]
}
};

request(opts);
};
Loading

0 comments on commit de120a1

Please sign in to comment.