-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.ts
318 lines (253 loc) · 7.66 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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 {b1gHandler} from './services/b1g-handler';
import {floSportsHandler} from './services/flo-handler';
import {paramountHandler} from './services/paramount-handler';
import {nflHandler} from './services/nfl-handler';
import {msgHandler} from './services/msg-handler';
import {mwHandler} from './services/mw-handler';
import {nesnHandler} from './services/nesn-handler';
import {cleanEntries, removeChannelStatus} from './services/shared-helpers';
import {appStatus} from './services/app-status';
import {SERVER_PORT} from './services/port';
import {useLinear} from './services/channels';
import {version} from './package.json';
const notFound = (_req, res) => {
res.writeHead(404, {
'X-Tuner-Error': 'EPlusTV: Error getting content',
});
res.write('404 not found');
res.end();
};
const shutDown = () => process.exit(0);
const schedule = async () => {
console.log('=== Getting events ===');
await espnHandler.getSchedule();
await foxHandler.getSchedule();
await mlbHandler.getSchedule();
await b1gHandler.getSchedule();
await floSportsHandler.getSchedule();
await mwHandler.getSchedule();
await nflHandler.getSchedule();
await paramountHandler.getSchedule();
await msgHandler.getSchedule();
await nesnHandler.getSchedule();
console.log('=== Done getting events ===');
await cleanEntries();
console.log('=== Building the schedule ===');
await scheduleEntries();
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('/linear-channels.m3u', (req, res) => {
if (!useLinear) {
notFound(req, res);
return;
}
const m3uFile = generateM3u(`${req.protocol}://${req.headers.host}`, true);
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('/linear-xmltv.xml', async (req, res) => {
if (!useLinear) {
notFound(req, res);
return;
}
const xmlFile = await generateXml(true);
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] = {};
}
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 b1gHandler.initialize();
await b1gHandler.refreshTokens();
await floSportsHandler.initialize();
await floSportsHandler.refreshTokens();
await nflHandler.initialize();
await nflHandler.refreshTokens();
await paramountHandler.initialize();
await paramountHandler.refreshTokens();
await msgHandler.initialize();
await msgHandler.refreshTokens();
await nesnHandler.initialize();
await nesnHandler.refreshTokens();
await schedule();
console.log('=== Starting Server ===');
app.listen(SERVER_PORT, () => console.log(`Server started on port ${SERVER_PORT}`));
})();
// Check for events every 4 hours and set the schedule
setInterval(async () => {
await schedule();
}, 1000 * 60 * 60 * 4);
// Check for updated refresh tokens 30 minutes
setInterval(async () => {
await espnHandler.refreshTokens();
await foxHandler.refreshTokens();
await mlbHandler.refreshTokens();
await b1gHandler.refreshTokens();
await floSportsHandler.refreshTokens();
await nflHandler.refreshTokens();
await paramountHandler.refreshTokens();
await msgHandler.refreshTokens();
await nesnHandler.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);