-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinitron.js
83 lines (78 loc) · 3.27 KB
/
spinitron.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
let spinitron = require('spinitron-spinpapi');
let moment = require('moment');
spinitron = new spinitron({
station: process.env.SPINSTATION, /* optional */
userid: process.env.SPINID,
secret: process.env.SPINSECRET
});
/* gets the ON AIR playlist. */
let getCurrentPlaylist = function(show) {
return new Promise((resolve, reject) => {
spinitron.getCurrentPlaylist(function(error, response) {
if (error) {
reject(error);
} else if (!response.success){
reject("Spinitron request was unsuccessful.");
} else {
spinitron.getPlaylistInfo({'PlaylistID': response.results}, function(error, response) {
if (error) {
reject(error);
} else if (!response.success){
reject("Spinitron request was unsuccessful.");
} else if (response.results['ShowID'] != show['ShowID']) {
reject("The current show does not own the current playlist");
} else {
resolve(response.results)
}
})
}
});
});
}
/* gets the information of the show that is SCHEDULED to start at the top of the next hour */
let getUpcomingShowInfo = function() {
return new Promise((resolve, reject) => {
let upcomingHour = moment().add(1, 'hour').format('HH');
/* upcomingHour the next hour -> "00", "01" ... "23" */
spinitron.getRegularShowsInfo({
'When': 'today',
'StartHour': '0',
}, function(error, response) {
if (error) {
reject(error);
} else if (!response.success){
reject("Spinitron request was unsuccessful.");
} else {
let shows = response.results;
/* filter today's shows, find only the one
scheduled to start at the upcoming hour.
will be empty if no shows are scheduled
for the upcoming hour. */
shows = shows
.filter( show => moment(show['OnairTime'], 'HH:m:s')
.format('HH') == upcomingHour);
if (shows.length == 0) {
reject("No show scheduled for upcoming hour.")
} else {
let upcomingShow = shows[0];
/* log show ID and show duration in hours */
let diff = moment(upcomingShow['OffairTime'], 'HH:m:s').diff(
moment(upcomingShow['OnairTime'], 'HH:m:s'),
'hours'
);
if (diff < 0) { /* show went into new day */
/* thank u javascript for not resolving
-21 % 24 as 3. */
diff = ((diff%24)+24)%24;
}
upcomingShow.duration = diff;
resolve(upcomingShow);
}
}
});
});
}
module.exports = {
getUpcomingShowInfo : getUpcomingShowInfo,
getCurrentPlaylist : getCurrentPlaylist
};