forked from m0ngr31/EPlusTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
234 lines (185 loc) · 5.59 KB
/
index.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import express from 'express';
import moment from 'moment';
import {generateM3u} from './services/generate-m3u';
import {initDirectories} from './services/init-directories';
import {generateXml} from './services/generate-xmltv';
import {launchChannel} from './services/launch-channel';
import {scheduleEntries} from './services/build-schedule';
import {espnHandler} from './services/espn-handler';
import {foxHandler} from './services/fox-handler';
import {mlbHandler} from './services/mlb-handler';
import {cleanEntries, removeChannelStatus} from './services/shared-helpers';
import {appStatus} from './services/app-status';
import {version} from './package.json';
const notFound = (_req, res) => res.status(404).send('404 not found');
const shutDown = () => process.exit(0);
const schedule = async (firstRun = true) => {
console.log('=== Getting events ===');
await espnHandler.getSchedule();
await foxHandler.getSchedule();
await mlbHandler.getSchedule();
console.log('=== Done getting events ===');
await cleanEntries();
console.log('=== Building the schedule ===');
await scheduleEntries(firstRun);
console.log('=== Done building the schedule ===');
};
const app = express();
app.get('/channels.m3u', (req, res) => {
const m3uFile = generateM3u(`${req.protocol}://${req.headers.host}`);
if (!m3uFile) {
notFound(req, res);
return;
}
res.writeHead(200, {
'Content-Type': 'application/x-mpegurl',
});
res.end(m3uFile, 'utf-8');
});
app.get('/xmltv.xml', async (req, res) => {
const xmlFile = await generateXml();
if (!xmlFile) {
notFound(req, res);
return;
}
res.writeHead(200, {
'Content-Type': 'application/xml',
});
res.end(xmlFile, 'utf-8');
});
app.get('/channels/:id.m3u8', async (req, res) => {
const {id} = req.params;
let contents = null;
// Channel data needs initial object
if (!appStatus.channels[id]) {
appStatus.channels[id] = {
heartbeat: new Date(),
};
}
const uri = `${req.protocol}://${req.headers.host}`;
if (!appStatus.channels[id].player?.playlist) {
try {
await launchChannel(id, uri);
} catch (e) {}
}
try {
contents = appStatus.channels[id].player?.playlist;
} catch (e) {}
if (!contents) {
console.log(
`Could not get a playlist for channel #${id}. Please make sure there is an event scheduled and you have access to it.`,
);
notFound(req, res);
return;
}
appStatus.channels[id].heartbeat = new Date();
res.writeHead(200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/vnd.apple.mpegurl',
});
res.end(contents, 'utf-8');
});
app.get('/chunklist/:id/:chunklistid.m3u8', async (req, res) => {
const {id, chunklistid} = req.params;
let contents = null;
if (!appStatus.channels[id]?.player?.playlist) {
notFound(req, res);
return;
}
try {
contents = await appStatus.channels[id].player.cacheChunklist(chunklistid);
} catch (e) {}
if (!contents) {
console.log(`Could not get chunklist for channel #${id}.`);
notFound(req, res);
return;
}
appStatus.channels[id].heartbeat = new Date();
res.writeHead(200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/vnd.apple.mpegurl',
});
res.end(contents, 'utf-8');
});
app.get('/channels/:id/:part.key', async (req, res) => {
const {id, part} = req.params;
let contents;
try {
contents = await appStatus.channels[id].player.getSegmentOrKey(part);
} catch (e) {
notFound(req, res);
return;
}
if (!contents) {
notFound(req, res);
return;
}
appStatus.channels[id].heartbeat = new Date();
res.writeHead(200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/octet-stream',
});
res.end(contents, 'utf-8');
});
app.get('/channels/:id/:part.ts', async (req, res) => {
const {id, part} = req.params;
let contents;
try {
contents = await appStatus.channels[id].player.getSegmentOrKey(part);
} catch (e) {
notFound(req, res);
return;
}
if (!contents) {
notFound(req, res);
return;
}
res.writeHead(200, {
'Cache-Control': 'no-cache',
'Content-Type': 'video/MP2T',
});
res.end(contents, 'utf-8');
});
// 404 Handler
app.use(notFound);
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);
(async () => {
console.log(`=== E+TV v${version} starting ===`);
initDirectories();
await espnHandler.initialize();
await espnHandler.refreshTokens();
await foxHandler.initialize();
await foxHandler.refreshTokens();
await mlbHandler.initialize();
await mlbHandler.refreshTokens();
await schedule();
console.log('=== Starting Server ===');
app.listen(8000, () => console.log('Server started on port 8000'));
})();
// Check for events every 4 hours and set the schedule
setInterval(async () => {
await schedule(false);
}, 1000 * 60 * 60 * 4);
// Check for updated refresh tokens 30 minutes
setInterval(async () => {
await espnHandler.refreshTokens();
await foxHandler.refreshTokens();
await mlbHandler.refreshTokens();
}, 1000 * 60 * 30);
// Remove idle playlists
setInterval(() => {
const now = moment();
for (const key of Object.keys(appStatus.channels)) {
if (appStatus.channels[key] && appStatus.channels[key].heartbeat) {
const channelHeartbeat = moment(appStatus.channels[key].heartbeat);
if (now.diff(channelHeartbeat, 'minutes') > 5) {
console.log(`Channel #${key} has been idle for more than 5 minutes. Removing playlist info.`);
removeChannelStatus(key);
}
} else {
console.log(`Channel #${key} was setup improperly... Removing.`);
removeChannelStatus(key);
}
}
}, 1000 * 60);