-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaday.js
91 lines (74 loc) · 2.38 KB
/
adaday.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
'use strict';
const Promise = require('bluebird');
const moment = require('moment-timezone');
const debugTime = require('debug')('AdADay:time_matching');
const culturePub = require('./culturepub.js').create();
const slackPublisher = require('./slack_publisher.js').create();
const configReader = require('./config_reader.js').create('config.json');
const adADay = function adADay() {
moment.tz.setDefault(configReader.get('app:time_zone'));
const isDayMatch = function isTimeToPublish() {
const DAY_IDS = {
'Monday': 1,
'Tuesday': 2,
'Wednesday': 3,
'Thursday': 4,
'Friday': 5,
'Saturday': 6,
'Sunday': 7
};
const now = moment();
const publish_on = configReader.get('app:publish_on');
debugTime(`Target days are ${publish_on}`);
debugTime(`Current day is ${moment().format('dddd')}`);
if (publish_on === 'weekdays') {
const isAfterSunday = (now.isoWeekday() >= DAY_IDS['Monday']);
const isBeforeSaturday = (now.isoWeekday() <= DAY_IDS['Friday']);
if (isAfterSunday && isBeforeSaturday) {
debugTime('Weekday match');
} else {
debugTime('Not a weekday');
}
return isAfterSunday && isBeforeSaturday;
}
debugTime('Anyday match');
return true;
};
const isTimeMatch = function isTimeMatch() {
const now = moment();
const publish_at = moment(configReader.get('app:publish_at'), 'HH:mm');
const isHour = now.isSame(publish_at, 'hour');
const isMinute = now.isSame(publish_at, 'minute');
debugTime(`Target time is ${publish_at.format('HH:mm')}`);
debugTime(`Current time is ${moment().format('HH:mm')}`);
if (isHour && isMinute) {
debugTime('Time match');
} else {
debugTime('No time match');
}
return isHour && isMinute;
};
const isTimeToPublish = function isTimeToPublish() {
return isDayMatch() && isTimeMatch();
};
const getAd = Promise.method(function getAd() {
if (isTimeToPublish()) {
debugTime('Time to publish!');
return culturePub.getAd()
.then((ad_data) => slackPublisher.publishAd(ad_data));
}
debugTime('Not time yet');
return null;
});
const run = Promise.method(function run() {
const sleep_duration = 60000; // ms
return getAd()
.then(setInterval(getAd, sleep_duration));
});
return {
run: run
}
};
module.exports = {
create: adADay
};