forked from m0ngr31/EPlusTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
215 lines (171 loc) · 4.97 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
import express from 'express';
import fs from 'fs';
import path from 'path';
import _ from 'lodash';
import kill from 'tree-kill';
import { generateM3u } from './services/generate-m3u';
import { cleanupParts } from './services/clean-parts';
import { slateStream } from './services/stream-slate';
import { initDirectories, tmpPath } from './services/init-directories';
import { generateXml } from './services/generate-xmltv';
import { checkNextStream, launchChannel } from './services/launch-channel';
import { getEventSchedules } from './services/get-events';
import { scheduleEntries } from './services/build-schedule';
const NUM_OF_CHANNELS: number = 100;
if (!process.env.ESPN_USER || !process.env.ESPN_PASS) {
console.log('Username and password need to be set!');
process.exit();
}
let START_CHANNEL = _.toNumber(process.env.START_CHANNEL);
if (_.isNaN(START_CHANNEL)) {
START_CHANNEL = 1;
}
let ACCESS_URL = process.env.ACCESS_URI;
while (ACCESS_URL.endsWith('/')) {
ACCESS_URL = ACCESS_URL.slice(0, -1);
}
interface IChannelStatus {
heartbeat: number;
pid?: any;
current?: string;
nextUp?: string;
nextUpTimer?: any;
}
interface IAppStatus {
channels: {
[string: number]: IChannelStatus
}
}
const appStatus: IAppStatus = {
channels: {}
};
const notFound = (_req, res) => res.status(404).send('404 not found');
const shutDown = async () => {
_.forOwn(appStatus.channels, (val, key) => {
val.pid && kill(val.pid);
});
process.exit(0);
};
const schedule = async () => {
console.log('=== Getting events ===');
await getEventSchedules();
console.log('=== Done getting events ===');
console.log('=== Building the schedule ===');
await scheduleEntries(START_CHANNEL);
console.log('=== Done building the schedule ===');
};
const app = express();
app.get('/channels.m3u', (req, res) => {
const m3uFile = generateM3u(NUM_OF_CHANNELS, ACCESS_URL, START_CHANNEL);
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(NUM_OF_CHANNELS, START_CHANNEL);
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;
const fileStr = `${id}/${id}.m3u8`;
const filename = path.join(tmpPath, fileStr);
let contents = null;
// Channel heatbeat
if (!appStatus.channels[id]) {
appStatus.channels[id] = {};
}
appStatus.channels[id].heartbeat = new Date().valueOf();
if (!fs.existsSync(filename)) {
contents = slateStream.getSlate('soon', ACCESS_URL);
// Start stream
launchChannel(id, appStatus, ACCESS_URL);
}
if (!contents) {
try {
contents = fs.readFileSync(filename);
} catch (e) {
console.log(`There was an error reading Channel #${id}'s m3u8...`);
notFound(req, res);
return;
}
}
checkNextStream(id, appStatus, ACCESS_URL);
res.writeHead(200, {
'Content-Type': 'application/vnd.apple.mpegurl',
'Cache-Control': 'no-cache',
});
res.end(contents, 'utf-8');
});
app.get('/channels/:id/:part.ts', (req, res) => {
const {id, part} = req.params;
let fileStr;
let filename;
const isSlate = id === 'starting' || id === 'soon';
if (isSlate) {
fileStr = `slate/${id}/${part}.ts`;
filename = path.join(process.cwd(), fileStr);
} else {
fileStr = `${id}/${part}.ts`;
filename = path.join(tmpPath, fileStr);
}
if (!fs.existsSync(filename)) {
console.log('Error opening part: ', filename);
notFound(req, res);
return;
}
res.writeHead(200, {
'Content-Type': 'video/MP2T',
'Cache-Control': 'no-cache',
});
const stream = fs.createReadStream(filename);
stream.pipe(res);
});
// 404 Handler
app.use(notFound);
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);
(async () => {
initDirectories(NUM_OF_CHANNELS, START_CHANNEL);
await schedule();
console.log('=== Starting Server ===')
app.listen(8000, () => console.log('Server started on port 8000'));
})();
// Cleanup intervals
setInterval(() => {
// Delete old TS files after 60 seconds
cleanupParts();
// Check for channel heartbeat and kill any streams that aren't being used
const now = new Date().valueOf();
_.forOwn(appStatus.channels, (val, key) => {
if (now - val.heartbeat > 60 * 1000) {
if (val.pid) {
console.log('Killing unwatched stream with PID: ', val.pid);
try {
kill(val.pid);
val.pid = null;
val.current = null;
} catch (e) {}
}
val.nextUp && val.nextUpTimer && clearTimeout(val.nextUpTimer);
val.nextUp = null;
val.nextUpTimer = null;
val.heartbeat = null;
}
});
}, 60 * 1000);
// Check for events every 4 hours and set the schedule
setInterval(async () => {
await schedule();
}, 1000 * 60 * 60 * 4);