-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceInfo.js
55 lines (48 loc) · 1.54 KB
/
serviceInfo.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
const request = require('request');
const moment = require('moment');
const debug = require('debug')('serviceInfo');
const info = require('./info.json');
const creds = info.credentials;
const apiEP = info.Endpoint;
const faveService = info.FaveServices[1];
// exports.getDayObject = (date, month, year) => {
// if (parseInt(date, 10) < 10) date = `0${parseInt(day, 10)}`;
// if (parseInt(month, 10) < 10) month = `0${parseInt(month, 10)}`;
// const dayObj = {
// date,
// month,
// year,
// }
// return dayObj;
// };
exports.getTodayObject = () => exports.getDayObject(Date.now());
exports.getDayObject = date => moment(date).format('YYYY/MM/DD');
const getService = (service, date = Date.now()) => new Promise((resolve, reject) => {
const d = exports.getDayObject(date);
const address = `https://${apiEP}/json/service/${service.serviceUid}/${d}`;
request.get(address, (err, response, textBody) => {
if (err) {
debug(err.message);
reject(err);
}
const body = JSON.parse(textBody);
// debug(JSON.parse(body));
if (body.error) {
debug(body.error);
if (body.error === 'No schedule found') {
// getService(service, date.setTime(date.getTime() + 1 * 86400000));
resolve(getService(service, date + 1 * 86400000));
} else {
reject(body.error);
}
} else {
resolve(body);
}
}).auth(creds.Username, creds.Password, true);
});
exports.getServiceAll = (service, cb) => {
const promise = getService(service);
promise.then((body) => {
cb(body);
});
};