-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
59 lines (50 loc) · 1.42 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
var request = require('request');
var schedule = require('node-schedule');
var env = require('node-env-file');
// Load enviroment settings
env(__dirname + '/.env');
// Run once per day
var cronSchedule = { hour: 0, minute: 0 };
if(process.env.DEBUG) {
// Run every minute
cronSchedule = '*/1 * * * *';
}
// Schedule the job
var j = schedule.scheduleJob(cronSchedule, updateAppGlanceSlice);
// Send AppGlanceSlice to the REST API
function updateAppGlanceSlice() {
console.log('Sending AppGlanceSlice...');
var dateToday = new Date();
var dateTomorrow = new Date();
dateTomorrow.setDate(dateTomorrow.getDate() + 1);
// Construct JSON containing our AppGlanceSlice
var data = {
"slices": [
{
"layout": {
"icon": "system://images/TIMELINE_BASEBALL",
"subtitleTemplateString": dateToday.toUTCString()
},
"expirationTime": dateTomorrow.toISOString()
}
]
};
// Send a PUT request to the REST endpoint
request({
url: process.env.REST_ENDPOINT,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-User-Token': process.env.REST_API_KEY
},
json: data
}, function(error, response, body){
if(error) {
console.log('FAILED sending AppGlanceSlice...');
console.log(error);
} else {
console.log('SUCCESS sending AppGlanceSlice...');
console.log(response.statusCode, body);
}
});
}