forked from buidl-labs/churrofi-twitter-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
244 lines (222 loc) · 6.6 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const nodeHTMLtoImage = require("node-html-to-image");
const fs = require("fs");
const fetchMarketCapAndPrice = require("./lib/data/celo-market-details");
const fetchTopEarningVGs = require("./lib/data/top-earning-vg");
const fetchTotalRewardsAndTop3Holders = require("./lib/data/user-rewards");
const { makeAddress, makeAmount } = require("./lib/utils");
require("dotenv").config();
const Twit = require("twit");
const formatter = new Intl.NumberFormat("en-US");
(async function main() {
/*
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
*/
const day = new Date().getDay();
const T = 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,
});
if (day == 2) {
// Tuesday
const { market_cap } = await fetchMarketCapAndPrice();
await generateMarketCapImage(market_cap);
await postImage(T, "✨ Celo's market cap ✨", "market-cap.png");
} else if (day == 3) {
// Wednesday
const { price } = await fetchMarketCapAndPrice();
const vg = await fetchTopEarningVGs();
await generateTop3VG(vg, price);
await postImage(
T,
"Top 3 earning groups on the Celo network in the past week 🤑",
"top-3-vg.png"
);
} else if (day == 4) {
// Thursday
const { top3 } = await fetchTotalRewardsAndTop3Holders();
await generateTop3CH(top3);
await postImage(
T,
"Top 3 @ChurroFi users with the highest earnings last week 🏅 \nStart voting on @CeloOrg through churrofi.app 🎯",
"top-3-ch.png"
);
} else if (day == 5) {
// Friday
const { price } = await fetchMarketCapAndPrice();
const { totalReward } = await fetchTotalRewardsAndTop3Holders();
await generateTotalRewardImage(totalReward, price);
await postImage(
T,
"💰 Total rewards earned by users through ChurroFi 💰",
"total-reward.png"
);
}
// const { totalReward, top3 } = await fetchTotalRewardsAndTop3Holders();
// const vg = await fetchTopEarningVGs();
// console.log("All data fetched, starting image generation.");
// await generateTotalRewardImage(totalReward, price);
// await generateTop3CH(top3);
// await generateTop3VG(vg, price);
// console.log("All images generated, start tweeting.");
// const T = 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,
// });
// await postImage(
// T,
// "Total rewards earned by users through ChurroFi",
// "total-reward.png"
// );
// await postImage(T, "Celo's market cap", "market-cap.png");
// await postImage(
// T,
// "Top 3 Celo users who've made the most yield through ChurroFi in the past week.",
// "top-3-ch.png"
// );
// await postImage(
// T,
// "Top 3 earning groups on the Celo network in the past week.",
// "top-3-vg.png"
// );
})();
async function postImage(T, message, image) {
var b64content = fs.readFileSync(image, { encoding: "base64" });
// first we must post the media to Twitter
T.post(
"media/upload",
{ media_data: b64content },
function (err, data, response) {
// now we can assign alt text to the media, for use by screen readers and
// other text-based presentations and interpreters
var mediaIdStr = data.media_id_string;
var altText = message;
var meta_params = { media_id: mediaIdStr, alt_text: { text: altText } };
T.post(
"media/metadata/create",
meta_params,
function (err, data, response) {
if (!err) {
// now we can reference the media and post a tweet (media will attach to the tweet)
var params = {
status: message,
media_ids: [mediaIdStr],
};
T.post("statuses/update", params, function (err, data, response) {
if (err) {
console.log(err);
} else {
console.log("Tweeted successfully.");
}
});
}
}
);
}
);
}
async function generateMarketCapImage(market_cap) {
const html = fs.readFileSync("./templates/market-cap.html").toString();
await nodeHTMLtoImage({
output: "./market-cap.png",
html,
selector: "#market-cap",
waitUntil: "networkidle2",
puppeteerArgs: {
defaultViewport: {
width: 1000,
height: 675,
},
},
content: { cap: formatter.format(market_cap) },
});
}
async function generateTotalRewardImage(totalReward, price) {
const html = fs.readFileSync("./templates/total-reward.html").toString();
await nodeHTMLtoImage({
output: "./total-reward.png",
html,
selector: "#total-reward",
waitUntil: "networkidle2",
puppeteerArgs: {
defaultViewport: {
width: 1000,
height: 675,
},
},
content: {
reward: formatter.format(totalReward.div(1e18).times(price).toNumber()),
},
});
}
async function generateTop3CH(top3) {
const html = fs.readFileSync("./templates/top-3-ch.html").toString();
await nodeHTMLtoImage({
output: "./top-3-ch.png",
html,
selector: "#top-3",
waitUntil: "networkidle2",
puppeteerArgs: {
defaultViewport: {
width: 1000,
height: 675,
},
},
content: {
first: {
address: makeAddress(top3[0].address),
amount: makeAmount(top3[0].totalReward),
},
second: {
address: makeAddress(top3[1].address),
amount: makeAmount(top3[1].totalReward),
},
third: {
address: makeAddress(top3[2].address),
amount: makeAmount(top3[2].totalReward),
},
},
});
}
async function generateTop3VG(vg, price) {
const html = fs.readFileSync("./templates/top-3-vg.html").toString();
await nodeHTMLtoImage({
output: "./top-3-vg.png",
html,
selector: "#top-vg",
waitUntil: "networkidle2",
puppeteerArgs: {
defaultViewport: {
width: 1000,
height: 675,
},
},
content: {
first: {
name: vg[0].name,
amount: formatter.format(
vg[0].reward.div(1e18).times(price).toFixed(0)
),
},
second: {
name: vg[1].name,
amount: formatter.format(
vg[1].reward.div(1e18).times(price).toFixed(1)
),
},
third: {
name: vg[2].name,
amount: formatter.format(
vg[2].reward.div(1e18).times(price).toFixed(1)
),
},
},
});
}