-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.tsx
433 lines (343 loc) · 10.9 KB
/
index.tsx
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import {Context, Hono} from 'hono';
import {serve} from '@hono/node-server';
import {serveStatic} from '@hono/node-server/serve-static';
import {BlankEnv, BlankInput} from 'hono/types';
import {html} from 'hono/html';
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 {gothamHandler} from './services/gotham-handler';
import {mwHandler} from './services/mw-handler';
import {nesnHandler} from './services/nesn-handler';
import {cbsHandler} from './services/cbs-handler';
import {cleanEntries, clearChannels, removeAllEntries, removeChannelStatus} from './services/shared-helpers';
import {appStatus} from './services/app-status';
import {SERVER_PORT} from './services/port';
import {useLinear} from './services/channels';
import { providers } from './services/providers';
import {version} from './package.json';
import { Layout } from './views/Layout';
import { Header } from './views/Header';
import { Main } from './views/Main';
import { Links } from './views/Links';
import { Style } from './views/Style';
import { Providers } from './views/Providers';
import { Script } from './views/Script';
import {Tools} from './views/Tools';
import {CBSSports} from './services/providers/cbs-sports/views';
import { MntWest } from './services/providers/mw/views';
import {Paramount} from './services/providers/paramount/views';
import {FloSports} from './services/providers/flosports/views';
import {MlbTv} from './services/providers/mlb/views';
import {FoxSports} from './services/providers/fox/views';
import {Nesn} from './services/providers/nesn/views';
import {B1G} from './services/providers/b1g/views';
import {NFL} from './services/providers/nfl/views';
import {ESPN} from './services/providers/espn/views';
import {ESPNPlus} from './services/providers/espn-plus/views';
import {Gotham} from './services/providers/gotham/views';
const notFound = (c: Context<BlankEnv, '', BlankInput>) => {
return c.text('404 not found', 404, {
'X-Tuner-Error': 'EPlusTV: Error getting content',
});
};
const shutDown = () => process.exit(0);
const getUri = (c: Context<BlankEnv, '', BlankInput>): string => {
if (process.env.BASE_URL) {
return process.env.BASE_URL;
}
const protocol = c.req.header('x-forwarded-proto') || 'http';
const host = c.req.header('host') || '';
return `${protocol}://${host}`;
};
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 gothamHandler.getSchedule();
await nesnHandler.getSchedule();
await cbsHandler.getSchedule();
console.log('=== Done getting events ===');
console.log('=== Building the schedule ===');
await cleanEntries();
await scheduleEntries();
console.log('=== Done building the schedule ===');
};
const app = new Hono();
app.use('/node_modules/*', serveStatic({root: './'}));
app.use('/favicon.ico', serveStatic({root: './'}));
app.route('/', providers);
app.get('/', async c => {
return c.html(
html`<!DOCTYPE html>${(
<Layout>
<Header />
<Main>
<Links baseUrl={getUri(c)} />
<Tools />
<Providers>
<ESPNPlus />
<NFL />
<MlbTv />
<FoxSports />
<CBSSports />
<ESPN />
<Paramount />
<Nesn />
<Gotham />
<B1G />
<FloSports />
<MntWest />
</Providers>
</Main>
<Style />
<Script />
</Layout>
)}`,
);
});
app.post('/rebuild-epg', async c => {
await removeAllEntries();
await schedule();
return c.html(<Tools />, 200, {
'HX-Trigger': `{"HXToast":{"type":"success","body":"Successfully rebuilt EPG"}}`,
});
});
app.post('/reset-channels', async c => {
clearChannels();
return c.html(<Tools />, 200, {
'HX-Trigger': `{"HXToast":{"type":"success","body":"Successfully cleared channels"}}`,
});
});
app.get('/channels.m3u', async c => {
const m3uFile = await generateM3u(getUri(c));
if (!m3uFile) {
return notFound(c);
}
return c.body(m3uFile, 200, {
'Content-Type': 'application/x-mpegurl',
});
});
app.get('/linear-channels.m3u', async c => {
if (!useLinear) {
return notFound(c);
}
const m3uFile = await generateM3u(getUri(c), true);
if (!m3uFile) {
return notFound(c);
}
return c.body(m3uFile, 200, {
'Content-Type': 'application/x-mpegurl',
});
});
app.get('/xmltv.xml', async c => {
const xmlFile = await generateXml();
if (!xmlFile) {
return notFound(c);
}
return c.body(xmlFile, 200, {
'Content-Type': 'application/xml',
});
});
app.get('/linear-xmltv.xml', async c => {
if (!useLinear) {
return notFound(c);
}
const xmlFile = await generateXml(true);
if (!xmlFile) {
return notFound(c);
}
return c.body(xmlFile, 200, {
'Content-Type': 'application/xml',
});
});
app.get('/channels/:id{.+\\.m3u8$}', async c => {
const id = c.req.param('id').split('.m3u8')[0];
let contents: string | undefined;
// Channel data needs initial object
if (!appStatus.channels[id]) {
appStatus.channels[id] = {};
}
const uri = getUri(c);
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.`,
);
removeChannelStatus(id);
return notFound(c);
}
appStatus.channels[id].heartbeat = new Date();
return c.body(contents, 200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/vnd.apple.mpegurl',
});
});
app.get('/chunklist/:id/:chunklistid{.+\\.m3u8$}', async c => {
const id = c.req.param('id');
const chunklistid = c.req.param('chunklistid').split('.m3u8')[0];
let contents: string | undefined;
if (!appStatus.channels[id]?.player?.playlist) {
return notFound(c);
}
try {
contents = await appStatus.channels[id].player.cacheChunklist(chunklistid);
} catch (e) {}
if (!contents) {
console.log(`Could not get chunklist for channel #${id}.`);
removeChannelStatus(id);
return notFound(c);
}
appStatus.channels[id].heartbeat = new Date();
return c.body(contents, 200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/vnd.apple.mpegurl',
});
});
app.get('/channels/:id/:part{.+\\.key$}', async c => {
const id = c.req.param('id');
const part = c.req.param('part').split('.key')[0];
let contents: ArrayBuffer | undefined;
try {
contents = await appStatus.channels[id].player?.getSegmentOrKey(part);
} catch (e) {
return notFound(c);
}
if (!contents) {
return notFound(c);
}
appStatus.channels[id].heartbeat = new Date();
return c.body(contents, 200, {
'Cache-Control': 'no-cache',
'Content-Type': 'application/octet-stream',
});
});
app.get('/channels/:id/:part{.+\\.ts$}', async c => {
const id = c.req.param('id');
const part = c.req.param('part').split('.ts')[0];
let contents: ArrayBuffer | undefined;
try {
contents = await appStatus.channels[id].player?.getSegmentOrKey(part);
} catch (e) {
return notFound(c);
}
if (!contents) {
return notFound(c);
}
return c.body(contents, 200, {
'Cache-Control': 'no-cache',
'Content-Type': 'video/MP2T',
});
});
app.get('/channels/:id/:part{.+\\.m4i$}', async c => {
const id = c.req.param('id');
const part = c.req.param('part').split('.m4i')[0];
let contents: ArrayBuffer | undefined;
try {
contents = await appStatus.channels[id].player?.getSegmentOrKey(part);
} catch (e) {
return notFound(c);
}
if (!contents) {
return notFound(c);
}
return c.body(contents, 200, {
'Cache-Control': 'no-cache',
'Content-Type': 'video/MP2T',
});
});
// 404 Handler
app.notFound(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 gothamHandler.initialize();
await gothamHandler.refreshTokens();
await nesnHandler.initialize();
await nesnHandler.refreshTokens();
await cbsHandler.initialize();
await cbsHandler.refreshTokens();
await mwHandler.initialize();
serve(
{
fetch: app.fetch,
port: SERVER_PORT,
},
() => {
console.log(`Server started on port ${SERVER_PORT}`);
schedule();
},
);
})();
// 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 gothamHandler.refreshTokens();
await nesnHandler.refreshTokens();
await cbsHandler.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);