-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
162 lines (141 loc) Β· 4.17 KB
/
server.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
/* Setting things up. */
var path = require('path'),
express = require('express'),
app = express(),
Twit = require('twit'),
config = {
/* Be sure to update the .env file with your API keys. See how to get them: https://botwiki.org/tutorials/how-to-create-a-twitter-app */
twitter: {
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
}
},
T = new Twit(config.twitter);
var clock_face = {
'0000': 'π',
'0030': 'π§',
'0100': 'π',
'0130': 'π',
'0200': 'π',
'0230': 'π',
'0300': 'π',
'0330': 'π',
'0400': 'π',
'0430': 'π',
'0500': 'π',
'0530': 'π ',
'0600': 'π',
'0630': 'π‘',
'0700': 'π',
'0730': 'π’',
'0800': 'π',
'0830': 'π£',
'0900': 'π',
'0930': 'π€',
'1000': 'π',
'1030': 'π₯',
'1100': 'π',
'1130': 'π¦',
'1200': 'π',
'1230': 'π§',
}
var digits = [
'0οΈβ£',
'1οΈβ£',
'2οΈβ£',
'3οΈβ£',
'4οΈβ£',
'5οΈβ£',
'6οΈβ£',
'7οΈβ£',
'8οΈβ£',
'9οΈβ£'
]
var calendar_icon = 'π';
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength,padString) {
targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength-this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0,targetLength) + String(this);
}
};
}
app.use(express.static('public'));
app.all("/post_time", function(req, res) {
var now = new Date();
T.post('statuses/update', { status: translate_time(now) }, function(err, data, response){
if(err) {
console.log('error!: ', err);
res.sendStatus(500);
}
else {
res.sendStatus(200);
}
});
console.log('post_time called: ' + translate_time(now) );
});
app.all("/post_date", function(req, res) {
var now = new Date();
T.post('statuses/update', { status: calendar_icon + ': ' + translate_date(now) }, function(err, data, response){
if(err) {
console.log('error!: ', err);
res.sendStatus(500);
}
else {
res.sendStatus(200);
}
});
console.log('post_date called: ' + calendar_icon + ': ' + translate_date(now) ) ;
});
var listener = app.listen(process.env.PORT, function () {
console.log('Your bot is running on port ' + listener.address().port + ' with timezone set to ' + process.env.TZ);
});
/*
Takes in a Date object and returns it as (string) 2οΈβ£0οΈβ£-1οΈβ£1οΈβ£-2οΈβ£0οΈβ£1οΈβ£9οΈβ£
*/
function translate_date(in_date_object) {
var in_date = in_date_object.getDate() + ' - ' + ( in_date_object.getMonth() + 1 ) + ' - ' + in_date_object.getFullYear();
var out_date = [];
for (var i = 0; i < in_date.length; i++) {
var current_char = in_date[i];
switch(current_char) {
case ':': case ' ': case '-': case '/':
out_date[i] = current_char;
break;
default:
out_date[i] = digits[in_date[i]] ;
}
}
return out_date.join('');
}
/*
Takes in a Date object returns the nearest clock_face value
*/
function translate_time(in_date_object) {
var in_time = '';
var minutes = in_date_object.getMinutes();
var hours = String(in_date_object.getHours() % 12).padStart(2, '0');
if ( minutes <= 15) {
in_time = hours + '00';
}
else if ( minutes <= 45) {
in_time = hours + '30';
}
else {
in_time = String(in_date_object.getHours() % 12 + 1).padStart(2, '0') + '00';
}
console.log('in_time: ' + in_time);
return clock_face[in_time];
}