-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
359 lines (282 loc) · 10.9 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
`
./oso-
`oyyhhs++/.`
`ohddyo/:o+++::.` `.--.`
:hyo+/ooo+++++++/. `sdmmddy-
++o+/+ooyosoossos+``/++ossyys/` `-/syyyyy-
-sssyysos++ssysoyh+dNNNNNNNNNmd- `++++++/:.
+ssyhy/+ooos+ooyhyhNNNNNNNNNNNm` /oydddhs/
`oyyyyyysss+oossshyNNNNNNNNNNNm` -+shmNNmmmmd`.
-syyyhhhhs+++osyhhdMNNNNNNNNNd+/:+dmmNNNNNmmmmdsdy/
`+yyhhhhhoo+ooosyyyNNNNNNNNNNNNNNNNNNNNNNNmmmmdyddd:
-syhhhhhhoossosssdNNNNNNNNNNNNNNmNmmNNNNNmmmmNdyshhs+-`
oyhhhhhyso+yhhmhhMMNNNNNNNNNNNNNNNmNNNNNmNmmNmhhhdddhs+`
`yyhhhhyysyhhydhhyNMMNNNNMNNNNNNNNNNNNNNNmNmmmmdhdyydhys-
`yyhhhhdmdhdhhdhhyNMMMMMMMMMMNNNNNMNNNNNmmmmmmmmddhhyso+-
+yyhhmNNNmmNNNdosNMMMMMMMMMMNNNNNMNNNNNNmmmmmmmmhysoo+/.
`/ydmNNNNNNNNmdyhMMMMMMNNMMMMMNNMMNNNNNmmmmmmmmdyysso+-
`syysssydmNNmmshMMMMMMMMMMMMMNNNNNNNNNmmNNdydmhhyyso-
:o::////+oomNNdmMMMMMMMMMMMMNNNNMMNNNhhdmN+:/mdhhys:
`-----::::::::/+oyNMNNMMMMMMMMMMMMNNNMMMmsyo+/+ys:-/ysdhs.
`.----::/:/:::::+sNMMMMMMMMMNNNNNNNNNNNMNdyoo+/:/:--:/hh+
``.-:////::/::smMMMMMMMMNNNNNmsyNNNNNNNNmyoo//+oydmhy-
`-::::://///+s/::/hMNNNNNNNNNNNmyodNNNNNNNMNmmmNNNNNmh+
`---.``-::-:dmdhs++NNNNNmmNNNNNNmysNNNNNNNNMMNNNNNNNmy`
.::-`:mmmmmNNNMMNdsdhyssssyhsdmmNNNNNMMNNNNNNNm-
`-:.` :dmmmmNNNMMd/:/+sysys/::ymmmNNNNMMNNNNNNmh`
.hddmmmNNNNs:smmdhyhmmh+:hmmmNNNNNNNNNNNm+
oddmmNNNNo/dNmyhhyhhmNms:hNmNNNNNNNNNNNh`
.hdmmdhdo:syso:+/:/:ohdmo/hhmmNmmmmdmNm-
/dmy/++--.---:..-.::---::/+/ymmdddmNmo
`sh:+oo..----:-.--//:----oo+:ymhosyy.
.::+oh-.-/-/:-..-++-::-+yo+/:+.
`.oydmh:-:/y+:..+:yo/-:dhy+.`
.ymNNmy/:+ss+/yso+:+dy/.
`odmmmmmdhyhhsyyyo+.
:hdddmdddhyo/.
`/o+/-.`
*/
//*******************************************************************
'use strict';
//*******************************************************************
const numberstring = require('numberstring');
const async = require('async');
const express = require('express');
const exphbs = require('express-handlebars');
const request = require('request');
const AWS = require('aws-sdk');
const { TwitterApi } = require('twitter-api-v2');
require('dotenv').config();
//*******************************************************************
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1', // specify your AWS region
});
// const dynamoDB = new AWS.DynamoDB.DocumentClient();
const docClient = new AWS.DynamoDB.DocumentClient();
//*******************************************************************
const PORT = process.env.PORT || 12345;
//*******************************************************************
// console.log('Twitter API Key:', process.env.TWITTER_API_KEY);
// console.log('Twitter API Secret:', process.env.TWITTER_API_SECRET);
// console.log('Twitter Access Token:', process.env.TWITTER_ACCESS_TOKEN);
// console.log('Twitter Access Token Secret:', process.env.TWITTER_ACCESS_TOKEN_SECRET);
// Initialize Twitter API v2 client with OAuth 2.0 User Context authentication
const twitterClient = new TwitterApi({
appKey: process.env.TWITTER_API_KEY,
appSecret: process.env.TWITTER_API_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
//*******************************************************************
let current_record;
let current_number;
let current_string;
let current_comma;
let current_twext;
//*******************************************************************
(async () => {
try {
const params = {
TableName: 'voncountdown',
};
const data = await docClient.scan(params).promise();
console.log('data : ' + JSON.stringify(data) );
if (data.Items.length === 0) {
console.log('no items found in the table');
current_number = 1111373357579;
current_comma = numberstring.comma(current_number);
current_string = numberstring(current_number, { cap: 'title', punc: '!' });
// Insert a record if no items are found
const insertParams = {
TableName: 'voncountdown',
Item: {
number: current_number,
string: current_string,
datetime: new Date().toISOString(),
status: true,
},
};
await docClient.put(insertParams).promise();
console.log('inserted new record : ' + current_number);
} else {
const lowest_number = data.Items.sort((a, b) => a.number - b.number)[0];
console.log('lowest_number : ' + JSON.stringify(lowest_number));
current_number = lowest_number.number;
current_comma = numberstring.comma(current_number);
current_string = numberstring(parseInt(current_number), {'cap': 'title', 'punc': '!'});
console.log('load number : ' + current_number);
console.log('load string : ' + current_string );
console.log('load commma : ' + current_comma );
countdown();
}
} catch (error) {
console.error(error.message);
}
})();
//*******************************************************************
var random = function(min, max) {
var rand = Math.floor(Math.random() * (max - min + 1)) + min;
console.log('rand : ' + rand );
return rand;
}
//*******************************************************************
var short_phrase = [
'Ha ha ha!!',
'Ah ah ah!!',
'Ah ha ha!!',
'Ah ha ha ha!!',
'Don\'t forget to count!!',
'Wonderful!!',
'I love motion pictures!!',
'I love counting!!',
'Now, that was silly!!',
'Wouldn\'t you agree, my bats?',
'I love traditions!!',
'I will count them!!',
'There\'s always something to count!!',
'Don\'t count the days, make the days count!!',
'Werry good!!',
'Yeees!!',
'You know that I am called the Count!!',
'I really love to count!!',
'I could sit and count all day!!',
'Sometimes I get carried away!!',
'Yeees!!',
'I count slowly!!',
'Once I\'ve started counting it\'s really hard to stop!!',
'I could count forever!!',
'I love counting whatever the amount!!',
'When I\'m alone, I count myself!!',
'Greetings!!',
'Counting is fun!!',
'I vant to count your numbers!!',
'I love big numbers and I cannot lie!!',
'Sometimes I just count away!!',
'Numbers are useful!!',
'I love to count things!!'
];
var short_tags = [
'@CountVonCount',
'@CountVonCount',
'@CountVonCount',
'@SesameWorkshop',
'@sesamestreet',
'@BigBird',
'@OscarTheGrouch',
'@elmo',
'@MeCookieMonster',
'@Grover',
'@KermitTheFrog',
'@ollie',
'@brianfunk_',
'@brianfunk_',
'#sesamestreet',
'#numberstring',
'#numbers',
'#count',
'#counting',
'#ilovecounting',
'#iheartcounting',
'#ilovenumbers',
'#iheartnumbers',
'#CountessVonBackwards',
'#CountessvonDahling',
'#LadyTwo',
'#TheCountess',
'#CountVonCount',
'#countmobile',
'#itsthefinalcountdown',
'#countdown',
'#countupsidedown',
'#countingisfun',
'#ahhaha',
'#yeees'
];
//*******************************************************************
var countdown = function() {
console.log('countdown start ' );
current_number--;
current_string = numberstring(current_number, {'cap': 'title', 'punc': '!'});
current_comma = numberstring.comma(current_number);
console.log('updated number : ' + current_number );
console.log('updated string : ' + current_string );
console.log('updated comma : ' + current_comma );
async.series(
[
function(callback){
//*******************************************************************
console.log('tweetdown ' );
current_twext = current_string;
if ( random(0,4) === 4 ) {
var twext_phrase = short_phrase[random(0,short_phrase.length-1)];
var twext_tag = short_tags[random(0,short_tags.length-1)];
console.log('twext_phrase : ' + twext_phrase );
console.log('twext_tag : ' + twext_tag );
current_twext = current_comma +'! '+ twext_phrase + ' '+ twext_tag;
}
console.log('current_twext : ' + current_twext );
(async () => {
let tweet = await twitterClient.v2.tweet(current_twext);
console.log('tweet posted : ', tweet);
callback(null, current_twext);
})();
},
function(callback){
//*******************************************************************
console.log('update number');
(async () => {
// Insert a record with new number
const insertParams = {
TableName: 'voncountdown',
Item: {
number: current_number,
string: current_string,
datetime: new Date().toISOString(),
status: true,
},
};
await docClient.put(insertParams).promise();
console.log('inserted new record : ' + current_number);
callback();
})();
}
],
function(err, results){
var result_status = 'success';
if (err) {
result_status = 'error';
console.error('countdown series error : ' + JSON.stringify(err));
}
console.log('update results : ' + JSON.stringify(results) );
//*******************************************************************
var delay = random(1234567, 7654321);
console.log('delay : ' + delay );
console.log('delay hours : ' + delay / 3600000 );
setTimeout(function() {
countdown();
}, delay);
}
);
};
//*******************************************************************
var app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.get('/', function (req, res) {
res.render('home', {
current_number: current_number,
current_string: current_string,
current_comma: current_comma
});
});
app.get('/badge', function(req,res) {
var badge_url = 'https://img.shields.io/badge/Von%20Countdown-'+ encodeURIComponent(current_comma) +'-a26d9e.svg';
//console.log('badge_url : ' + badge_url );
request(badge_url).pipe(res);
});
app.listen(PORT);
//*******************************************************************