-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializeTwitter.js
143 lines (123 loc) · 4.17 KB
/
initializeTwitter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var fs = require('fs');
var {HEBREW, ENGLISH} = require("./languages.js");
var sedra = require('./sedra.js');
var splitVerse = require('./splitVerse.js');
var Twit = require('twit');
var {DateTime} = require("luxon");
function requiredGet(obj, key) {
if (obj.hasOwnProperty(key)) {
return obj[key];
}
throw key + " not found in " + obj;
}
function loadSecrets() {
if (process.env.parshabot_use_secret_from_env) {
return process.env;
}
return JSON.parse(fs.readFileSync('secrets.json'))
}
function options() {
var secrets = loadSecrets();
var twitOptions = {}
// copy the options to make sure that they're all available, and nothing else is present
twitOptions["consumer_key"] = requiredGet(secrets, "consumer_key")
twitOptions["consumer_secret"] = requiredGet(secrets, "consumer_secret")
twitOptions["access_token"] = requiredGet(secrets, "access_token")
twitOptions["access_token_secret"] = requiredGet(secrets, "access_token_secret")
twitOptions["timeout_ms"] = 60*1000
twitOptions["strictSSL"] = true
return twitOptions;
}
var chainAppend = function(previous, text) {
if (Object.keys(previous).length === 0) {
previous.text = text;
return previous;
}
previous.next = {text: text};
previous = previous.next;
return previous;
};
var aliyaNumberToHebrew = {
1: "ראשון",
2: "שני",
3: "שלישי",
4: "רביעי",
5: "חמישי",
6: "שישי",
7: "שביעי",
}
var formatAliyaHeader = function(aliya, parsha) {
return parsha.hebrew + " | " + parsha.english + "\n" + aliyaNumberToHebrew[aliya.aliyaNumber];
}
var buildTweetChain = function(aliya, parsha, lang) {
var chain = {text: formatAliyaHeader(aliya, parsha)};
var previous = chain;
var hebrewVerses = [];
var englishVerses = [];
sedra.forEachVerse(aliya, HEBREW, verse => hebrewVerses.push(verse));
sedra.forEachVerse(aliya, ENGLISH, verse => englishVerses.push(verse));
for (var i = 0; i < hebrewVerses.length; i++) {
var hebrewSplits = splitVerse(hebrewVerses[i]);
var firstHebrew = chainAppend(previous, hebrewSplits[0]);
previous = firstHebrew;
for (var j = 1; j < hebrewSplits.length; j++) {
previous = chainAppend(previous, hebrewSplits[j]);
}
var englishSplits = splitVerse(englishVerses[i]);
var englishPrevious = firstHebrew.english = {};
for (var j = 0; j < englishSplits.length; j++) {
englishPrevious = chainAppend(englishPrevious, englishSplits[j]);
}
}
return chain;
}
// TODO: give this file a better name (perhaps twitterClient.js)
module.exports = function() {
twitterApi = new Twit(options());
var newTweet = function(tweet, lastTweetId) {
if (tweet === undefined) {
return;
}
var parameters = {status: tweet.text};
if (lastTweetId) {
parameters["in_reply_to_status_id"] = lastTweetId;
parameters["auto_populate_reply_metadata"] = true;
}
twitterApi.post('statuses/update', parameters, function(error, data, response) {
// TODO: confirm that this tweet succeeded. If not, exponential backoff
if (error) {
console.error(error);
} else {
console.log(data);
}
newTweet(tweet.next, data.id_str);
// Delay English tweets slightly so that Twitter creates a single thread for all of the Hebrew
setTimeout(() => {
newTweet(tweet.english, data.id_str);
}, 2000);
});
};
return {
ifAliyaHasNotBeenTweeted: function(aliya, parsha, callback) {
// A rough estimate for a recency of when to search. Should be more than enough
var twoWeeksAgo = DateTime.utc().minus({weeks: 2});
twitterApi.get(
"search/tweets",
{q: '"' + formatAliyaHeader(aliya, parsha) + '"'
+ " from:parshabot since:" + twoWeeksAgo.toFormat("yyyy-LL-dd")},
function(error, data, response) {
if (error) {
console.error(error);
} else if (data.statuses.length > 0) {
console.log("Already tweeted: " + data.statuses[0].text);
} else {
callback();
}
});
},
tweetAliya: function(aliya, parsha, lang) {
var tweetChain = buildTweetChain(aliya, parsha, lang);
newTweet(tweetChain);
},
};
}