-
Notifications
You must be signed in to change notification settings - Fork 14
/
run.js
90 lines (83 loc) · 3.39 KB
/
run.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
const cheerio = require('cheerio');
const E = require('events');
const request = require('request');
const separateReqPool = {maxSockets: 15};
const async = require('async');
let tweets={},apiurls=[],N=[];
/////////////////////////// CONFIGURE TWITTER HANDLERS /////////////////////////////////////////////////////
var THandlers=[
{
name:'WhaleWatch',
url:"https://twitter.com/whalewatchio?lang=en",
webhook:"",
avatar_url:"https://www.sideshow.com/storage/product-images/903429/thanos_marvel_feature.jpg",
keywords:"long",
},
{
name:'Karthik',
url:"https://twitter.com/Karthikdk72?lang=en",
webhook:"",
avatar_url:"https://www.sideshow.com/storage/product-images/903429/thanos_marvel_feature.jpg",
keywords:"*",
}
];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ADD TWEETS
THandlers.forEach((th,i) => {
tweets[th.url] = [];
apiurls.push(th.url);
});
//DISCORD WEBHOOK
const sendDiscordMessage = (pl) => {
const {content,turl} = pl;
const {name,webhook,avatar_url} = THandlers.filter((d,i) => d.url === turl)[0];
request.post(webhook).form({username:name,avatar_url:avatar_url,content:content});
}
console.log('Twitter => Discord program is running');
//MONITOR
setInterval(() => {
async.map(apiurls, function(item, callback){
request({url: item, pool: separateReqPool}, function (error, response, body) {
try {
const $ = cheerio.load(body);
var turl = "https://twitter.com" + response.req.path;
if(!tweets[turl].length){
//FIRST LOAD
for(let i=0;i<$('div.js-tweet-text-container p').length;i++){
tweets[turl].push($('div.js-tweet-text-container p').eq(i).text());
}
}
else{
//EVERY OTHER TIME
for(let i=0;i<$('div.js-tweet-text-container p').length;i++){
const s_tweet = $('div.js-tweet-text-container p').eq(i).text();
//CHECK IF TWEET IS NEWS
if(tweets[turl].indexOf(s_tweet) === -1){
tweets[turl].push(s_tweet);
const th_kw = THandlers.filter((d,i) => d.url === turl)[0].keywords.split(',');
const th_name = THandlers.filter((d,i) => d.url === turl)[0].name;
let nFlag=false;
th_kw.forEach((kw,j) => {
if(kw === '*'){
nFlag=true;
}
else{
if(s_tweet.indexOf(kw) != -1){
nFlag=true;
}
}
});
if(nFlag){
sendDiscordMessage({content:s_tweet,turl:turl});
}
}
}
}
} catch (e) {
console.log('Error =>' + e);
}
});
}, function(err, results){
//console.log(results);
});
},1000);//RUNS EVERY 1 SECONDS