-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduledTweet.js
70 lines (57 loc) · 1.98 KB
/
scheduledTweet.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
require('dotenv').config();
const Twit = require('twit');
const request = require('request');
const SECONDS_IN_DAY = 86400;
let T = new Twit({
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token: process.env.access_token,
access_token_secret: process.env.access_token_secret,
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
strictSSL: true, // optional - requires SSL certificates to be valid.
});
let d = new Date();
let currentTime = (d.getTime() / 1000);
let yesterdayTime = ((currentTime - currentTime % SECONDS_IN_DAY) - SECONDS_IN_DAY) + "";
let options = {
uri: process.env.URL,
method: "POST",
json: {
"jsonrpc":"2.0",
"method":"icx_call",
"params":{
"to": process.env.SCORE,
"dataType":"call",
"data":{
"method":"getStoryOfDate",
"params":{
"date": yesterdayTime
}
}
},
"id":5
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
let count = Object.keys(body.result.story).length;
let s = "";
for (let i = 0; i < count; i++) {
s += unescape(body.result.story[i]) + " ";
}
if (s === "") {
console.log(d.getUTCFullYear() + "-" + d.getUTCMonth() + "-" + d.getUTCDate() + ": NO ONE POSTED!!" );
return;
}
let newDate = new Date(yesterdayTime * 1000);
let newURL = "https://www.iconstory.online/en/" + newDate.getUTCFullYear() + ("0" +(newDate.getUTCMonth() + 1)).slice(-2) + ("0" + newDate.getUTCDate()).slice(-2);
console.log(newURL); // Print the shortened url.
s += "\n" + newURL;
postTweet(s);
}
});
function postTweet(s) {
T.post('statuses/update', { status: s }, function(err, data, response) {
console.log(data);
});
}