-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
103 lines (76 loc) · 2.27 KB
/
app.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
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const app = express();
app.get("/", (req,res)=>{
var Twit = require('twit');
var key = require('./key.json');
let T = new Twit(key)
let our_id=""
let our_username="nocontextnocon2"
function request(url){
return T.get(url).then( data => {
return data;
});
}
async function getMax(id){
let mxFav = -1;
let tweetId = "";
let url = "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=" + id + "&count=200"+"&stringify_ids=true";
let tweets = await request(url);
tweets = tweets.data;
tweets.forEach(async tweet => {
let str = tweet.created_at;
let created_date = new Date(
str.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
"$1 $2 $4 $3 UTC"));
created_date = created_date.getTime();
let now = new Date();
now = now.getTime();
let hours = 3;
if(now - created_date > hours*60*60*1000) // in the last x hours
return ;
let fav = 0;
if(tweet.retweeted_status == undefined)
fav = tweet.favorite_count;
else
fav = tweet.retweeted_status.favorite_count; //rts included
if(fav > mxFav){
mxFav = fav;
tweetId = tweet.id_str;
}
});
return [mxFav,tweetId];
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
async function autoRT(id , count=200)
{
let url="";
if(id => /\D/.test(id))
url = "https://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name="+id+"&count="+count+"&stringify_ids=true";
else
url = "https://api.twitter.com/1.1/friends/ids.json?cursor=-1&user_id="+id+"&count="+count+"&stringify_ids=true";
let users = await request(url);
users = users.data.ids;
let globalMaxFav = -1;
let tweetId = "";
await asyncForEach(users, async id=>{
let data = await getMax(id);
if(data[0] > globalMaxFav){
globalMaxFav = data[0];
tweetId = data[1];
}
console.log(data[0]);
});
console.log(globalMaxFav,tweetId);
T.post('statuses/retweet/:id', { id: tweetId }, (err, data, response)=>{
console.log(data)
});
}
autoRT(our_username);
setInterval( ()=>autoRT(our_username) , 180 * 60 * 1000) // each 180 minutes
res.send("<h1>hi bro</h1>");
});
app.listen(process.env.PORT || 3000, "0.0.0.0");