-
Notifications
You must be signed in to change notification settings - Fork 7
/
m3u-proxy.js
218 lines (199 loc) · 7.2 KB
/
m3u-proxy.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
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const get = require('simple-get');
const byline = require('byline');
const flow = require('xml-flow');
const debug = require('debug')('m3u-proxy');
// Definitions for command line araguments
const definitions = [
{ name: 'config', alias: 'c', type: String, defaultValue: './config.json' }
];
const cmdLineArgs = require('command-line-args');
// const { resolve } = require('path');
// Set passed arguments
const args = cmdLineArgs(definitions);
const config = require(args.config);
const getFile = (url, filename) => {
debug(` ┌getFile: ${filename}`);
return new Promise(resolve => {
// Prepare destination
const dirname = path.dirname(filename);
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname, { recursive: true });
const file = fs.createWriteStream(filename + '.tmp');
// and download
get({ url: url, rejectUnauthorized: false }, (error, response) => {
if (error) {
fs.unlinkSync(filename + '.tmp');
// reject(new Error(`Failed to load resource: ${url}`));
throw error;
}
// pipe received data
response.pipe(file);
// and close
response.on('end', () => {
if (fs.existsSync(filename)) fs.unlinkSync(filename);
fs.renameSync(filename + '.tmp', filename);
debug(` └getFile: ${filename}`);
resolve();
});
});
// resolve();
});
};
const M3UFilePrefix = /^#EXTM3U/;
const M3UPrefix = /^#EXTINF/;
const M3UFields = /^#EXTINF:-?\d+,?(?: *?([\w-]*)="(.*?)")?(?: *?([\w-]*)="(.*?)")?(?: *?([\w-]*)="(.*?)")?(?: *?([\w-]*)="(.*?)")?(?: *?([\w-]*)="(.*?)")?.*,(.*)/;
const processM3U = (source, model) => {
debug(` ┌M3U-Process: ${source.name}${model.name}`);
return new Promise(resolve => {
// Preparation
if (model.filters) {
for (let i = 0; i < model.filters.length; i++) model.filters[i].regex = new RegExp(model.filters[i].regex, 'i');
}
if (model.transformations) {
for (let i = 0; i < model.transformations.length; i++) model.transformations[i].regex = new RegExp(model.transformations[i].regex, 'i');
}
// Loop
const stream = byline.createStream(fs.createReadStream(`${config.importFolder}/${source.name}.m3u`, { encoding: 'utf8' }));
const streams = [];
let fields = {};
stream.on('data', (line) => {
// byline skips empty lines
if (line.match(M3UFilePrefix)) {
// First line
} else if (line.match(M3UPrefix)) {
// We get fields
const matches = line.match(M3UFields);
// if (!matches) {
// }
try {
for (let i = 1; i < 8; i += 2) {
if (matches[i]) fields[matches[i]] = matches[i + 1];
}
if (!fields['tvg-name']) fields['tvg-name'] = matches[11].trim();
if (!fields['group-title']) fields['group-title'] = fields['tvg-name'].match(/\w*/); // Compact M3U files = no group-title
} catch (err) {
console.error(line);
}
} else {
// And stream URL
fields['stream'] = line;
// Now let's check filters
let valid;
if (!model.filters) {
valid = true;
} else {
valid = false;
for (let i = 0; i < model.filters.length; i++) {
if (model.filters[i].regex.test(fields[model.filters[i].field])) {
valid = true;
break;
}
}
}
// Do we need to apply transformations?
if (valid && model.transformations) {
for (let i = 0; i < model.transformations.length; i++) {
fields[model.transformations[i].field] = fields[model.transformations[i].field].replace(model.transformations[i].regex, model.transformations[i].substitution);
}
}
if (valid) streams.push(fields);
fields = {};
}
});
stream.on('end', () => {
debug(` └M3U-Process: ${source.name}${model.name}`);
resolve(streams);
});
});
};
const exportM3U = (source, model, streams) => {
debug(` ┌M3U-Write: ${source.name}${model.name}`);
return new Promise(resolve => {
// Prepare destination
if (!fs.existsSync(`${config.exportFolder}`)) fs.mkdirSync(`${config.exportFolder}`, { recursive: true });
const file = fs.createWriteStream(`${config.exportFolder}/${source.name}${model.name}.m3u`);
// And export
file.write('#EXTM3U\n');
streams.forEach(stream => {
file.write(`#EXTINF:-1`);
if (stream['tvg-id']) file.write(` tvg-id="${stream['tvg-id']}"`);
if (stream['tvg-name']) file.write(` tvg-name="${stream['tvg-name']}"`);
if (stream['tvg-logo']) file.write(` tvg-logo="${stream['tvg-logo']}"`);
file.write(` group-title="${stream['group-title']}",${stream['tvg-name']}\n`);
file.write(`${stream['stream']}\n`);
});
file.end();
debug(` └M3U-Write: ${source.name}${model.name}`);
resolve();
});
};
const diffHours = (dtStr) => {
const pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}) ([+\-0-9]{3})(\d{2})/;
const dt = new Date(dtStr.replace(pattern, '$1-$2-$3 $4:$5:$6 $7:$8'));
return (dt - new Date())/1000/60/60;
};
const processEPG = (source, streams) => {
debug(` ┌EPG-Process: ${source.name}`);
return new Promise(resolve => {
// Always M3U before EPG, so no need to check export folder
const xmlStream = flow(fs.createReadStream(`${config.importFolder}/${source.name}.xml`));
const epg = fs.createWriteStream(`${config.exportFolder}/${source.name}.xml`);
//
epg.write('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE tv SYSTEM "xmltv.dtd">\n<tv>\n');
xmlStream.on('tag:channel', (node) => {
if (typeof node.$attrs !== 'undefined' && node.$attrs.id !== '' && streams.indexOf(node.$attrs.id) >= 0) {
epg.write(flow.toXml(node));
epg.write('\n');
}
});
xmlStream.on('tag:programme', (node) => {
if (streams.indexOf(node.$attrs.channel) >= 0) {
if (diffHours(node.$attrs.start) < 48 && diffHours(node.$attrs.stop) > -1) {// Starts in less than 48 hours and Finishes less than 1 hour ago
epg.write(flow.toXml(node));
epg.write('\n');
}
}
});
xmlStream.on('end', () => {
epg.write('</tv>');
debug(` └EPG-Process: ${source.name}`);
resolve();
});
});
};
const processSource = async (source) => {
debug(`┌Source: ${source.name}`);
let streams;
try {
await getFile(source.m3u, `${config.importFolder}/${source.name}.m3u`);
const models = [];
for (const model of source.models) {
models.push(processM3U(source, model)
.then(async (result) => {
await exportM3U(source, model, result);
}));
}
await Promise.all(models);
} catch (err) {
console.log(err);
}
if (source.epg) {
try {
await getFile(source.epg, `${config.importFolder}/${source.name}.xml`);
streams = await processM3U(source, source.models[0]);
await processEPG(source, streams.map(x => x['tvg-id']));
} catch (err) {
console.log(err);
}
}
debug(`└Source: ${source.name}`);
};
(async () => {
const sources = [];
for (const source of config.sources) {
sources.push(processSource(source));
}
Promise.all(sources);
})();